Search code examples
htmlcsscss-shapes

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.


Solution

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