简体   繁体   English

孩子的背景颜色覆盖父母的背景图片

[英]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.

相关问题 CSS在子元素的顶部显示父元素的背景颜色吗? - CSS display parent element's background-color on top of that of the child? 绝对定位的孩子没有得到父母的背景颜色 - Absolutely positioned child not getting parent's background color 使用 Javascript 根据父级的背景颜色设置子级的不透明度值 - Set the Child's opacity value depending on the Parent's background-color with Javascript 如果父元素和子元素都包含内联背景色,则跳过向子元素添加类名 - Skip adding class name to child element(s) if both parent and child element contains inline background-color 当将鼠标悬停在其子元素上时,如何保持父元素的背景颜色不变? - how can I keep parent element's background-color unchanged when hover on its child element? 将子元素的位置更改为绝对位置后,父元素不显示背景颜色 - Parent element not showing background-color after changing child element's position to absolute 悬停父div时,更改子div的bg颜色 - When hovering parent div, change bg color of child divs 在父母背景图像上悬停时更改子颜色 - Change child color on hover over parent background image 如何为CSS类.background-color-blue添加更多特异性,使其覆盖默认div的背景色 - how to add more specificity to css class .background-color-blue so it overrides default div's background color 在HTML或CSS中更改图像的背景颜色 - Change Image's Background Color in HTML or CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM