6

如何仅使用 CSS 绘制此图像左侧的功能区末端? 在此处输入图像描述

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

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

4

2 回答 2

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

看演示

参考

于 2012-11-12T09:53:24.520 回答
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;
}
于 2012-11-12T09:49:57.877 回答