【问题标题】:CSS style heading - background must cover only text with a bottom borderCSS 样式标题 - 背景必须只覆盖带有底部边框的文本
【发布时间】:2015-06-12 03:02:13
【问题描述】:

我正在尝试像这样设置标题的样式:

但我需要一些帮助,因为我无法正确理解。

我打算使用渐变,但我必须只有标题的文本(带有一些填充)具有背景色,并且渐变会在表格单元格中的特定点改变颜色。标题可以是不同的词——一些长一些,一些短一些——所以背景颜色不能停在一个特定的点。我还需要为单元格的宽度加下划线(边框底部)。

我不确定我的 HTML 是否需要更改或我的 CSS 是否需要更改或两者兼而有之,或者是否可以这样做。

我的 HTML 代码:

<div class="wrapper">
    <div class="row">
        <div class="heading">Heading</div>
    </div>
    <div class="row">
        <div class="stuff">Just some stuff.</div>
    </div>
</div>

我的 CSS 代码:

.wrapper {
    border-spacing: 0px 0px;
    display: table;
    width: 100%;
}

.row {
    display: table-row;
}
.heading {
    border-bottom: 2px solid red;
    background-color: lime;
    color: blue;
    direction: ltr;
    display: table-cell;
    font-weight: bold;
    min-height: 25px;
    overflow: hidden;
    padding: 2px;
    padding-left: 30px;
    padding-right: 30px;
    text-align: left;
    text-transform: uppercase;
    vertical-align: top;
}

.stuff {
    width: 100%;
    display: table-cell;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    您应该调整标题的宽度,使其仅占据其内容的宽度。一个常见的技巧是简单地使用display: inline-block。我更改了您的一些stylesmarkup 以创建下边框效果。你的 wrapperstuff 类是多余的,所以我删除了它们。

    JSFiddlehttp://jsfiddle.net/mkmeLzjL/11/

    HTML

    <div class="row">
        <div class="heading">Profile</div>
    </div>
    <br>
    <div>
        <div>Just some stuff.</div>
    </div>
    

    CSS

    @import url(http://fonts.googleapis.com/css?family=Open+Sans);
    
    * {
        font-family: Open Sans;
    }
    
    .row {
        border-bottom: 2px solid black;
    }
    
    .heading {
        background-color: black;
        color: white;
        display: inline-block;
        font-weight: bold;
        padding: 7px 30px 7px 15px;
        font-size: 20px;
        text-transform: uppercase;
    }
    

    【讨论】:

    • 谢谢 - 我知道将边框放在行中是我没有弄清楚的技巧。
    【解决方案2】:

    这是一个使用伪元素的解决方案:http://jsfiddle.net/npeqzm32/

    HTML:

    <div class="wrapper">
        <div class="row">
            <h1>Heading</h1>
        </div>
        <div class="row">
            <div class="stuff">Just some stuff.</div>
        </div>
    </div>
    

    CSS:

    * {
        margin: 0;
        padding: 0;
        border: 0;
    }
    
    body {
        padding: 10px;
    }
    
    .row {
        position: relative;
    }
    
    .row h1 {
        font: bold 20px/2 Sans-Serif;
        background-color: #222;
        color: white;
        display: inline-block;
        padding: 0 10px;
        text-transform: uppercase;
        letter-spacing: 1px;
    }
    
    .row h1:before {
        content: "";
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 3px;
        background-color: #222;
    }
    

    【讨论】:

      【解决方案3】:

      我做了一些更改,以输出与您想要的有点相似。

      DEMO HERE

      HTML

      <div class="wrapper">
          <div class="row">
              <div class="heading">Heading</div>
          </div>  
      </div>
      

      CSS

      .row {
        border-bottom:4px solid black;
      }
      
      .row .heading 
      {
        display:inline-block;
        background-color:#111;
        color:#fff;
        font-size:24px;
        font-family:calibri;
        font-weight:bold;
        padding:10px 20px;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-09
        • 1970-01-01
        • 2018-05-12
        • 2015-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多