【问题标题】:how to make line cross center text without span [duplicate]如何使没有跨度的行交叉中心文本[重复]
【发布时间】:2015-01-17 21:45:37
【问题描述】:

我想在文本的背面画一条居中的线。我的代码适用于谷歌浏览器,但不适用于 safari 和 IE。有很多关于这方面的教程,但它们都在文本周围有一个跨度。反正有没有跨度画线? 以下是我的代码:

<h1>Header</h1>

h1{overflow: hidden;text-align: center;}

h1:before, h1:after {position: absolute;
top: 51%;
overflow: hidden;
width: 50%;
height: 2px;
content: '\a0';
background-color: #e6ebfa;}

h1:before {margin-left: -52%; text-align: right;}
h1:after {margin-left: 2%;}

【问题讨论】:

    标签: css


    【解决方案1】:

    更新

    解决方案 1:

    演示:http://jsfiddle.net/1cy803ar/36/

    CSS:

    .title {
        display:block;
        margin:0;
        padding:0;
        text-align:center
    }
    .title h1 {
        overflow: hidden;
        text-align: center;
        position:relative;
        display:block;
        margin:0;
        padding:0;
    }
    .title h1:before, .title h1:after {
        content:'\a0';
        display:inline-block;
        width:100px;
        height:1px;
        background-color:red;
        line-height:0;
        margin:0 5px;
    }
    

    HTML:

    <div class="title">
         <h1>Title Header</h1>
    </div>
    

    解决方案 2:

    是的,演示:http://jsfiddle.net/1cy803ar/9/

    CSS:

    h1 {
        overflow: hidden;
        text-align: center;
        position:relative; //You missed this.
    }
    h1:before, h1:after {
        position: absolute;
        top: 51%;
        overflow: hidden;
        width: 40%; //modified
        height: 2px;
        content:'\a0';
        background-color: #e6ebfa;
    }
    h1:before {
        left:0;
    }
    h1:after {
        right: 0;
    }
    

    【讨论】:

    • 谢谢你,KK。但它只在 IE 和 Safari 上有一条正确的线。
    • 我忘了说,我的代码在 firefox chrome 上工作,但在 ie 和 safari 上不工作。在 ie 和 safari 上,只有右行。
    • 您需要分别为 :before 和 :after 伪元素指定 left 和 right 值。你也可以参考这个stackoverflow.com/questions/23584120/…
    • 您好 KK:再次感谢。我试过左右。但问题是标题不是固定宽度,我可能会在其他更长的标题上使用它。像这个:jsfiddle.net/lukelee/1cy803ar/27我在想有没有溢出隐藏的前后?
    • @KK 是的,现在可以使用 jsfiddle.net/1cy803ar/39 谢谢老兄
    猜你喜欢
    • 2021-02-10
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2018-03-06
    相关资源
    最近更新 更多