简体   繁体   English

如何在没有父div的背景图像的情况下让子div

[英]How can I have child div without background image from parent div

I have two divs: One parent, one its child. 我有两个div:一个父母,一个孩子。

Parent has background image: 家长有背景图片:

.parent{
  background-image: url('urltoimage/img.png');
  z-index: 1;
 }
.child{
  z-index: 2;
 }

but parent background image is still covering the background of its child. 但父级背景图片仍覆盖其子级的背景。 I want that child doesnot have background image 我希望那个孩子没有背景图片

I also tried with z-index , but no success. 我也尝试使用z-index ,但没有成功。

You have no image for the child, nor is any background color for the child specified. 您没有给孩子的图像,也没有为孩子指定任何背景色。 By inheritance property, the child inherits the parent attributes and overrides the parent attributes' values it specifies. 通过继承属性,子级继承父级属性,并覆盖其指定的父级属性的值。

Your code could be something like this: 您的代码可能是这样的:

.parent{
  background-image: url('urltoimage/img.png');
  z-index: 1;
 }
.child{
   background-image: url('urltoimage/CHILD_IMAGE.png');
   // or you could specify background color
   z-index: 2;
 }

May be you are doing something wrong while writing the HTML part of the code but CSS looks fine. 编写代码的HTML部分时,可能是您做错了,但是CSS看起来不错。

HTML: HTML:

<div class="parent">
   <div class="child">
   </div>
</div>

Is this how you are doing it? 这是你的方式吗? Also don`t forget to add height and width in your css. 也不要忘记在CSS中添加高度和宽度。

Example: a JSFiddle 示例: 一个JSFiddle

If your child div has no background settings, it's natural that its' parent div background will show through. 如果您的子div没有背景设置,则自然会显示其父div背景。 If you are trying to create the effect of cutting out the area of the child's div from the parent div. 如果您尝试创建从父div切出孩子div的区域的效果。 Then, you might have to use a different approach. 然后,您可能必须使用其他方法。 One example might be to set the child's background to the same as the its' parents' parent's background. 一个示例可能是将孩子的背景设置为其父母的背景相同。 Otherwise, what has been mentioned about setting the dimensions and/or background of the child's div by others on this post will be your only recourse. 否则,其他人在这篇文章中提到的设置儿童div的尺寸和/或背景将是您的唯一选择。

Here's a thought, can you use a .png image file for the parent's background-image that has a transparent area where the child div will be positioned? 这是一个想法,您是否可以将.png图像文件用于父级的背景图像,而该背景图像具有放置子div的透明区域?

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

相关问题 如何在父div中使用Jquery而不影响子div的情况下淡入和淡出背景图像? - How can I fade are background image in and out using Jquery in a parent div without having it affect the child div? 当子容器div处于活动状态时,如何禁用父背景div? - How can I disable parent background div when a child container div is active? 如何使div的图像带有向左浮动的白色背景? - How can I have a div with an image with a white background floated left? 如何使用JavaScript在父div内显示子div而不触发其他 - How can I show the child div inside the parent div using javascript without triggering the others 如何添加子div背景图片并将其应用于整个父部分 - how to add child div background image and applied to entire parent section 如何使子图像比父div大并放置在任何位置 - How to have a child image be larger then a parent div and be positioned anywhere 有没有办法让子div的背景扩展到父div之外? - Is there a way to have a child div's background expand outside of parent div? 如何在 div 中固定背景? - How can I have fixed background in a div? 添加子div时,具有背景图像“折叠”的父div - Parent div with a background-image “collapses” when adding a child div 我如何在父div旁边有一个固定的div? - How can I have a fixed div next to a parent div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM