如何在底部创建一个全角三角形?

时间:2019-02-01 07:17:51

标签: html css css-shapes

我只想这样做:

flag

但是我在使用CSS时遇到很多麻烦...有人可以帮助我吗?

这是我更好的尝试:

.flag.vertical {
    background-color: #dd7758;
    height: 0;
    padding-bottom: 25px;
    text-align: center;
    color: white;
    border-style: solid;
    border-width: 0 10px 10px 10px;
    border-color: transparent transparent white transparent;
}
<div class="flag-wrapper"><span class="flag vertical">-5%</span></div>

我的疑问是要使这个白色三角形位于底部。此处的文本和字体系列的垂直对齐并不重要。

5 个答案:

答案 0 :(得分:5)

width设置为0

.flag-wrapper{ background-color: #dd7758;
    height: 0;
    padding-bottom: 25px;
   text-align: center;
    color: white;
    border-style: solid;
    border-width: 20px 20px 20px 20px;
    border-color: transparent transparent white transparent;
    width: 0; 
    }

span{
margin-left:-10px
}
<div class="flag-wrapper"><span>-5%</span></div>

答案 1 :(得分:3)

您可以在元素之后使用三角形

编辑:您可以根据需要使用clip-path制作形状
这也可以解决您的Another question that maybe do you know. If I want to add this div over an image, is it possible that the triangle will be transparent instead of white?评论

.flag.vertical {
    background-color: #dd7758;
    padding: 10px 10px 25px;
    text-align: center;
    color: white;
    display: inline-block;
    clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 50% 70%, 0% 100%);
}
<div class="flag-wrapper"><span class="flag vertical">-5%</span></div>

答案 2 :(得分:1)

使用以下代码,您还可以在div

中添加足够的内容

* {
  box-sizing: border-box;
}
.flag {
  width: 110px;
  height: 56px;
  margin: 0 auto;
  padding-top: 15px;
  position: relative; 
  background: hotpink;
  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 white;
  border-left: 55px solid transparent;
  border-right: 55px solid transparent;
}
<div class="flag">
  5 Items
</div>

答案 3 :(得分:1)

这里有一个渐变的想法,它会响应,并且您将比剪辑路径有更好的支持:

.flag-wrapper {
    background-color: #dd7758;
    padding:10px 5px 30px;
    margin:10px;
    text-align: center;
    color: white;
    display:inline-block;
    background:
      linear-gradient(to top left,transparent 48%,#dd7758 50%) bottom left/50% 15px,
      linear-gradient(to top right,transparent 48%,#dd7758 50%) bottom right/50% 15px,
      linear-gradient(#dd7758,#dd7758)top/100% calc(100% - 15px);
    background-repeat:no-repeat;
}

body {
  background:#ccc;
}
<div class="flag-wrapper">-5%</div>
<div class="flag-wrapper">-25%</div>
<div class="flag-wrapper">-100%</div>

您还可以使其与图像作为背景一起使用,但会失去透明度:

.flag-wrapper {
    background-color: #dd7758;
    padding:10px 5px 30px;
    margin:10px;
    text-align: center;
    color: white;
    display:inline-block;
    background:
      linear-gradient(to bottom right,transparent 48%,#ccc 50%) bottom left/50.1% 15px,
      linear-gradient(to bottom left,transparent 48%,#ccc 50%) bottom right/50.1% 15px,
     url(https://picsum.photos/200/300?image=1069)center/cover;
    background-repeat:no-repeat;
}

body {
  background:#ccc;
}
<div class="flag-wrapper">-5%</div>
<div class="flag-wrapper">-25%</div>
<div class="flag-wrapper">-100%</div>

答案 4 :(得分:0)

您可以与另一个div一起隐藏底部三角形

HTML:

<div class="flag-wrapper">
   -5%
   <div class="pointer">
</div></div>

CSS:

.flag-wrapper{
  text-align: center;
  padding-top: 10px;
  background-color: #dd7758;
  width:50px;
  height:65px;
}
.pointer{
  content: ' ';
    position: absolute;
    width: 45px;
    height: 45px;
    left: 10px;
    top: 54.5px;
    background-color: #fcfcfc;
    transform: rotate(45deg) skew(5deg, 5deg);
    -moz-transform: rotate(45deg) skew(5deg, 5deg);
    -ms-transform: rotate(45deg) skew(5deg, 5deg);
    -o-transform: rotate(45deg) skew(5deg, 5deg);
    -webkit-transform: rotate(45deg) skew(5deg, 5deg);
}