user1765738
user1765738

Reputation: 13

CSS linear gradient for div with dynamic height

I need to create a linear gradient like this Gradient requirement

I am trying to achieve it with css like this:

background: linear-gradient(-60deg,
          rgba(255,183,107,1) 0%,
          rgba(255,167,61,1) 15%,
          rgba(255,124,0,1) 15%,
          rgba(255,127,4,1) 100%);

This works for a specific height, but as the height of the div changes the shape of gradient changes as shown here Issue when height of div increases or decreases. I want that the gradient should always touch the top right corner of div.

Any help is highly appreciated

Upvotes: 1

Views: 751

Answers (1)

Temani Afif
Temani Afif

Reputation: 272965

You can code it like below:

.box {
  height:100px;
  background: 
    linear-gradient(to bottom right,#0000 50%,rgba(255,183,107,1) 50.1%) 
        top right /1000px 1730px no-repeat /* 1.73 = tan(60deg) */
    rgba(255,127,4,1);
    
    /* resize and see the result*/
    resize:both;
    overflow:hidden;
}
<div class="box"></div>

Upvotes: 1

Related Questions