2016-05-09 10 views
0

私の質問は非常にシンプルですが、かなり長い間苦労しています。部門内の反応的なテキストの移動

top:50%; 
bottom:30%; 
etc. 

のようなものを使用して%を使用したテキストにボックスを移動する方法

https://jsfiddle.net/txzk0ffm/

body { 
 
    width:100%; 
 
} 
 
h1{ 
 
    margin:0; 
 
} 
 
.box-wrap { 
 
    width:70%; 
 
    background:red; 
 
    height:350px; 
 
} 
 
.text-wrap { 
 
    width:35%; 
 
    position:relative; 
 
    top:25%; 
 
    background:rgba(0,0,0,.5); 
 
}
<div class="box-wrap"style="background:red url(http://www.saadiyat.ae/Admin/Content/banner-img-3.jpg) 50% 50% no-repeat;"> 
 
    <div class="img-wrap" > 
 
    <div class="text-wrap"> 
 
     <h1> 
 
     Random text 
 
     </h1> 
 
    </div> 
 
    </div> 
 
</div>

私は、相対位置を使用しようとしましたが、何も思いません起こること

+0

あなたはテキストのみの背景をしたいですか?私は正しい? –

+0

テキストをコンテンツの中央に保存しますか?なぜ%を使用するのですか? –

+0

https://jsfiddle.net/21rzs17t/ – DK3

答えて

2

親の高さがautoである、あなたはそれの内側に垂直mesurmentとしてパーセンテージを使用することはできませんので。

あなたが問題を解決するために

.img-wrap { 
    height: 100%; 
} 

を追加する必要があります。

h1{ 
 
    margin:0; 
 
} 
 
.box-wrap { 
 
    width:70%; 
 
    background:red; 
 
    height:350px; 
 
} 
 
.text-wrap { 
 
    width:35%; 
 
    position:relative; 
 
    top:25%; 
 
    background:rgba(0,0,0,.5); 
 
} 
 

 
.img-wrap { 
 
    height: 100%; 
 
}
<div class="box-wrap"style="background:red url(http://www.saadiyat.ae/Admin/Content/banner-img-3.jpg) 50% 50% no-repeat;"> 
 
    <div class="img-wrap" > 
 
    <div class="text-wrap"> 
 
     <h1> 
 
     Random text 
 
     </h1> 
 
    </div> 
 
    </div> 
 
</div>

+0

ありがとう、私は通常、身長の高いautoを使っています。それは問題になる可能性があります。それは、ハードピクセルx_ x – Harugawa

0

は、親への固定heightを指定し、それが動作します:

.text-wrap { 
    height: 350px; 
} 

.text-wrap h1{ 
    width:35%; 
    position:relative; 
    top:25%; 
    background:rgba(0,0,0,.5); 
} 

See this fiddle

+2

このように嫌いです。なぜそう多くの人々が必要とされない場所でpx値を使用しているのですか? – Qwertiy

+0

私はそれを行う他の方法を知らなかったので、あなたの答えはより良いので、あなたは私のupvoteを持っていることを認識しますが、それはそれを行う他の方法ですので、 さらに、OPはボックスラップにも固定高さを使用します。 –

0

これはあなたがあなたの.box-wrap{ position: relative;}とあなたの.text-wrap{ position: absolute;}

を与えるために持っているよりも何を意味するかである場合

body { 
 
    width:100%; 
 
} 
 
h1{ 
 
    margin:0; 
 
} 
 
.box-wrap { 
 
    width:70%; 
 
    background:red; 
 
    height:350px; 
 
    position:relative; 
 
} 
 
.text-wrap { 
 
    width:35%; 
 
    position:absolute; 
 
    top:25%; 
 
    background:rgba(0,0,0,.5); 
 
}
<div class="box-wrap"style="background:red url(http://www.saadiyat.ae/Admin/Content/banner-img-3.jpg) 50% 50% no-repeat;"> 
 
    <div class="img-wrap" > 
 
    <div class="text-wrap"> 
 
      <h1> 
 
      Random text 
 
      </h1> 
 
    </div> 
 
    </div> 
 
</div>

0

次に、テキストラップクラスのためのあなたのCSSを変更してくださいすることができます -

.text-wrap { 
    position: relative; 
    top: 25%; 
    display: inline-block; 
    background: rgba(0,0,0,.5); 
} 

それはあなたを助けるかもしれません。

0

Height of the img-wrap

あなたのテキストラップ自体がtext-wrapとまったく同じ高さを持っているimg-wrapに対するその位置を、持っています。

最も簡単な解決策はimg-wrapbox-wrapの高さを与えることです:

.img-wrap { 
    height: 100%; 
} 
関連する問題