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%にする必要がありますか?ここで

は、固定は50px-負の上マージンを持つ例です。

.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

あなたは伝説とのdivで次のテキストをラップし、シンプルなポジショニングでこれを行うと、その位置を行うことができます。相対と次の位置として設定することができます:絶対

チェックこのスニペット

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

フレキシボックスで別の解決策

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

希望これは

+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

関連する問題