【问题标题】:CSS for ribbon end用于功能区末端的 CSS
【发布时间】:2012-11-12 09:44:56
【问题描述】:

如何仅使用 CSS 绘制此图像左侧的功能区末端?

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

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

【问题讨论】:

  • @at.你得到答案了吗?

标签: css pseudo-element css-shapes


【解决方案1】:

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

【讨论】:

  • 我印象深刻!连IE8都支持!
  • @BramVanroy 谢谢你的朋友!需要进一步调整以匹配图像。
  • @freebird 不需要再匹配图片了,我只对功能区的末端感兴趣。现在就试试这个。
  • 这个答案效果很好。实际上我所需要的只是让左侧透明,它给了我丝带的末端!非常简单。但是,这个答案与@DominicGreen 的答案完全相同,他首先回答,所以我必须奖励他。
【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    相关资源
    最近更新 更多