css - 为什么子元素的背景图像位于父元素背景颜色之上?

标签 css

我有以下 CSS 代码:

#overlayouter
        {
            display: inline-block;
            background-color: red;

            width: 900px;
            height: 900px;

            z-index: 100;
        }
        #overlayinner
        {
            display: inline-block;
            position: relative;

            z-index: 0;

            width: 900px;
            height: 900px;

            background-image: url(picurl);
            background-position: 0 0;
            background-repeat: no-repeat;
        }

HTML 只是:

<div id="overlayouter">
    <div id="overlayinner">

    </div>    
</div>

我的子 div 中有一个背景图像。

我想要在它的父 div 中使用 rgb() 的背景颜色(现在它只是红色)。

但是为什么子元素的背景在上面呢?是因为一个元素只能有背景颜色或背景图像吗? child 的种类占据了主导地位?

我希望父 div 稍后有一个 rgba,这将产生某种类型的覆盖。

我已经设置了一个 jsfiddle 显示问题.. http://jsfiddle.net/9Rj9V/

最佳答案

Without z-index:
enter image description here

With z-index:
enter image description here

The Stacking Context:
With z-index

In summary:

  • Positioning and assigning a z-index value to an HTML element creates a stacking context, (as does assigning non-full opacity).
  • Stacking contexts can be contained in other stacking contexts, and together create a hierarchy of stacking contexts.
  • Each stacking context is completly independent from its siblings: only descendant elements are considered when stacking is processed.
  • Each stacking context is self-contained: after the element's contents are stacked, the whole element is considered in the stacking order of the parent stacking context. -MDN

关于css - 为什么子元素的背景图像位于父元素背景颜色之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17000580/

相关文章:

javascript - 我正在尝试按顺序将数字插入到 div 主体中

javascript - 基本 React 代码不产生任何输出

html - 删除 Twitter 徽章的列表样式

css - 更改文档片段的 css 类定义

html - Bootstrap : how to make left and right containers with same height?

html - Bootstrap 控件布局偏移问题

jquery - 如何淡出一个选项卡并将其替换为另一个选项卡,而不从页面中删除任何一个?

html - 如何制作响应式填充?

javascript - 计算父级边距的 outerHeight

html - 我该如何处理设置 z-index?悬停在过渡完成之前取消 z-index