CSS triangle filling for a progress bar

2019-07-21 11:23发布

I actually googled and searched some info but couldn't find it.

My aim is to achieve something similar to progress bar styling such as filling inside of triangle. Is there any ways? filling

JSFiddle

.angle {
    width: 0; 
    height: 0; 
    border-left: 75px solid transparent;
    border-right: 75px solid transparent;       
    border-bottom: 75px solid black;
}

3条回答
可以哭但决不认输i
2楼-- · 2019-07-21 11:37

In order to make the triangle, I would use two pseudo elements to 'cut it out' of the square div. Then, with a nested div, use absolute positioning to allow you to 'fill' it to a certain value (by setting the .amount div's height in %).

.amount {
  position: absolute;
  height: 0%;
  width: 100%;
  bottom: 0;
  left: 0;
  transition: all 1s;
  background: tomato;
}
.tri {
  position: relative;
  height: 200px;
  width: 200px;
  background: lightgray;
}
.tri:before,
.tri:after {
  content: "";
  position: absolute;
  border-top: 200px solid white;
  top: 0;
  z-index: 8;
}
.tri:before {
  border-left: 100px solid transparent;
  left: 50%;
}
.tri:after {
  border-right: 100px solid transparent;
  left: 0;
}
.tri:hover .amount {
  height: 100%;
}
<div class="tri">
  <div class="amount"></div>
</div>

查看更多
冷血范
3楼-- · 2019-07-21 11:40

May something like this?

.angle {
    position: relative;
    width: 0; 
    height: 0; 
    border-left: 100px solid transparent;
    border-right: 100px solid transparent;
    border-bottom: 100px solid blue;
}

.angle:after {
    position: absolute;
    content: "";
    top: 0;
    left: 50%;
    margin-left: -50px;
    width: 0; 
    height: 0; 
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 50px solid black;
}

fiddle: http://jsfiddle.net/bkaxzLnu/3/

查看更多
Ridiculous、
4楼-- · 2019-07-21 11:44

Here is another CSS ONLY, NO-BORDERS, NO AFTER/BEFORE HACKS option:

You could use clip-path. It allows you to show only part of an element and hide the rest.

So you could do something like this:

  .amount {
    position: absolute;
    height: 100%;
    width: 0%;
    bottom: 0;
    left: 0;
    transition: all 1s;
    background: tomato;
  }

  .tri {
    position: relative;
    width: 500px;
    height: 50px;
    background: #ddd;

    /* triangle */
    clip-path: polygon( 100% 0%,100% 100%, 0% 100%);
  }
  .tri:hover .amount {
    width: 100%;
    background: chartreuse ;
  }
<div class="tri">
  <div class="amount"></div>
</div>

查看更多
登录 后发表回答