简体   繁体   English

CSS 元素共享相同的线性渐变背景

[英]CSS Elements sharing the same linear gradient background

I want to do a section in my project with elements sharing the same linear background image, that are all surrounded by a different color background.我想在我的项目中做一个部分,元素共享相同的线性背景图像,这些元素都被不同的颜色背景包围。

I can sort of do with z-indexes and background: inherit, but the problem is that every element contains the whole gradient, not the part of it depending on it's position like I want.我可以使用 z-indexes 和背景:继承,但问题是每个元素都包含整个渐变,而不是它的一部分,这取决于我想要的位置。

Here is my attempt:这是我的尝试:

 .gradient { position: absolute; height: 100vh; width: 100vw; background: linear-gradient(90deg, rgba(242, 251, 95, 1) 0%, rgba(0, 89, 135, 1) 100%, rgba(99, 102, 23, 1) 100%); z-index: 100; display: flex; align-items: center; justify-content: center; gap: 100px; } .bg { position: absolute; min-width: 80%; height: 80%; background-color: red; } .circle { position: relative; border-radius: 50%; min-width: 100px; min-height: 100px; border: 2px solid green; background: inherit; display: flex; justify-content: center; align-items: center; z-index: 1; }
 <div class="gradient"> <div class="circle">ABC</div> <div class="circle">ABC</div> <div class="bg"></div> </div>

I'm having a hard time describing it in English so I included two pictures.我很难用英语描述它,所以我附上了两张照片。

What I did我做了什么

What I want我想要的是

You could try moving the circles into the red div and giving that a mix blend mode and the circles a background of grey:您可以尝试将圆圈移动到红色 div 中,并为其提供混合混合模式,并且圆圈具有灰色背景:

 .gradient { position: absolute; height: 100vh; width: 100vw; background: linear-gradient(90deg, rgba(242, 251, 95, 1) 0%, rgba(0, 89, 135, 1) 100%, rgba(99, 102, 23, 1) 100%); z-index: 100; display: flex; align-items: center; justify-content: center; } .bg { min-width: 80%; height: 80%; background-color: red; display: flex; align-items: center; justify-content: center; gap: 100px; mix-blend-mode: hard-light; } .circle { position: relative; border-radius: 50%; min-width: 100px; min-height: 100px; border: 2px solid grey; background: inherit; display: flex; justify-content: center; align-items: center; z-index: 1; background-color: grey; }
 <div class="gradient"> <div class="bg"> <div class="circle">ABC</div> <div class="circle">ABC</div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM