2016-11-08 70 views
3

我正在嘗試創建一個將在主div後面有一行的橫幅/標題。我想如圖所示線和絲毫不差,以在中間(垂直)的文本:將hr與文本垂直排列在div後面

enter image description here

問題是,如果我改變瀏覽器的大小,hr和文本沒有對齊(垂直)。下面是第一個版本:

.section { 
 
\t clear: both; 
 
\t padding: 0px; 
 
\t margin: 0px; 
 
} 
 

 
.col { 
 
    text-align:center; 
 
\t display: block; 
 
\t float:left; 
 
\t margin: 1% 0 1% 0%; 
 
} 
 
.col:first-child { margin-left: 0; } 
 

 
.group:before, 
 
.group:after { content:""; display:table; } 
 
.group:after { clear:both;} 
 
.group { zoom:1; /* For IE 6/7 */ } 
 

 
.span_3_of_3 { width: 100%; } 
 
.span_2_of_3 { width: 66.66%; } 
 
.span_1_of_3 { width: 33.33%; } 
 

 
@media only screen and (max-width: 480px) { 
 
\t .col { margin: 1% 0 1% 0%; } 
 
\t .span_3_of_3, .span_2_of_3, .span_1_of_3 { width: 100%; } 
 
} 
 

 
#middle 
 
{ 
 
    background:#CCC; 
 
    color:#FC3699; 
 
    height:100px; 
 
    margin-top:0; 
 
    line-height:100px; 
 
} 
 

 
hr { 
 
    margin-top:40px; 
 
    border:2px solid #FC3699; 
 
}
<div class="section group"> 
 
\t <div class="col span_1_of_3"> 
 
\t \t <div class="topsection"> 
 
\t \t \t <div class="header"><hr /></div> 
 
\t \t </div> 
 
\t </div> 
 
\t <div id="middle" class="col span_1_of_3"> 
 
\t \t aaaaaaa 
 
\t </div> 
 
\t <div class="col span_1_of_3"> 
 
\t \t <div class="topsection"> 
 
\t \t \t <div class="header"><hr /></div> 
 
\t \t </div> 
 
\t </div> 
 
</div>

這裏是第二個版本。而不是有3列,我可以使用類似下面的代碼片段。問題是,在這條線:

高度:100像素; line-height:100px;

我想要的高度爲100%。但行高不能

.main { 
 
    height:100%; 
 
    min-height:100px; 
 
    display: block; 
 
    text-align: center; 
 
    overflow: hidden; 
 
    white-space: nowrap; 
 
} 
 

 
.main > span { 
 
    width:200px; 
 
    height:100px; 
 
    line-height: 100px; 
 
    background:#F1F1F1; 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 
\t 
 
.main > span:before, 
 
.main > span:after { 
 
    content: ""; 
 
    position: absolute; 
 
    top: 50%; 
 
    width: 9999px; 
 
    height: 3px; 
 
    background: #FC3699; 
 
} 
 

 
.main > span:before { 
 
    right: 100%; 
 
    margin-right: 15px; 
 
} 
 

 
.main > span:after { 
 
    left: 100%; 
 
    margin-left: 15px; 
 
}
<div class="main"> 
 
    <span style="vertical-align: middle;">Text</span> 
 
</div>

這裏是第三和最好的版本,我只需要text-aling:center;文本。但是,即使我在CSS中添加它,它不會是有原因的工作:

.main { 
 
    height:100%; 
 
    min-height:100px; 
 
    display: block; 
 
    text-align: center; 
 
    overflow: hidden; 
 
    white-space: nowrap; 
 
} 
 

 
.main > span { 
 
    width:200px; 
 
    height:100px; 
 
    line-height: 100px; 
 
    background:#F1F1F1; 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 
\t 
 
.main > span:before, 
 
.main > span:after { 
 
    content: ""; 
 
    position: absolute; 
 
    top: 50%; 
 
    width: 9999px; 
 
    height: 3px; 
 
    background: #FC3699; 
 
} 
 

 
.main > span:before { 
 
    right: 100%; 
 
} 
 

 
.main > span:after { 
 
    left: 100%; 
 
} 
 

 
#child { 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
}
<div class="main"> 
 
    <span><div id="child">Text</div></span> 
 
</div>

+0

有很多方法可以做到這一點。你在第二個版本之前完成了這個:before&:之後。試試這個#child {display:table-cell; vertical-align:middle;寬度:繼承;}。 – AmitV

+0

@ Amit1992我對CSS有一些限制。第一個答案正常工作:D謝謝大家 –

回答

3

如果flexbox是一個選項,這很容易 - 你只需要給

display: flex; 
    justify-content:center; 
    align-items:center; 

,你可以做掉所有的定位寬度計算 - 見下面的演示:

.main { 
 
    height: 100%; 
 
    min-height: 100px; 
 
    display: flex; 
 
    justify-content:center; 
 
    align-items:center; 
 
    width: 100%; 
 
    text-align: center; 
 
    white-space: nowrap; 
 
} 
 
.main > span { 
 
    width: 200px; 
 
    height: 100px; 
 
    background: #F1F1F1; 
 
    display: inline-flex; 
 
    justify-content:center; 
 
    align-items:center; 
 
} 
 
.main:before, 
 
.main:after { 
 
    content: ""; 
 
    height: 3px; 
 
    background: #FC3699; 
 
    flex:1; 
 
}
<div class="main"> 
 
    <span>Text</span> 
 
</div>

+1

準確無誤。我會這樣做(https://jsfiddle.net/xgj78k73/) –

1

問題是,你有.col1%保證金和hr固定的位置。

我建議使用:before的包裝,並將其定位垂直中間的包裝。這樣,即使包裝高度改變時,行會在相對相同的位置,以包裝(jQuery的僅適用於演示):

$(document).ready(function() { 
 
    setInterval(function() { 
 
    $('.col3').animate(
 
     {height: 300}, 
 
     2000, 
 
     function() { 
 
     $('.col3').animate({height: 120}, 2000); 
 
     }); 
 
    }, 5000); 
 
})
* { 
 
    box-sizing: border-box; 
 
} 
 
.col3 { 
 
    width: 33.33333%; 
 
    height: 120px; 
 
    display: inline-block; 
 
    background-color: grey; 
 
    color: #fc3699; 
 
    z-index: 10; 
 
    position: relative; 
 
    margin: 1% 0; 
 
} 
 
.col3:before { 
 
    content: ''; 
 
    height: 100%; 
 
    vertical-align: middle; 
 
    display: inline-block; 
 
} 
 
.wrapper { 
 
    position: relative; 
 
    text-align: center; 
 
} 
 
.wrapper:before { 
 
    content: ''; 
 
    display: block; 
 
    width: 100%; 
 
    height: 4px; 
 
    background-color: #fc3699; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 50%; 
 
    margin-top: -2px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrapper"> 
 
    <div class="col3"> 
 
    aaaaa 
 
    </div> 
 
</div>

1

這是我如何做到這一點,有一個圖標:

<div class="propos_underline marginbottom"> 
      <div class="icon icon-cubes"></div> 
      <div class="clear"></div> 
     </div> 

CSS:

.propos_underline{ 
    height: 1px; 
    width: 60%; 
    margin: 0 auto; 
    background-color: #d4d8da; 
    position: relative; 
} 

.propos_underline .icon{ 
    position: absolute; 
    width: 70px; 
    height: 40px; 
    background: #ffffff; 
    font-size: 30px; 
    margin: auto; 
    top: 0; 
    bottom: 0; 
    left: 0; 
    right: 0; 
    color: #d4d8da; 
} 
.icon-cubes:before { 
    content: "\e993"; 
} 

結果: enter image description here