2012-11-12 25 views
6

如何僅使用CSS繪製此圖像左側的色帶末端? enter image description here用於色帶端的CSS

我知道我可以使用,在CSS角斜接的事實,所以我可以有大小爲0和其他邊界更大的邊界div給我三角形。有沒有辦法做到這一點只有1 div?或者我需要堆疊一些三角形?我真的希望有1 div,以便用戶不必考慮這一點,我可以使用CSS :before僞元素來插入它。實現這個最好的方法是什麼?

只需要支持IE9 +和其他瀏覽器的現代版本。

+0

@at。你有答案嗎? – freebird

回答

2

網上有很多資源顯示如何做到這一點。一個很好的教程在網上這裏的CSS技巧http://css-tricks.com/snippets/css/ribbon/

香港專業教育學院還停留它在的jsfiddle你在這裏跟http://jsfiddle.net/WqNQU/

<h1 class="ribbon"> 
    <strong class="ribbon-content">Everybody loves ribbons</strong> 
</h1> 



.ribbon { 
font-size: 16px !important; 
/* This ribbon is based on a 16px font side and a 24px vertical rhythm. I've used em's to position each element for scalability. If you want to use a different font size you may have to play with the position of the ribbon elements */ 

width: 50%; 

position: relative; 
background: #ba89b6; 
color: #fff; 
text-align: center; 
padding: 1em 2em; /* Adjust to suit */ 
margin: 2em auto 3em; /* Based on 24px vertical rhythm. 48px bottom margin - normally 24 but the ribbon 'graphics' take up 24px themselves so we double it. */ 
} 
.ribbon:before, .ribbon:after { 
content: ""; 
position: absolute; 
display: block; 
bottom: -1em; 
border: 1.5em solid #986794; 
z-index: -1; 
} 
.ribbon:before { 
left: -2em; 
border-right-width: 1.5em; 
border-left-color: transparent; 
} 
.ribbon:after { 
right: -2em; 
border-left-width: 1.5em; 
border-right-color: transparent; 
} 
.ribbon .ribbon-content:before, .ribbon .ribbon-content:after { 
content: ""; 
position: absolute; 
display: block; 
border-style: solid; 
border-color: #804f7c transparent transparent transparent; 
bottom: -1em; 
} 
.ribbon .ribbon-content:before { 
left: 0; 
border-width: 1em 0 0 1em; 
} 
.ribbon .ribbon-content:after { 
right: 0; 
border-width: 1em 1em 0 0; 
} 
4

HTML

<div class="ribbon"> 
    <strong class="ribbon-content">Everybody loves ribbons</strong> 
</div>​ 

CSS

.ribbon { 
    font-size: 16px !important; 
    width: 50%; 
    position: relative; 
    background: #ba89b6; 
    color: #fff; 
    text-align: center; 
    padding: 1em 2em; /* Adjust to suit */ 
    margin: 2em auto 3em; 
    } 
    .ribbon:before { 
    content: ""; 
    position: absolute; 
    display: block; 
    bottom: -1em; 
    border: 1.5em solid #986794; 
    z-index: -1; 
    } 
    .ribbon:before { 
    left: -2em; 
    border-right-width: 1.5em; 
    border-left-color: transparent; 
    } 

.ribbon .ribbon-content:before { 
content: ""; 
position: absolute; 
display: block; 
border-style: solid; 
border-color: #804f7c transparent transparent transparent; 
bottom: -1em; 
} 
.ribbon .ribbon-content:before { 
left: 0; 
border-width: 1em 0 0 1em; 
} 

See Demo

Reference

+0

我印象深刻!即使IE8支持這個! –

+0

@BramVanroy謝謝你的隊友!需要進一步調整以匹配圖像。 – freebird

+0

@ freebird不需要進一步匹配圖像,我只對功能區結束感興趣。現在就試試這個。 –