1. 程式人生 > 實用技巧 >使用css3在網頁中實現各種三角形樣式集合(原理解析)

使用css3在網頁中實現各種三角形樣式集合(原理解析)

大家在瀏覽web網頁的時候經常看到有各種各樣的圖形,比如圓形,正方形,長方形,三角形等,今天特意做了一個關於用CSS實現三角形的教程,那css三角形怎麼寫呢?css實現三角形的原理是什麼?

推薦手冊:css線上手冊

請看一下例項詳解。

(相關視訊教程推薦:CSS教程

css3三角形使用場景

我們知道有一個特效是span下拉列表,也就是說當滑鼠放上一個div時,會出現原本隱藏的內容或者二級選單,這樣一個下拉列表會突出來一個小的三角形指向原本的div區域塊,如圖所示:

那麼我們現在來總結一下,我們是如何寫出這些三角形的。

CSS3三角形實現原理

在css3中,我們主要使用的是一個transparent的屬性,意思是透明的,比如我們將字型設定為黑色會寫color:black,那麼如果想把字型的顏色設定為透明,我們可以寫color:transparent。同理background-color:transparent可以設定背景為透明。

三角形8種分類

我們根據三角形的位置分為8種:

1

triangle up,triangle down,triangle left,triangle right,triangle top left,triangle top right,triangle bottom left和triangle bottom right。


8種三角形實現例項

1.triangle up 上三角形

1

2

3

4

5

6

7

#triangle-up{

width:0;

height:0;

border-left:50px solid transparent;

border-right

:50px solid transparent;

border-bottom:100px solid black;

}

2.triangle down 下三角形

1

2

3

4

5

6

7

#triangle-down{

width:0

height:0

border-left50px solid transparent

border-right:50px solid transparent;

border-top:100px solid black;

}

3.triangle left 左三角形

1

2

3

4

5

6

7

#triangle-

left{

width:0

height:0

border-top50px solid transparent

border-bottom:50px solid transparent;

border-right:100px solid black;

}

4.triangle right 右三角形

1

2

3

4

5

6

7

#triangle-right{

width:0

height:0

border-top50px solid transparent

border-bottom:50px solid transparent;

border-left:100px solid black;

}

5.triangle top left 左上三角形

1

2

3

4

5

6

#triangle-topleft{

width:0

height:0

border-top100px solid black

border-right:100px solid transparent;

}

6.triangle top right 右上三角形

1

2

3

4

5

6

#triangle-topright{

width:0

height:0

border-top100px solid black

border-left100px solid transparent

}

7.triangle bottom left 左下三角形

1

2

3

4

5

6

#triangle-bottomleft{

width:0

height:0

border-bottom100px solid black

border-right100px solid transparent

}

8.triangle bottom right 右下三角形

1

2

3

4

5

6

#triangle-bottomright{

width:0

height:0

border-bottom100px solid black

border-left100px solid transparent

}

大致的效果如圖所示: