2016-08-15 10 views
1

結果ペインの幅を狭くする場合は、画像領域のサイズが変更されます。別のdiv内の縦と横の中心div

このようなサイズ変更が発生すると、テキストを常に縦と横の中央に配置したいと考えています。

私はこれに適切なCSSを見つけることができません。ここで

.holders { 
 
    position: absolute; 
 
    bottom: 0; 
 
    left: 50%; 
 
    margin-left: -150px; /*half of image width*/ 
 
} 
 
.holder { 
 
    float: left; /*this cannot be changed because we have a row of blocks like the example one in the bottom*/ 
 
    position: relative; 
 
} 
 
.text { 
 
    position: absolute; 
 
    top: 150px; 
 
    /*this is not right at this moment*/ 
 
} 
 
.img { 
 
    width: 100%; 
 
    height: auto; 
 
}
<div style="position: fixed;top:0;left:0;width:100%;height:100%"> 
 
    <div class="holders"> 
 
    <div class="holder"> 
 
     <div class="text"> 
 
     This is text that should be in the center of the block vertically and horizontally 
 
     </div> 
 
     <img class="img" src="http://www.fnordware.com/superpng/pnggrad8rgb.png" /> 
 
    </div> 
 
    </div> 
 
</div>

jsfiddle例です:任意の助けhttps://jsfiddle.net/mddc/4tx5h1tq/34/

ありがとう!

答えて

2

.holders { 
 
    position: absolute; 
 
    bottom: 0; 
 
    left: 50%; 
 
    /* margin-left: -150px; */ 
 
    transform: translateX(-50%);  /* see comments below */ 
 
} 
 
.holder { 
 
    float: left; 
 
    position: relative; 
 
} 
 
.text { 
 
    width: 100%; 
 
    text-align: center; 
 
    position: absolute; 
 
    top: 50%;       /* center text vertically */ 
 
    left: 50%;       /* center text horizontally */ 
 
    transform: translate(-50%, -50%); /* horizontal & vertical centering fine-tuning; 
 
             http://stackoverflow.com/a/36817384/3597276 */ 
 
} 
 
.img { 
 
    width: 100%; 
 
    height: auto; 
 
}
<div style="position: fixed;top:0;left:0;width:100%;height:100%"> 
 
    <div class="holders"> 
 
    <div class="holder"> 
 
     <div class="text"> 
 
     This is text that should be in the center of the block vertically and horizontally 
 
     </div> 
 
     <img class="img" src="http://www.fnordware.com/superpng/pnggrad8rgb.png" /> 
 
    </div> 
 
    </div> 
 
</div>

Revised Fiddle

+1

修正されたjsfiddleは完璧に動作します!本当にありがとう!!!! – curious1

1

using flexboxとお考えですか?最小限の変更で、あなたがやりたいことを正確に実行します。

.holders { 
    position: absolute; 
    bottom: 0; 
    left: 50%; 
    margin-left: -150px; /*half of image width*/ 

} 
.holder { 
    float: left; /*this cannot be changed because we have a row of blocks like the example one in the bottom*/ 
    position: relative; 
} 

.text { 
    position: absolute; 
    top: 0; 
    right: 0; 
    bottom: 0; 
    left: 0; 
    display: flex; 
    flex-direction: column; 
    justify-content: space-around; 
} 

.text p { 
    flex: 0 0 auto; 
    margin: 0; 
    text-align: center; 
} 
.img { 
    width: 100%; 
    height: auto; 
} 
+0

おかげFO rをキンミング!私はあなたのソリューションをテストしました。これは、テキストを垂直方向に正しく配置します。しかし、水平方向では、画像領域が狭くなっていない。テキストを "This is text"に短縮し、画像が応答して縮小するときのテキストの位置を見ることができます。 – curious1

+0

これは 'marginholders-left:-150px'が' .holder'にあるからです。あなたがそれを取り除くと、それはもうそれをしません。(https://jsfiddle.net/4tx5h1tq/37/) –

+0

このHTML構造全体は、実際のページのものを反映しています。レスポンシブなイメージとテキストを集中させなければならない。手伝ってくれてありがとう! – curious1

1

あなたはより少ないコードで同じことを行うことができます。

HTML

<div class="center"> 
    <p class="text">This is text that should be in the center of the block vertically and horizontally</p> 
</div> 

CSS

.center { 
    -webkit-align-items: center; 
    -webkit-justify-content: center; 
    align-items: center; 
    background: url('http://www.fnordware.com/superpng/pnggrad8rgb.png'); 
    display: -webkit-flex; 
    display: flex; 
    height: 300px; 
    justify-content: center; 
    width: 300px; 
} 

LINK

https://jsfiddle.net/4tx5h1tq/42/

+0

ヘンリック、チンミングしてくれてありがとう! – curious1

関連する問題