功能区结束的CSS

时间:2012-11-12 09:44:57

标签: css css3 pseudo-element css-shapes

如何仅使用CSS绘制此图像左侧的功能区末尾? enter image description here

我知道我可以使用CSS中的角落被斜接的事实,所以我可以让div的边框大小为0,其他边框更大,以给我三角形。有没有办法只用1 div来做到这一点?或者我需要堆叠一些三角形?我真的更喜欢有1 div,所以用户不必考虑这个,我可以使用CSS :before伪元素来插入它。实现这个的最佳方法是什么?

IE9 +和其他浏览器的现代版本只需要支持。

2 个答案:

答案 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;
}