2015-04-02 81 views
0

我实现了一个内嵌头(我把它命名为是)类似如下:
Inline Header style
与HTML象下面这样:制作内嵌标题样式而不跨度

<h1 class="title-inline-left"><span>Inline Title</span></h1> 

,并用CSS(LESS)像如下:

.title-inline-left{ 
    position: relative; 

    &:after{ 
     display: block; 
     content: ""; 
     position: absolute; 
     right: 0; 
     top: 50%; 
     background-color: #333; 
     width: 100%; 
     height: 3px; 
     z-index: -1; 
    } 
    span{ 
     background-color: #fff; 
     z-index: 1; 
     padding-right: 10px; 
    } 
} 

我怎样才能做到这一点没有一个<span>? (如果用更少的代码更好)

回答

3

你可以这样做通过使用HTML数据属性,看下面的代码:

HTML

<div class="title" data-content="Inline Title"></div> 

CSS

.title { 
    height: 4px; 
    background: #000; 
    position: relative; 
} 

.title:after { 
    content: attr(data-content); 
    background: #fff; 
    font-size: 18px; 
    font-family: Arial; 
    font-weight: bold; 
    position: absolute; 
    top: -8px; 
    padding: 0 7px; 
} 

直播demo

0

CSS

.title-inline-left{ 
    position: relative; 
} 
    .title-inline-left:after{ 
     display: block; 
     content: ""; 
     position: absolute; 
     right: 0; 
     top: 50%; 
     background-color: #333; 
     width: 100%; 
     height: 3px; 
     z-index: -1; 
    } 
    .title-inline-left:before { 
    display: block; 
    content: "Inline Title"; 
    width: 19%; 
    background-color: white; 
} 

HTML

<h1 class="title-inline-left"></h1> 

DEMO

+0

'内容: “内联标题”;'需要一个动态的解决方案。这是硬编码。 :( – 2015-04-02 05:56:53

+0

@MayeenulIslam试试这个,但这里也必须指定'.title -inline-left:之前的宽度http://jsfiddle.net/4aa532vL/16/ – 2015-04-02 06:18:08