Search code examples
csscss-shapes

How to create a down arrow using CSS?


I try to do an arrow with transparent color inside and coloured border but I'm new in this world . Someone can help me to do something like this: arrow down with text inside

My code:

.pentagon {
    position: relative;
    width: 350px;
    top:20rem;
    left: 20rem;
    border-width: 80px 0 100px 0;
    border-style: solid;
    border-color: #01ff70 transparent;
}
.pentagon:before {
    content: "";
    position: absolute;
    height: 0;
    width: 0;
    left: -7.5rem;
    border-width: 100px 300px 300px;
    border-style: solid;
    border-color: transparent transparent #01ff70;
    transform: rotate(180deg);
}

Solution

  • I would build this with multiple gradient to create each line:

    .arrow {
      width:200px;
      height:200px;
      background:
    
        linear-gradient(blue,blue)  top center/ 100px 3px,
        
        linear-gradient(blue,blue)  calc(50% - 50px) 0/3px 50%,
        linear-gradient(blue,blue)  calc(50% + 50px) 0/3px 50%,
        
        linear-gradient(blue,blue)  0 50%/51px 3px,
        linear-gradient(blue,blue)  100% 50%/51px 3px,
        
        linear-gradient(to bottom right,
          transparent calc(50% - 2px),blue calc(50% - 2px),
          calc(50% + 2px),transparent calc(50% + 2px)) 100% 100%/50% 50%,
        linear-gradient(to bottom left,
          transparent calc(50% - 2px),blue calc(50% - 2px),
          calc(50% + 2px),transparent calc(50% + 2px)) 0 100%/50% 50%;
        
      background-repeat:no-repeat;
      text-align:center;
      line-height:220px;
    }
    <div class="arrow">
      text inside
    </div>

    And with some CSS variable to control the different values:

    .arrow {
      --c:blue; /*main color*/
      --t:2px; /*thickness*/
      --d: 100px; /*top length*/
      --p:50%; /*percentage of top*/
      
      --g:linear-gradient(var(--c),var(--c));
      width:200px;
      height:200px;
      background:
        /*1*/
        var(--g) top center/ var(--d) var(--t),
        /*2*/
        var(--g)  calc(50% - var(--d)/2) 0/var(--t) calc(var(--p) + var(--t)/2),
        var(--g)  calc(50% + var(--d)/2) 0/var(--t) calc(var(--p) + var(--t)/2),
        /*3*/
        var(--g)  left  0 top var(--p)/calc(50% - var(--d)/2) var(--t),
        var(--g)  right 0 top var(--p)/calc(50% - var(--d)/2) var(--t),
        /*4*/
        linear-gradient(to bottom right,
          transparent calc(50% - var(--t)/2 - 1px),var(--c) calc(50% - var(--t)/2),
          calc(50% + var(--t)/2),transparent calc(50% + var(--t)/2 + 0.5px)) 100% 100%/50% calc(100% - var(--p)),
        linear-gradient(to bottom left,
          transparent calc(50% - var(--t)/2 - 1px),var(--c) calc(50% - var(--t)/2),
          calc(50% + var(--t)/2),transparent calc(50% + var(--t)/2 + 0.5px)) 0 100%/50% calc(100% - var(--p));
        
      background-repeat:no-repeat;
      display:inline-block;  
      text-align:center;
      line-height:220px;
    }
    <div class="arrow">
      text inside 
    </div>
    <div class="arrow" style="--c:red;--t:5px;--d:40px;--p:40%">
     text inside
    </div>
    <div class="arrow" style="--c:green;--t:3px;--d:70px;--p:60%">
     text inside
    </div>

    1. a line placed at the top center with a width equal to --d and a height equal to --t
    2. The two vertical lines with a width equal to --t and a height equal --p + half the thickness to have them correctly linked with the bottom part. We place the first line at the top 0 and from the center we remove half --d so it start at the left of the top line. Same logic with the other line at the right.
    3. The two horizontal lines with a width equal to half the width left by the top line (100% - var(--d))/2 and a height equal to --t. Their placement is easy to find. the left one at 0 from the left and --p from the top. Same logic for the right one.
    4. The two diagonal lines are two gradient that will take half the width and 100% - var(--p). Here we play with the coloration to color only a diganonal part and we keep the remaining transparent.

    If you want more, we can add a coloration inside the arrow:

    .arrow {
      --c:blue; /*main color*/
      --b:red; /*background color*/
      --t:2px; /*thickness*/
      --d: 100px; /*top length*/
      --p:50%; /*percentage of top*/
      
      --g:linear-gradient(var(--c),var(--c));
      width:200px;
      height:200px;
      background:
        /*1*/
        var(--g) top center/ var(--d) var(--t),
        /*2*/
        var(--g)  calc(50% - var(--d)/2) 0/var(--t) calc(var(--p) + var(--t)/2),
        var(--g)  calc(50% + var(--d)/2) 0/var(--t) calc(var(--p) + var(--t)/2),
        /*3*/
        var(--g)  left  0 top var(--p)/calc(50% - var(--d)/2) var(--t),
        var(--g)  right 0 top var(--p)/calc(50% - var(--d)/2) var(--t),
        /*4*/
        linear-gradient(to bottom right,
          var(--b) calc(50% - var(--t)/2 - 1px),var(--c) calc(50% - var(--t)/2),
          calc(50% + var(--t)/2),transparent calc(50% + var(--t)/2 + 0.5px)) 100% 100%/50% calc(100% - var(--p)),
        linear-gradient(to bottom left,
          var(--b) calc(50% - var(--t)/2 - 1px),var(--c) calc(50% - var(--t)/2),
          calc(50% + var(--t)/2),transparent calc(50% + var(--t)/2 + 0.5px)) 0 100%/50% calc(100% - var(--p)),
         /*5*/
         linear-gradient(var(--b),var(--b)) center top/var(--d) var(--p);
      background-repeat:no-repeat;
      display:inline-block;  
      text-align:center;
      line-height:220px;
    }
    <div class="arrow">
      text inside
    </div>
    <div class="arrow" style="--c:red;--b:yellow;--t:5px;--d:40px;--p:40%">
      text inside
    </div>
    <div class="arrow" style="--c:green;--b:pink;--t:3px;--d:70px;--p:60%">
      text inside
    </div>