如何仅使用CSS绘制此图像左侧的功能区末尾?
我知道我可以使用CSS中的角落被斜接的事实,所以我可以让div
的边框大小为0,其他边框更大,以给我三角形。有没有办法只用1 div
来做到这一点?或者我需要堆叠一些三角形?我真的更喜欢有1 div
,所以用户不必考虑这个,我可以使用CSS :before
伪元素来插入它。实现这个的最佳方法是什么?
IE9 +和其他浏览器的现代版本只需要支持。
答案 0 :(得分:5)
<强> 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 强>
答案 1 :(得分:2)
网上有很多资源显示如何执行此操作。这里有一个非常好的教程在css-tricks上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;
}