【问题标题】:Background-color behind a border-radius边框半径后面的背景颜色
【发布时间】:2016-01-05 17:35:32
【问题描述】:

我想知道,是否可以在边框半径后面设置背景颜色?

我想实现这个效果:

我使用这个 HTML:

<div class="content-header" tabindex="-1">
    <span class="html">TITLE</span>
</div>
<div class="content-txt-v2" tabindex="-1">
  <p>
  Content
  </p>        
</div>

我使用这个 CSS:

.content-header {
    height: 18px;
    line-height: 18px;
    background-color: #4C741B;
    margin-top: 20px;
    float: left;
    -moz-border-radius: 10px 10px 0px 0px;
    -webkit-border-radius: 10px 10px 0px 0px;
    -khtml-border-radius: 10px 10px 0px 0px;
    border-radius: 10px 10px 0px 0px;    
    padding: 0px 14px;
    font-weight: bold;
    font-size: 14px;
    color:#fff;
}
.content-txt-v2 {
    clear: both;
    width: 669px;
    padding: 0 10px;
    margin: 0px auto;
      border: 2px solid #E9EAE3;
    -moz-border-radius: 20px 10px 10px 10px;
    -webkit-border-radius: 20px 10px 10px 10px;
    -khtml-border-radius: 20px 10px 10px 10px;
    border-radius: 20px 10px 10px 10px;
    float:left;
    background-color:red;
}

我在JSFIDDLE中添加了上面的代码

我尝试设置我的 content-txt-v2 div 的背景颜色,但没有成功。谁能指导我正确的方向?只有两个div有可能吗?还是应该添加更多对象?

【问题讨论】:

    标签: html css


    【解决方案1】:

    你为什么不做这样的事情?它可以为您提供所需的输出而不会让人头疼!

    * {
      box-sizing: border-box;
    }
    .content-header {
      height: 18px;
      line-height: 18px;
      background-color: #4C741B;
      margin-top: 20px;
      float: left;
      -moz-border-radius: 10px 10px 0px 0px;
      -webkit-border-radius: 10px 10px 0px 0px;
      -khtml-border-radius: 10px 10px 0px 0px;
      border-radius: 10px 10px 0px 0px;
      padding: 0px 14px;
      font-weight: bold;
      font-size: 14px;
      color: #fff;
      z-index: 10;
      /* z-index same as red bar*/
      position: relative;
    }
    .content-header:after {
      position: absolute;
      top: 100%;
      left: 0;
      content: '';
      background: #4C741B;
      width: 18px;
      height: 18px;
      display: block;
      z-index: 5;
      /* z-index less than red bar*/
    }
    .content-txt-v2 {
      clear: both;
      width: 669px;
      padding: 0 10px;
      margin: 0px auto;
      border: 2px solid #E9EAE3;
      -moz-border-radius: 20px 10px 10px 10px;
      -webkit-border-radius: 20px 10px 10px 10px;
      -khtml-border-radius: 20px 10px 10px 10px;
      border-radius: 20px 10px 10px 10px;
      float: left;
      background-color: red;
      position: relative;
      /* for giving z-index */
      z-index: 10;
      /* z-index equal to title block */
    }
    <div class="content-header" tabindex="-1">
      <span class="html">TITLE</span>
    </div>
    <div class="content-txt-v2" tabindex="-1">
      <p>
        Content
      </p>
    </div>

    【讨论】:

    • 谢谢!您的解决方案也是一个非常好的解决方案。最后,我使用了您的解决方案以避免使用负边距。谢谢!
    • 是的.. 这就是优势。我打算分享这两个解决方案,一个这个和另一个与@Jonas共享的相同,但由于负边距和高度/宽度问题我放弃了它
    【解决方案2】:

    这很难做到,但你可以伪造它:https://jsfiddle.net/85c8ss94/1/

    .content-header {
        height: 30px;
        line-height: 18px;
        background-color: #4C741B;
        margin-top: 20px;
        float: left;
        -moz-border-radius: 10px 10px 0px 0px;
        -webkit-border-radius: 10px 10px 0px 0px;
        -khtml-border-radius: 10px 10px 0px 0px;
        border-radius: 10px 10px 0px 0px;    
        padding: 0px 14px;
        font-weight: bold;
        font-size: 14px;
        color:#fff;
        margin-bottom: -12px;
    }
    .content-txt-v2 {
        clear: both;
        width: 669px;
        padding: 0 10px;
        margin: 0px auto;
        border: 2px solid #E9EAE3;
        -moz-border-radius: 20px 10px 10px 10px;
        -webkit-border-radius: 20px 10px 10px 10px;
        -khtml-border-radius: 20px 10px 10px 10px;
        border-radius: 20px 10px 10px 10px;
        float:left;
        background-color:red;
    }
    

    【讨论】:

    • 谢谢!确实很难做到;)无论如何,您的解决方案对我有用!谢谢!