IvanShport
IvanShport

Reputation: 3

How to make 1 gradient into several elements

I have a page on which there are n elements of different size and position. The background color of the page is default. And there is an element stretched across the entire page, it is filled with a gradient (for example, from yellow to red). How can I make so that the background does not change and only these n elements are filled with a gradient?

I tried to do the following: I created the element (random-div) with the "overflow: hidden" option, and put an element inside it that was stretched across the entire screen (gradient-background). I expected the element (gradient-background) to hide everything that goes beyond its size, but this did not happen. In the end, I realized that I was thinking the wrong way and completely confused.

.random-div {
    position: absolute;
    width: /* random */;
    left: /* random */;
    top: /* random */;
    overflow: hidden;
}

.gradient-background {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    background: linear-gradient(50.93deg, #00122D -8.75%, #003366 50.04%, rgba(0, 106, 106, 0.25) 153.06%);
    z-index: -150;
}

Help solve this problem. It is not necessary to follow my train of thought. Only the result is important. Any methods are accepted.

Upvotes: 0

Views: 55

Answers (1)

Temani Afif
Temani Afif

Reputation: 273389

Use background-attachment:fixed ref

body {
  background:pink;
}

.a,.b {
  width:100px;
  height:100px;
  margin:10px;
  background:linear-gradient(blue,red) fixed;
}

.b {
 margin-left:250px;
}
<div class="a">

</div>
<div class="b">

</div>

Upvotes: 2

Related Questions