2016-11-27 11 views
1

위에 블록을 중심이되는 HTML 내가 가진 :수직으로 다른 블록의 하단 라인

enter image description here

이것은 내가 뭘하려 :

<div class="image">Image</div> 
<div class="legend"> 
    Legend<br/> 
    width variable height<br/> 
    the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image 
</div> 
<div class="following">The following text should follow immediatly the legend,regardless of the height of the legend or the image</div> 

이 내가 원하는 결과입니다

.image { 
    height:100px; 
    background-color:red; 
} 
.legend { 
    transform:translateY(-50%); 
    background-color:blue; 
    width:300px; 
    margin:0 auto 
} 
.following { 
    background-color:yellow; 
    margin-top:-45px; 
} 

이것은 내가 얻은 결과입니다. This is not what I want

문제는 다음과 같습니다. 전설과 다음 텍스트 사이에이 여백을 두지 않아도됩니다.

The whole attempt codepen is here

질문 : 모든 용액 JS 않고도 원하는 결과를 얻을?

는 (기록을 위해 : this is a solution with JS)

답변

1

당신은 요소의 높이를 알고 계십니까? 정확하게 50 %가 필요합니까? 여기

는 고정 50 픽셀 음성 위쪽 여백과 예입니다

.image { 
 
    height:100px; 
 
    background-color:red; 
 
} 
 
.legend { 
 
    background-color:blue; 
 
    width:300px; 
 
    margin:-50px auto 0; 
 
} 
 
.following { 
 
    background-color:yellow; 
 
}
<div class="image">Image</div> 
 
<div class="legend"> 
 
    Legend<br/> 
 
    width variable height<br/> 
 
    the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image 
 
</div> 
 
<div class="following">The following text should follow immediatly the legend, regardless of the height of the legend or the image</div>

하지만이 좋은 솔루션입니다 (정확하게 당신이 찾고있는 아마되지 않습니다) 또 다른 옵션 :)

.image { 
 
    height:100px; 
 
    background-color:red; 
 
} 
 
.legend { 
 
    background-color:blue; 
 
    width:300px; 
 
    margin:0 auto; 
 
    transform: translateY(-50%) translateX(-50%); 
 
    position: absolute; 
 
    left: 50%; 
 
} 
 
.legend:after { 
 
    content: attr(following); 
 
    display: block; 
 
    width: 100vw; 
 
    clear: both; 
 
    position: absolute; 
 
    background-color:yellow; 
 
    height: auto; 
 
    transform: translateX(-50%); 
 
    left: 50%; 
 
    
 
} 
 
.following { 
 
}
<div class="image">Image</div> 
 
<div class="legend" following="The following text should follow immediatly the legend, regardless of the height of the legend or the image"> 
 
    Legend<br/> 
 
    width variable height<br/> 
 
    the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image 
 
</div>

+0

가 아니, 어느 요소의 높이를 모르는 데 도움이 – sylvain

+0

그리고 정확히 50 % 높이가이다의 "가서"해당 요소를해야합니까? – Dekel

+0

예 정확히 중간에 – sylvain

0

당신은 간단한 위치로이 작업을 수행 범례 포장 및 사업부와 다음 텍스트 및 위치 할 수 있습니다 : 상대적 위치로 설정할 수 있습니다 다음 : 절대

확인이 조각

.image { 
 
    height: 100px; 
 
    background-color: red; 
 
} 
 
.legend { 
 
    transform: translateY(-50%); 
 
    background-color: blue; 
 
    width: 300px; 
 
    margin: 0 auto; 
 
} 
 
.following { 
 
    background-color: yellow; 
 
    position: absolute; 
 
    top: 50%; 
 
    width: 100%; 
 
} 
 
.container { 
 
    position: relative; 
 
    left: 0; 
 
}
<div class="image">Image</div> 
 
<div class="container"> 
 
    <div class="legend"> 
 
    Legend 
 
    <br/>width variable height 
 
    <br/>the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image 
 
    </div> 
 
    <div class="following">The following text should follow immediatly the legend,regardless of the height of the legend or the image</div> 
 
</div>

인 flexbox 또 다른 솔루션

.image { 
 
    height:100px; 
 
    background-color:red; 
 
} 
 
.item{ 
 
    transform:translateY(-50%); 
 
} 
 
.center { 
 
    background-color:blue; 
 
    width:300px; 
 
    margin:0 auto; 
 

 
} 
 

 
.Aligner { 
 
    display: flex; 
 
    flex-direction:column; 
 
} 
 

 

 
.Aligner-item--top { 
 
    width:100%; 
 
    background:red; 
 
} 
 

 
.following { 
 

 
    width:100%; 
 
    background-color:yellow; 
 
    
 
}
<div class="Aligner"> 
 

 
    <div class=" Aligner-item Aligner-item--top image">Image</div> 
 
    <div class="item"> 
 
    <div class="Aligner-item center">Legend<br/> 
 
    width variable height<br/> 
 
    the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image</div> 
 
    <div class="Aligner-item Aligner-item--bottom following">The following text should follow immediatly the legend,regardless of the height of the legend or the image</div> 
 
    </div> 
 
</div>
,132 10

희망이

+0

고마워요.하지만'.following'은 정상적인 흐름에 있어야합니다. 그래서 내용을 추가 할 수 있습니다. (내 설명에 분명하지 않아서 죄송합니다. 나는 그것을 고칠 것입니다.) http : // codepen을보십시오.io/sylvainbannier/pen/VmzEYe – sylvain

+0

위치 직후의 정적 요소의 경우 : 절대적으로 이전에 절대 위치 된 요소의 높이로 margin-top을 설정해야합니다.이 http://codepen.io/sahithiK/를 확인하십시오. 펜/woqYrm – Geeky

+0

절대적으로 http://stackoverflow.com/questions/6588309/static-elements-after-position 절대적으로 후에 정적 요소 포지셔닝 – Geeky