Discover a unique trick that allows you to adjust the coloration of a fixed element within a different block using the property background-attachment:fixed
.block {
position: fixed;
top: 10px;
left: 10px;
height: 25px;
width: 25px;
border:1px solid;
box-sizing:border-box;
}
.is-green {
display: block;
height: 300px;
width: 100%;
background:
linear-gradient(#fff,#fff) 10px 10px/25px 25px fixed no-repeat,
green;
}
.is-white {
display: block;
height: 300px;
width: 100%;
background:
linear-gradient(#000,#000) 10px 10px/25px 25px fixed no-repeat,
#fff;
}
body {
padding-bottom:200px;
}
<div class="block">
</div>
<div class="is-green">
</div>
<div class="is-white">
</div>
<div class="is-green">
</div>
Despite its effectiveness, there are a couple of limitations with this approach: Content within the block will disrupt the background effect, and you must update values in multiple locations.
To address these issues, you can utilize pseudo-elements and CSS variables:
:root {
--t:10px;
--l:10px;
--h:25px;
--w:25px;
}
.block {
position: fixed;
z-index:999;
top: var(--t);
left: var(--l);
height: var(--h);
width: var(--w);
border:1px solid;
box-sizing:border-box;
color:red;
}
.is-green {
display: block;
height: 300px;
width: 100%;
position:relative;
z-index:0;
background:green;
}
.is-green::before {
content:"";
position:absolute;
z-index:99;
top:0;
left:0;
right:0;
bottom:0;
background:
linear-gradient(#fff,#fff) var(--l) var(--t)/var(--w) var(--h) fixed no-repeat;
}
.is-white {
display: block;
position:relative;
height: 300px;
width: 100%;
background: #fff;
color:#000;
}
.is-white::before {
content:"";
position:absolute;
z-index:99;
top:0;
left:0;
right:0;
bottom:0;
background:
linear-gradient(#000,#000) var(--l) var(--t)/var(--w) var(--h) fixed no-repeat;
}
body {
padding-bottom:200px;
}
<div class="block">
Hi
</div>
<div class="is-green">
lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum
</div>
<div class="is-white">
lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum
</div>
<div class="is-green">
lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum lorem pisum
</div>
Adjust the gradient color to your preference with ease using this method.