[英]Child's bg color overrides parent's background image
Child's bg color overrides parent's background image孩子的背景颜色覆盖父母的背景图片
I have a parent image which is required to be put as a backround of whole page.我有一个父图像,需要将其作为整个页面的背景放置。 Now if I put background color into a child div, the parent image is not visible.
现在,如果我将背景颜色放入子 div 中,则父图像不可见。 Can I somehow put background color of child behind the parent background image?
我可以以某种方式将孩子的背景颜色放在父背景图像后面吗? Is it even possible?
甚至有可能吗?
Tried this one: How to display parent's bg-image over child elements bg-color?试过这个: How to display parent's bg-image over child elements bg-color?
Code: https://codepen.io/anon/pen/KazpYM代码: https : //codepen.io/anon/pen/KazpYM
But the solutions provided doesn't work as I need to put some text in the child div which should be visible over parent background image.但是提供的解决方案不起作用,因为我需要在子 div 中放置一些文本,这些文本应该在父背景图像上可见。
.athletes{ background-image: url(https://i.imgur.com/Ytf12qW.jpg); background-repeat: no-repeat; -webkit-background-size: 45% 100%; background-size: 45% 100%; background-position: 14%; } .line{ width: 100%; height: 200px; } .line-gray{background-color: #c1c1c1;} .line-violet{background-color: #5e42b0;}
<div class="athletes"> <div class="line"></div> <div class="line line-gray"></div> <div class="line line-violet"></div> </div>
If you want just the background of child elements to be transparent use rgba(x,x,x,opacity) for color.如果您只想让子元素的背景透明,请使用 rgba(x,x,x,opacity) 作为颜色。 If you want to have a transparent image on parent use ::after pseudo selector and set opacity of the element.
如果您想在父级上使用透明图像,请使用 ::after 伪选择器并设置元素的不透明度。 Here is a working example for you.
这是一个适合您的工作示例。
.athletes{ width: 100%; height: 100%; display: block; position: relative; } .athletes::after{ top: 0; left: 0; bottom: 0; right: 0; opacity: 0.5; position: absolute; z-index: -1; content: ""; background: url(https://i.imgur.com/Ytf12qW.jpg) no-repeat center center/cover; } .line{ width: 100%; height: 200px; display: flex; color: purple; justify-content: center; align-items: center; background-color: rgba(111,111,111,0.4) } .line-gray{background-color: rgba(222,222,0,0.4);} .line-violet{background-color: rgba(127,177,0,0.4);}
<div class="athletes"> <div class="line">item1</div> <div class="line line-gray">item2</div> <div class="line line-violet">item3</div> </div>
Hope it helps, comment this answer if something is unclear.希望它有所帮助,如果有不清楚的地方,请评论此答案。
no, you can not!你不能! you must to use background color rgba for child's background!
您必须使用背景颜色 rgba 作为孩子的背景! with parameter a, you can set opacity for colors!!!
使用参数a,可以设置颜色的不透明度!!! then you can see parent background image.
然后你可以看到父背景图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.