【问题标题】:Parent’s background shows around the child’s border-radius when the parent has overflow:hidden当父母溢出时,父母的背景显示在孩子的边界半径周围:隐藏
【发布时间】:2016-02-19 17:21:53
【问题描述】:

这是我刚才问过的question 的扩展。然而,这一次的问题更具体,更接近我正在努力解决的真实案例。

如果另一个元素内有一个元素,并且父元素和子元素都具有边框半径,并且父元素的背景与子元素的背景不同,则它在子元素的圆角周围可见。

如果父元素上有overflow: hidden 属性,问题会更加严重。那么solution让子元素的边框半径小于父元素的边框半径就不再起作用了。

CodePen 示例:http://codepen.io/azangru/pen/pgmOvJ(观察子元素黑色背景下方显示的黄色背景)。

HTML:

<div class="dark-bg">
  <div class="outer">
    <div class="middle">
      <div class="inner">
      </div>
    </div>
    <div class='some-text'>
      This is text. There can be quite a decent amount of text here, so the outer div needs to have overflow: hidden. Like this, you see?
    </div>
  </div>
</div>

CSS:

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.dark-bg {
  height: 100%;
  width: 100%;
  padding-top: 10px;
  background: black;
}

.outer {
  width: 200px;
  height: 200px;
  margin: auto;
  background: white;
  border-radius: 10px;
  overflow: hidden;
}

.middle {
  width: 100%;
  height: 50%;
  background: yellow;
  border-radius: 10px 10px 0 0;
}

.inner {
  width: 100%;
  height: 100%;
  background: black;
  border-radius: 10px 10px 0 0;
}
.some-text {
  margin-top: 20px;
  padding: 0 10px;
}

有没有办法让孩子的背景完全覆盖父母的背景?

(当然,一个明显的解决方案是从父级中删除 overflow: hidden 属性并为文本添加另一个容器,该容器又将具有 overflow: hidden。但是,我真的不希望尽可能走那条路)

【问题讨论】:

    标签: css


    【解决方案1】:

    原因是背景的边框被抗锯齿,然后允许通过它进行一定量的混合。

    对内部元素应用 transform: translateZ(0px) 可以使其不那么明显。

    一个不太通用但更有效的解决方案是对内部应用阴影

    似乎还有一个伪元素,克隆基本元素属性,可以解决问题

    /*
    .inner {
        box-shadow: 0px 0px 0px 1px black;  
    }
    */
    
    
    html, body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    
    .dark-bg {
      height: 100%;
      width: 100%;
      padding-top: 10px;
      background: black;
    }
    
    .outer {
      width: 200px;
      height: 200px;
      margin: auto;
      background: white;
      border-radius: 10px;
      overflow: hidden;
    }
    
    .middle {
      width: 100%;
      height: 50%;
      background: yellow;
      border-radius: 10px 10px 0 0;
    }
    
    .inner {
      width: 100%;
      height: 100%;
      background: black;
      border-radius: 10px 10px 0 0;
      position: relative;
    }
    
    .inner:after {
      content: "";
      position: absolute;
      background-color: inherit;
      top: 0px;
      left: 0px;
      right: 0px;
      bottom: 0px;
    }
    .some-text {
      margin-top: 20px;
      padding: 0 10px;
    }
    <div class="dark-bg">
      <div class="outer">
        <div class="middle">
          <div class="inner">
          </div>
        </div>
        <div class='some-text'>
          This is text. There can be quite a decent amount of text here, so the outer div needs to have overflow: hidden. Like this, you see?
        </div>
      </div>
    </div>

    【讨论】:

    • box-shadow 解决方案对我不起作用,因为我不能依赖背景为黑色。 transform: translateZ(0px) 方法很有趣,谢谢。
    猜你喜欢
    • 2022-11-12
    • 2021-10-29
    • 1970-01-01
    • 2014-10-23
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多