如何获得' div'形状与CSS的旗帜

时间:2015-03-08 21:25:37

标签: html css svg css-shapes

我想在网站上的一些元素上添加标签,并设计一个标签,底部是倒V形切口的旗帜。

到目前为止,我有这个:

HTML

<div class="css-shapes"></div>

CSS

.css-shapes{
    border-left: 99px solid #f00fff;
    border-right: 99px solid #f00fff;
    border-bottom: 39px solid transparent;
}

http://jsfiddle.net/yhexkm4u/2/

然而,我需要背景是白色的并且在紫色和1px的这个形状周围边界。我试图在这个内部装入相同形状的白色,但是一切都变得混乱而且没有像预期的那样。

也许这是一种错误的方法,但我希望最终得到的标签看起来像这样:

7 个答案:

答案 0 :(得分:10)

使用CSS:

您可以在伪元素上使用CSS transforms来创建底部带有透明倒三角形的背景:

&#13;
&#13;
body{background:url('http://lorempixel.com/image_output/food-q-c-640-480-1.jpg');background-size:cover;}
p{
  position: relative;
  width: 150px; height: 150px;
  overflow: hidden;
  border-top:3px solid #EF0EFE;
}
p:before, p:after{
  content: '';
  position: absolute;
  top: -3px;
  height: 100%; width: 50%;
  z-index: -1;
  border:2px solid #EF0EFE;
  box-sizing:border-box;
}
p:before{
  left: 0;  
  transform-origin: 0 0;
  transform: skewY(-20deg);
  border-width:0 0 4px 3px;
}
p:after{
  right: 0;
  transform-origin: 100% 0;
  transform: skewY(20deg);
  border-width:0 3px 4px 0;
}
&#13;
<p>Some text ... </p>
&#13;
&#13;
&#13;

请注意,您需要在transformtransform-origin属性上添加供应商前缀,以最大限度地提高浏览器支持。有关详细信息,请参阅canIuse

使用SVG

另一种方法是使用带有多边形元素的inline SVG

&#13;
&#13;
body{background: url('http://lorempixel.com/image_output/food-q-c-640-480-1.jpg');background-size: cover;}
div{position: relative;width: 100px; height: 150px;}
svg{position: absolute;width: 100%;height: 100%;z-index: -1;}
&#13;
<div>
  <svg viewbox="-1.5 -1.5 103 153">
    <polygon points="100 0, 100 100, 50 85, 0 100, 0 0" fill="transparent" stroke-width="3" stroke="#ef0efe"/>
  </svg>
  <p>Some text ... </p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:7)

使用pseudo-elementstransform旋转创建如下所示的轮廓横幅的方法略有不同:

Before overflow cut

  • 此角度形状由position: absolute个伪元素:before:after创建:

Before overflow cut

  • 在父母身上用overflow: hidden切断多余的部分以形成我们的横幅:

After overflow cut

  • 使用box-shadow创建轮廓,并通过拉动/推动x轴46px防止两个角度重叠 - box-shadow: 46px 0 0 3px #000

完整示例

&#13;
&#13;
div {
  height: 100px;
  width: 100px;
  margin: 100px auto;
  position: relative;
  overflow: hidden;
  border: solid 3px #000;
  border-bottom: none;
  text-align: center;
}
div:before,
div:after {
  content: '';
  display: block;
  height: 100%;
  width: 200%;
  transform: rotate(20deg);
  box-shadow: 46px 0 0 3px #000;
  position: absolute;
  top: 1px;
  right: -120%;
}
div:after {
  transform: rotate(-20deg);
  left: -120%;
  box-shadow: -46px 0 0 3px #000;
}
&#13;
<div>Text</div>
&#13;
&#13;
&#13;

答案 2 :(得分:4)

<强> STOLEN FROM CSS-SHAPES

#flag {
  width: 110px;
  height: 56px;
  padding-top: 15px;
  position: relative;
  background: red;
  color: white;
  font-size: 11px;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
}
#flag:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-bottom: 13px solid #eee;
  border-left: 55px solid transparent;
  border-right: 55px solid transparent;
}

样本:

#flag {
  width: 110px;
  height: 56px;
  padding-top: 15px;
  position: relative;
  background: red;
  color: white;
  font-size: 11px;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
}
#flag:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-bottom: 13px solid #eee;
  border-left: 55px solid transparent;
  border-right: 55px solid transparent;
}
          
<div id="flag"></div>


我的方法

我的方法使用偏斜元素,并允许您快速定位它们。

div {
  height: 100px;
  width: 100px;
  position: relative;
  border-left: 10px solid tomato;
  border-top: 10px solid tomato;
  border-right: 10px solid tomato;
  text-align: center;
  line-height: 100px;
  font-size: 30px;
}
div:before {
  content: "";
  position: absolute;
  height: 50%;
  width: 50%;
  left: -10px; /*width of border*/
  bottom: -30px;
  z-index: -2;
  -webkit-transform: skewY(-20deg);
  transform: skewY(-20deg);
  border-bottom: 10px solid tomato;
  border-left: 10px solid tomato;
}
div:after {
  content: "";
  position: absolute;
  height: 50%;
  width: 50%;
  right: -10px; /*width of border*/
  bottom: -30px;
  z-index: -2;
  -webkit-transform: skewY(20deg);
  transform: skewY(20deg);
  border-bottom: 10px solid tomato;
  border-right: 10px solid tomato;
}
div:hover, div:hover:before, div:hover:after{
  background:lightgray;
  }
<div>TEXT</div>

答案 3 :(得分:2)

我已经开始更新您的CSS以创建您想要的效果:

&#13;
&#13;
.css-shapes {
  height: 250px;
  width: 0px;
  border-left: 99px solid #f00fff;
  border-right: 99px solid #f00fff;
  border-bottom: 39px solid transparent;
  position: relative
}
.n-shape {
  height: 248px;
  width: 0px;
  border-left: 95px solid #ffffff;
  border-right: 95px solid #ffffff;
  border-bottom: 39px solid transparent;
  position: absolute;
  top: -6px;
  right: -95px;
}
.top {
  position: absolute;
  top: 0px;
  width: 198px;
  height: 2px;
  background-color: #f00fff;
  left: -99px;
  border-bottom: 1px solid #f00fff;
}
&#13;
<div class="css-shapes">
  <div class="n-shape"></div>
  <div class="top"></div>
</div>
&#13;
&#13;
&#13;

小提琴:http://jsfiddle.net/dywhjwna/

答案 4 :(得分:1)

快速解决方法是旋转它:

 transform: rotate(90deg);

Fiddle


另一种解决方案是SVG路径here's a fiddle!

答案 5 :(得分:1)

这是我想出的。

Link Fiddle

它对应于你所寻找的东西,但我想应该有一个更好的方式&#34;而不是玩边框。

HTML

<div id="text-div">
    Text
</div>
<div id="pacman">
    <div id="left-triangle"></div>
    <div id="right-triangle"></div>
</div>

CSS

#text-div {
    width: 118px;
    height: 60px;
    text-align: center;
    border: 1px solid purple;
    border-bottom: 0px;
    line-height: 60px;
}

#pacman { 
    width: 0px; 
    height: 0px;
    border-right: 60px solid purple; 
    border-top: 0px; 
    border-left: 60px solid purple; 
    border-bottom: 60px solid transparent;
}

#left-triangle{
    position: relative;
    left: -59px;
    border-right: 58px solid transparent; 
    border-top: 0px; 
    border-left: 58px solid white; 
    border-bottom: 58px solid transparent;
}
#right-triangle{
    position: relative;
    top: -59px;
    left: -57px;
    border-right: 58px solid white;
    border-top: 0px; 
    border-left: 58px solid transparent; 
    border-bottom: 58px solid transparent;
}

答案 6 :(得分:0)

一种更好的解决方案,将文本轻松放置在中间,底部使用矩形背景,底部使用三角形。

.css-shapes{ 
  position: relative;
  height: 250px; 
  width: 150px; 
  background: #FFD05B;
  color: #fff;
  text-align: center;
  line-height:225px;
  font-size: 90px;
  box-sizing: border-box;
}
.css-shapes:after{
  content: '';
  position:absolute;
  left:0;
  bottom: 0;
  display: block;
  width: 100%;
  height:50px;
  border-bottom: 25px solid #fff;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
  box-sizing: border-box;
}
<div class="css-shapes">1</div>