2016-11-08 3 views
3

주 div 뒤에 한 줄의 배너/작은 글을 작성하려고합니다. 나는 라인 (수직) 중간에있는 획의 텍스트와 같이 원하는 :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 개의 열이있는 대신 아래의 스 니펫과 같은 것을 사용할 수 있습니다. 문제는 다음 줄에 있습니다.

높이 : 100px; 줄 높이 : 100px;

높이를 100 %로하고 싶습니다. 그러나 라인 높이는하지 수 : 만 text-aling:center; 텍스트에이 곳

.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>

여기 세 번째와 최고의 버전입니다. 내가 CSS에 추가하더라도, 그것은 이유가 작동하지 않습니다 : flexbox이 옵션은

.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

이렇게하려면 여러 가지 방법이 있습니다. 당신은 이것을 다음과 같이했습니다 : & : after after in second version. 이 #child {디스플레이 : 표 셀; 세로 맞춤 : 가운데; 너비 : 상속;}. – AmitV

+0

@ Amit1992 CSS 관련 몇 가지 제한 사항이 있습니다. 첫 번째 대답은 잘 작동합니다. D Thank you all –

답변

3

경우이 쉽습니다 - 그냥

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

문제 16,는 1%.col에 대한 마진과 hr에 대한 고정 된 위치를 가지고있다.

래퍼에 :before을 사용하고 세로로 가운데에 래퍼를 배치하는 것이 좋습니다.

$(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

이 내가 아이콘으로, 그것을 할 방법은 다음과 같습니다 래퍼 높이가 변경 그런 식으로해도 라인은 래퍼에 같은 장소 상대 (jQuery를 단지 데모의 경우)에있을 것입니다 :

<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

관련 문제