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


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

我們知道有一個特效是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

}

大致的效果如圖所示:

下三角:

a:before{

  1. content: "";
  2. position: absolute;
  3. bottom: -10px;
  4. left: 50%;
  5. margin-left: -5px;
  6. width: 0;
  7. height: 0;
  8. border-style: solid;
  9. border-width: 5px;
  10. border-color: #b9b9b9 transparent transparent transparent;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM