2016-04-24 77 views
2

我有一个div文本,我添加了边框底部,但底部线宽与文本宽度相等,有没有什么办法可以让底线比文本小得多?我想是这样的形象:文本边框底部问题

enter image description here

我的代码:

.title-line { 
 

 
border-bottom: 1px solid rgba(0, 0, 0, 0.2); 
 
    font-family: lato; 
 
    font-size: 18px; 
 
    font-weight: normal; 
 
    padding: 0 0 1em; 
 
    text-align: center; 
 
}
<dt class="title-line">My Text Example</dt>

+0

抱歉,但我不明白 – Robert

回答

4

是的,你可以使用伪元素(如::after),而不是border-bottom

.title-line { 
 
    position: relative; /* important for absolute child to work */ 
 
    font-family: lato; 
 
    font-size: 18px; 
 
    font-weight: normal; 
 
    padding: 0 0 1em; 
 
    text-align: center; 
 
} 
 

 

 
.title-line::after { 
 
    content: ''; /* required to display pseudo elements */ 
 
    height: 1px; /* this works like a border-width */ 
 
    width: 10%; /* you can use a percentage of parent or fixed px value */ 
 
    background: #CCC; /* the color of border */ 
 
    position: absolute; 
 
    bottom: 0; /* position it at the bottom of parent */ 
 
    margin: 0 auto; left: 0; right: 0; /* horizontal centering */ 
 
}
<dt class="title-line">My Text Example</dt>

+0

谢谢你,但我有一个问题,可以有更多的文本,我不能使用的位置是:绝对的:之后,有没有任何方式? – Robert

+1

@RobertD Aziz的CSS应该适用于任何带有'title-line'类的文本。为什么你不能使用绝对定位:after? –

+1

确实,对不起,这是我的错误,因为我补充说:在顶部之后:10%。阿齐兹答案是完美的,非常感谢哟阿齐兹 – Robert