【问题标题】:Create a horizontal rule with text in the middle? [duplicate]创建一个中间有文字的水平线? [复制]
【发布时间】:2013-09-02 13:29:11
【问题描述】:

我想在我的页面上有一个如下所示的分隔线:

最好的方法是什么?

【问题讨论】:

    标签: html css


    【解决方案1】:

    我们可以在没有图像或屏蔽线的情况下做到这一点like so

    HTML

    <div class="rule">
        <div class="line"><div></div></div>
        <div class="words">words are cool</div>
        <div class="line"><div></div></div>
    </div>
    

    CSS

    .rule {
        display: table;
    }
    
    .rule>div {
        display: table-cell;
        white-space:nowrap;
    }
    
    .line>div {
        border-bottom: 1px solid silver;
        height: 1px;
    }
    
    .words {
        padding: 0 5px;
    }
    
    .line {
        width: 50%;
        vertical-align: middle;
    }
    

    【讨论】:

      【解决方案2】:

      演示:http://jsfiddle.net/8zve4/

      我不喜欢额外的标记,但这应该可以。

      CSS:

      .hline {
        border: 1px solid #EEE;
        color: #666;
        font-family: helvetica;
        font-weight: bold;
        font-variant: small-caps;
        letter-spacing: .1em;
        line-height: 0px;
        text-align: center;
        text-transform: uppercase;
      }
      
      .hline > span {
        background-color: #FFF;
          padding: 0px 1em;
      }
      

      HTML:

      <div class="hline"><span>Our latest work</span></div>
      

      【讨论】:

        【解决方案3】:

        演示:http://jsfiddle.net/5tqE5/1/

        这使用了旧版浏览器不支持的attr()。它可以被一个额外的元素替换。

        <div class="lines" data-text="Some Text Goes Here"></div>
        
        .lines {
            position: relative;
            font-size: 20px;
            font-family: sans-serif;
            margin: 0 auto;
            border-top: 1px solid silver;
            margin-top: 20px;
        }
        
        .lines:before{
            content: attr(data-text);
            background-color: #fff;
            position: absolute;
            text-align: center;
            left: 50%;
            width: 220px;
            margin-left: -110px;
            padding: 10px;
            top: -20px;
        }
        

        【讨论】:

        • 非常喜欢这个解决方案。当 HTML5 和 CSS3 完全集成时,可能会在几年内使用它。
        【解决方案4】:

        html

        <h3><span>My latest work</span></h3>
        

        css

        h3 {
        position:relative;
        text-align:center;}
        
        h3 span {
            display:inline-block;
            padding:0 10px;
            background:#fff;
        }
        h3:before {
            content:"";
            display:block;
            position:absolute;
            z-index:-1;
            left:0;
            right:0;
            top:50%;
            height:1px;
            background:#ccc;
        }
        

        【讨论】:

        • 成功了……谢谢!
        【解决方案5】:

        不知道“最好的”是什么——你没有给出评估它的条件。最小、最快、最兼容等等。

        Anyhoo,我刚刚从您的图像中截取了 1 像素宽的切片并保存了它。然后我将它用作 div 的背景图像。

        CSS:

        #myDiv
        {
            background: url(horizline1x41px.png) repeat;
            text-align: center;
            line-height: 41px;
        }
        #myDiv span
        {
            padding-left: 16px;
            padding-right: 16px;
            background: white;
            font-weight: bold;
            font-size: 1.5em;
        }
        

        HTML:

        <div id='myDiv'><span>OUR LATEST WORK</span></div>
        

        【讨论】:

          猜你喜欢
          • 2020-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-07
          • 2021-04-08
          • 2019-09-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多