2016-07-26 8 views
0

私はCSSとHTMLでいくつかの練習をしていますが、ここではこの問題に直面したページのレイアウトを作成しています。 注:コード以下は、私は、レイアウトコンテナの幅をパーセントで表しながら位置を絶対位置として使用してテキストにテキストを配置する方法

<!DOCTYPE html> 
    <html> 
    <head> 
    <style> 
     * 
     {margin:0px;padding:0px;box-sizing:border-box;} 
     #container 
     { 
      width:800px; 
      position:relative; 
      background-color:darkgray; 
     } 

     #temp 
     { 
      position: relative; 
     } 
     #content 
     { 
      position:absolute; 
      top:100px; 
      right:120px; 
      width:200px; 
      height:200px; 
      border:1px solid yellow; 
     } 
    </style> 
</head> 
<body> 
    <div id="container"> 
     <div id="temp"> 
      <img src="psp.jpg" alt="no imgage"> 
     <div id="content"> 
     <h1> 
      Goku Vs Vegeta 
      <form> 
       <input type="text" placeholder="Start fighting" name="img"> 
      </form> 
      </h1>  
     </div> 
     </div> 
    </div> 
</body> 
</html> 

を作成しながら、上記のコードは、テキストは、我々はテキストがないブラウザのズームを増減するかどうかを正確に画像上の残っている私のために完璧に動作直面する問題のほんの一例ですそれを位置を変える。

コンテナの幅をピクセルからパーセンテージに変更すると問題が発生します。ここでは2番目のコードがあります。

<!DOCTYPE html> 
    <html> 
    <head> 
    <style> 
     * 
     {margin:0px;padding:0px;box-sizing:border-box;} 
     #container 
     { 
      width:100%; 
      position:relative; 
      background-color:darkgray; 
     } 

     #temp 
     { 
      position: relative; 
     } 
     #content 
     { 
      position:absolute; 
      top:100px; 
      right:120px; 
      width:200px; 
      height:200px; 
      border:1px solid yellow; 
     } 
    </style> 
</head> 
<body> 
    <div id="container"> 
     <div id="temp"> 
      <img src="psp.jpg" alt="no imgage"> 
     <div id="content"> 
     <h1> 
      Goku Vs Vegeta 
      <form> 
       <input type="text" placeholder="Start fighting" name="img"> 
      </form> 
      </h1>  
     </div> 
     </div> 
    </div> 
</body> 
</html> 

第二コードの問題は、我々はブラウザのズームを増減場合は、テキストの位置が変化すると、それはブラウザのまたは容器へのポートを表示するであろうむしろ、画像に対してではありません画像には適用されません。誰かがこの問題を解決する方法を教えてもらえますか?

JSフィドル:H1でhttps://jsfiddle.net/30d2nqeL/

+0

psp.jpg IMGのサイズは? – Toby

+0

@Tobyのサイズは263 KBと幅:1336の高さ:768 –

+0

そして、それは正確に何を目指しているのですか?私はイメージを見ることができないので、ボックスにどこに行くべきかを明確にする何かがイメージにあるかどうかはわかりません。オンラインで画像にリンクできますか?またはもっと説明してください.. – Toby

答えて

1

 * { 
 
      margin: 0px; 
 
      padding: 0px; 
 
      box-sizing: border-box; 
 
     } 
 
     
 
     #container { 
 
      width: 100%; 
 
      position: relative; 
 
      background-color: darkgray; 
 
     } 
 
     
 
     #temp { 
 
position: relative; 
 
      background-image: url('http://lorempixel.com/1336/768'); 
 
      background-size: cover; 
 
      height: 400px; 
 
      padding: 0 10vh; 
 
     } 
 
     
 
     #content { 
 
position: absolute; 
 
top: 50%; 
 
margin-top: -100px; 
 
right: 0; 
 
margin-right: 10%; 
 
      width: 200px; 
 
      height: 200px; 
 
      border: 1px solid yellow; 
 
      background-color: rgba(255,255,255,.4); 
 
     } 
 
     
 
     #content h1 { 
 
      text-align: center; 
 
margin: 1em 0 1em 0; 
 
      color: #000; 
 
     } 
 
#content form { 
 
    text-align: center; 
 
}
<div id="container"> 
 
    <div id="temp"> 
 
    <div id="content"> 
 
     <h1>Goku Vs Vegeta</h1> 
 
     <form> 
 
     <input type="text" placeholder="Start fighting" name="img"> 
 
     </form> 
 
    </div> 
 
    </div> 
 
</div>

0
  1. 形 - 悪い習慣。
  2. css background-imageを使用してください。
  3. flexboxを確認してください。

* { 
 
      margin: 0px; 
 
      padding: 0px; 
 
      box-sizing: border-box; 
 
     } 
 
     
 
     #container { 
 
      width: 100%; 
 
      position: relative; 
 
      background-color: darkgray; 
 
     } 
 
     
 
     #temp { 
 
      display: flex; 
 
      align-items: center; 
 
      flex-direction: row-reverse; 
 
      background-image: url('http://lorempixel.com/1336/768'); 
 
      background-size: cover; 
 
      height: 768px; 
 
      padding: 0 10vh; 
 
     } 
 
     
 
     #content { 
 
      display: flex; 
 
      flex-direction: column; 
 
      justify-content: center; 
 
      align-items: center; 
 
      width: 200px; 
 
      height: 200px; 
 
      border: 1px solid yellow; 
 
      background-color: rgba(255,255,255,.4); 
 
     } 
 
     
 
     #content h1 { 
 
      text-align: center; 
 
      margin: -.4em 0 1em 0; 
 
      color: #000; 
 

 
     }
<div id="container"> 
 
    <div id="temp"> 
 
    <div id="content"> 
 
     <h1>Goku Vs Vegeta</h1> 
 
     <form> 
 
     <input type="text" placeholder="Start fighting" name="img"> 
 
     </form> 
 
    </div> 
 
    </div> 
 
</div>

+0

これはうまく動作しますが、フレックスボックスなしでのみ動作し、位置プロパティを使用する方法はありますか? –

+0

私の2番目の答えを見てください。 –

関連する問題