【问题标题】:Align underline under the texts in a horizontal div在水平 div 中的文本下对齐下划线
【发布时间】:2013-09-10 09:52:25
【问题描述】:

在这个Question 中,我关注了 w3d 用户的回答,但没有得到顶部 div 下的 div。

CSS

div.demo {
    display:table;
    width:100%;
}
div.demo span {
    display:table-cell;
    text-align:center;
}
#under {
    width:100px;
    height:2px;
    background-color:blue;
}

HTML

<div class="demo">
    <span>Span 1</span>
    <span>Span 2</span>
    <span>Span 3</span>
</div>
<div class="demo">
    <span><div id='under'></div></span>
    <span><div id='under'></div></span>
    <span><div id='under'></div></span>
</div>

JSFiddle

【问题讨论】:

    标签: html css


    【解决方案1】:

    在id下试试这个css

    #under {
        width:100px;
        height:2px;
        background-color:blue;
        margin:0px auto; /*ADDED*/
    }
    

    注意:使用 class 而不是 ID,因为您在单页中多次使用 under id。

    这是一个有效的 HTML

    <div class="demo">
        <div>Span 1</div>
        <div>Span 2</div>
        <div>Span 3</div>
    </div>
    <div class="demo">
        <div><span class="under"></span></div>
        <div><span class="under"></span></div>
        <div><span class="under"></span></div>
    </div>
    

    这里是 CSS

    div.demo {
        display:table;
        width:100%;
    }
    div.demo div {
        display:table-cell;
        text-align:center;
    }
    .under {
        width:100px;
        height:2px;
        background-color:blue;
        margin:0px auto;
        display:block;
    }
    

    DEMO HERE

    【讨论】:

    • 好的,我会使用类。但是你怎么想增加保证金?我没有在上面的 div 中添加边距?
    • 您将文本对齐中心设置为上 div,因此文本对齐跨度中心。和注意: div 不允许作为 span 标签的子级。所以改变你的html结构
    • 你能帮我处理新的 css(因为 div 不允许作为 span 的孩子)
    • 答案下方添加了有效的 html 和 css
    【解决方案2】:

    我知道您已经接受了答案,但也许可以考虑使用border-spacing 和border-bottom?

    JSFiddle

    CSS

    div.demo {
        border-spacing: 75px 0;
        display: table;
        width: 100%;
    }
    
    div.demo span {
        border-bottom: 2px solid blue;
        display: table-cell;
        text-align: center;
    }
    

    HTML

    <div class="demo">
        <span>Span 1</span>
        <span>Span 2</span>
        <span>Span 3</span>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 2013-04-16
      • 2017-11-05
      • 1970-01-01
      • 2016-03-24
      相关资源
      最近更新 更多