三種純CSS實現三角形的方法

spacer.gifspacer.giftip-triangle

看到像上圖這樣的 tip 的小三角,你會怎麼辦?

切個圖上去?恩,不錯,簡單,兼容性也一級棒,不但好控制,那點小東西也增加不了多少圖片的大小。但有沒有更好更講究技巧的辦法呢?哈哈,那必須有啊,而且還不止一種呢:)

純 CSS 做三角形的方法,目前我知道三種,分別是利用 border 屬性,“◆”字符,和 CSS3 transfrom 做 45 度旋轉實現的,CSS3的方法是在碼頭哥的博客上學到的,很感謝你們的分享,前端有你們更精彩!

1.利用 border 屬性實現三角形

這個原理很簡單,我我們先看下面的圖,這是一個邊框爲 20px 的 div,看他的邊框,是個梯形,變化會從這裏開始。

div-border-20

CSS:

.triangle{ width:30px; height:30px; border-width:20px; border-style:solid; border-color:#e66161#f3bb5b #94e24f #85bfda; }

好的,現在我把它的寬和高都設爲 0,看看有什麼變化。

div-w-h-0

四個邊框都變成三角形了,現在我再把它的左右和下邊框的顏色都設成透明或和背景顏色相同的顏色,就出來我們想要的三角形了,推薦把邊框設置成透明,這樣拓展性更好。

注:IE6下把邊框設置成 transparent 時會出現黑影,並不會透明,把 border-style 設置成 dashed 可以解決。

border-t-n

CSS:

.triangle{ width:0; height:0; border-width:20px; border-style:solid dashed dashed dashed; border-color:#e66161 transparent transparent transparent; }

如果我們想實現下圖的效果該怎麼辦呢?很簡單,做兩個小三角,一個是背景色,一個是邊框色,然後利用定位重疊在一起,記住他們的定位要相差一個像素。

boder-triangle

HTML:


class="message-box"> >我是利用 border 屬性實現的>

class="triangle-border tb-border">

>

class="triangle-border tb-background">

>

>

CSS:

.message-box { position:relative; width:240px; height:60px; line-height:60px; background:#E9FBE4;box-shadow:1px 2px 3px #E9FBE4; border:1px solid #C9E9C0; border-radius:4px; text-align:center;color:#0C7823; } .triangle-border { position:absolute; left:30px; overflow:hidden; width:0;height:0; border-width:10px; border-style:solid dashed dashed dashed; } .tb-border { bottom:-20px;border-color:#C9E9C0 transparent transparent transparent; } .tb-background { bottom:-19px; border-color:#E9FBE4 transparent transparent transparent; }

猛擊demo

2.利用”◆“字符實現三角形

字符實現也是要用兩個字符用絕對定位去模擬,只是它不能模擬出三角形,它是個菱形,然後露出半個頭,底色又和背景色一樣,看上去就像是個三角形了。。

注意:它的大小是由 font-size 決定的,width 和 height 都決定不了,但最好還是加上,這樣各個瀏覽器去生成這個字符的時候能保持一致,我們去寫絕對定位的時候就不用寫 hack 了。

HTML:


class="message-box"> >我是利用 ◆ 字符實現的>

class="triangle-character tc-border">

>

class="triangle-character tc-background">

>

>

CSS:

.message-box { position:relative; width:240px; height:60px; line-height:60px; background:#E9FBE4;box-shadow:1px 2px 3px #E9FBE4; border:1px solid #C9E9C0; border-radius:4px; text-align:center;color:#0C7823; } .triangle-character { position:absolute; left:30px; overflow:hidden; width:26px;height:26px; font:normal 26px "宋體"; // 字符的大小和字體也有關係哦! } .tc-border { bottom:-13px;color:#C9E9C0; } .tc-background { bottom:-12px; color:#E9FBE4; }

猛擊demo

3.利用 CSS3 transfrom 旋轉 45 度實現三角形

先創建一個帶 border 的 div ,設置好背景色和相鄰的兩個邊框的顏色,然後選擇 45 度,聽着很簡單是嗎,但是利用 IE 的 matrix filter 實現 css3 transfrom 的兼容方案很頭大,我是沒看懂,有看懂的兄弟情賜教啊:)

注:IE6下無效。

triangle-transform-45

HTML:


class="message-box"> >我是利用 css transfrom 屬性字符實現的>

class="triangle-css3 transform ie-transform-filter">

>

>

CSS:

.message-box { position:relative; width:240px; height:60px; line-height:60px; background:#E9FBE4;box-shadow:1px 2px 3px #E9FBE4; border:1px solid #C9E9C0; border-radius:4px; text-align:center;color:#0C7823; } .triangle-css3 { position:absolute; bottom:-8px; bottom:-6px\0; left:30px;overflow:hidden; width:13px; height:13px; background:#E9FBE4; border-bottom:1px solid #C9E9C0;border-right:1px solid #C9E9C0; } .transform { -webkit-transform:rotate(45deg); -moz-transform:rotate(45deg); -o-transform:rotate(45deg); transform:rotate(45deg); } .ie-transform-filter { -ms-filter: "progid:DXImageTransform.Microsoft.Matrix( M11=0.7071067811865475,M12=-0.7071067811865477, M21=0.7071067811865477, M22=0.7071067811865475, SizingMethod='auto expand')"; filter: progid:DXImageTransform.Microsoft.Matrix( M11=0.7071067811865475,M12=-0.7071067811865477, M21=0.7071067811865477, M22=0.7071067811865475, SizingMethod='auto expand'); }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章