2015-02-10 124 views
7

我使用僞元素:before:after在標題之前和之後繪製一條線。它的工作有一個形象:在僞元素之前和之後使用來製作一條線

.mydiv::before { 
content: url(img/line.png);} 
.mydiv::after { 
content: url(img/line.png);} 

下面是結果:

you can see befor and after the 1px white horizontale line

但是,我想行擴大和前後的標題在整個DIV填補,這樣:

The line reach reach the both sides of the div

是否有指定爲它拉伸圖像比例的方法嗎?我嘗試這一點,但它不工作:

.mydiv img { 
    width: 100%; 
    height: auto; 
} 
+4

爲什麼要使用的圖像?只要做'邊界'。 – Slime 2015-02-10 14:47:18

+0

像粘液說...爲什麼使用圖像...檢查http://stackoverflow.com/questions/15557627/css-title-with-horizo​​ntal-line-on-e-任何側 – LinkinTED 2015-02-10 14:58:59

回答

8

你並不需要同時:before:after,無論是2的就足夠了,當你一直在說,你不需要的圖像。請參閱下面的方法。

#header { 
 
    width: 100%; 
 
    height: 50px; 
 
    margin: 50px 0; 
 
    text-align: center; 
 
    font-size: 28px; 
 
    position: relative; 
 
    background-color: #57585C; 
 
} 
 

 
#header:after { 
 
    content: ''; 
 
    width: 100%; 
 
    border-bottom: solid 1px #fff; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 50%; 
 
    z-index: 1; 
 
} 
 

 
h3 { 
 
    background-color: #57585C; /* Same as the parents Background */ 
 
    width: auto; 
 
    display: inline-block; 
 
    z-index: 3; 
 
    padding: 0 20px 0 20px; 
 
    color: white; 
 
    position: relative; 
 
    font-family: calibri; 
 
    font-weight: lighter; 
 
    margin: 0; 
 
}
<div id="header"> 
 
    <h3>Last Projects</h3> 
 
</div>

+1

它工作正常。我喜歡你使用背景顏色隱藏線條的方式。謝謝 – Guillaume 2015-02-10 15:19:23

+0

@Guillaume謝謝。很高興我能幫上忙。 – 2015-02-10 15:22:22

相關問題