【问题标题】:Why is the text visible, but the background is not?为什么文本可见,但背景不可见?
【发布时间】:2013-01-29 21:48:44
【问题描述】:

我有以下标记:

<div class="one">Content</div>
<div class="two"></div>

以及以下样式表:

.one, .two {
    height: 20px;
}
.one {
    background-color: #f00;
}
.two {
    background-color: #0f0;
    margin-top: -10px;
}

为什么文字Content可见,而红色背景不可见?由于给定的样式表,我希望文本也只能部分可见。

为了您的方便,我还创建了一个jsFiddle

【问题讨论】:

  • 对我来说很好(Chrome)
  • 我也可以看到红色背景(使用最新的 Chrome)。
  • 你能解释一下你期望的输出吗?
  • 在 Chrome 和 IE8 红色背景中**可见,但只有 10 像素,因为您在 div 2 上的边距为负数。
  • 对于任何误解,我深表歉意。我也可以看到红色的 div,但只有 10 像素。然而文本是完全可见的,即使它在红色 div 内

标签: css


【解决方案1】:

如果您希望第一个 div 的文本仅部分可见,则需要使用位置和 z-index。

.one, .two {
    height: 20px;
}
.one {
    background-color: #f00;
    position: relative;
    z-index: 1;
}
.two {
    background-color: #0f0;
    margin-top: -10px;
    position: relative;
    z-index: 2;
}

http://jsfiddle.net/v5LfZ/2/

【讨论】:

  • 这解决了问题,但它没有解释为什么我的versions.behaves like it?!?
  • 因为背景是背景,他应该在背景中,但如果你希望你的背景不在背景中 - 使用 z-index :)
猜你喜欢
  • 2012-11-28
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 2018-06-02
相关资源
最近更新 更多