Aral Roca
Aral Roca

Reputation: 5929

How to create a full width triangle at the bottom?

I want just to do that:

flag

However I have a lot of troubles to do it with css... Someone can help me?

This is my better try:

.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>

My doubts are about to make this white triangle on the bottom. Doesn't matter here the vertical align of the text and the font family.

Upvotes: 2

Views: 204

Answers (5)

Temani Afif
Temani Afif

Reputation: 274307

Here is an idea with gradient where it will be responsive and you will have better support than clip path:

.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>

You can also make it working with image as background but you lose transparency:

.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>

Upvotes: 1

Mohit Gupta
Mohit Gupta

Reputation: 739

Use the following code, you'll also be able to add enough content in your 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>

Upvotes: 1

Nidhi
Nidhi

Reputation: 1453

You can user after element for a triangle

Edit: You can use clip-path to make a shape as you want
This also solves your 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? comment

.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>

Upvotes: 3

maximelian1986
maximelian1986

Reputation: 2482

You may hide bottom triangle with another 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);
}

Upvotes: 0

ellipsis
ellipsis

Reputation: 12152

Set the width to 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>

Upvotes: 5

Related Questions