2017-03-27 6 views
0

浮動画像とその段落を持つようにしています。しかし、私は段落を真ん中で開始したいので、基本的には整列しません。だから私は私のスパンにマージントップを入れたいが、それをやり遂げなかった。 http://codepen.io/pango143/pen/LWJpmLHTML/CSS:フローティングされた要素の横にある余白を追加できません。

+2

''はインライン要素であり、上部のマージンとパディングは適用されません。 'display:inline-block' – Terry

+0

に設定して、インライン・ブロック・インジケータを表示させると、スパンの内容が画像の下に表示されます。 –

+0

しかし、情報をありがとう、私は今考えている。もしあなたがこのコメントの答えを修正する方法を知っていれば、私はupvoteに感謝することができます –

答えて

1

あなたは行の高さプロパティでそれを修正することができます。

span要素

line-height: 110px; 

OR

に次のCSSコードを追加し表示を設定します。テーブルと、と親要素とdisplay:table-cell;縦線:中央;を子要素に追加します。あなたの画像に影が含まれていて、画像が実際にはそれよりも早く終了すると目が見えるようになった場合、マージントップのプロパティで下に移動できます。

はここにあなたのCSSです:あなたはblock label element

span { 
    display: inline-block; 
    margin-top: 25px; 
} 
+0

行の高さを変更するとスパンの内容の開始点が解決されますが、メッセージが長すぎる場合はテキスト全体が破棄されます –

+0

@Arnoldwannabe別の解決策を使って投稿を編集しました –

+0

テリーの有益なコメントスパンはインライン要素です。あなたに投票するu人。 –

0

だけの要素に表示インラインブロックを追加します:

<div class="container"> 
 

 
    <article> 
 
    
 
    <section> 
 
     
 
     <p> 
 
      <img style="float:left;" src="https://www.w3schools.com/images/w3schools_green.jpg" /> 
 
     <span style="margin-top: 45px; display:inline-block;">Hello World! My name is James polar Good morning</span> 
 
     </p> 
 
     
 
    </section> 
 
    
 
    </article> 
 

 

 
</div>
私のコード

<div class="container"> 
 
    <article> 
 
    <section> 
 
     <p> 
 
     <img style="float:left;" src="https://www.w3schools.com/images/w3schools_green.jpg" /> 
 
     <span style="margin-top: 25px;">Hello World! My name is James polar Good morning</span> 
 
     </p> 
 
    </section> 
 
    </article> 
 
</div>

codepenため

例えばあなたはdiplay-インラインおよび垂直整列でこれを行うことができます

0

:あなたの後ろにして、中間

img { 
 
    display: inline-block; 
 
    width: 30%; 
 
    vertical-align: middle; 
 
} 
 
span { 
 
display: inline-block; 
 
    font-size: 16px; 
 
    margin-top: -73px; /*ajust */ 
 
    vertical-align: middle; 
 
    width: 70%; 
 
} 
 
p { 
 
    position :relative; 
 
    width: 100%; 
 
    font-size:0; 
 
}
<div class="container"> 
 

 
    <article> 
 
    
 
    <section> 
 
     
 
     <p> 
 
      <img src="https://www.w3schools.com/images/w3schools_green.jpg" /> 
 
     <span>Hello World! My name is James polar Good morning</span> 
 
     </p> 
 
     
 
    </section> 
 
    
 
    </article> 
 

 

 
</div>

+0

完璧!いつもやるべきことは簡単です! – Sandwell

0

このような何かを意味ですか?

<div class="container"> 
 

 
    <article> 
 

 
<section> 
 
    
 
    <div> 
 
     <img style="position: absolute;" src="https://www.w3schools.com/images/w3schools_green.jpg" /> 
 
    <p style="padding-top: 40px;">Hello World! My name is James polar Good morning</p> 
 
    </div> 
 
    
 
</section> 
 
    
 
    </article> 
 

 

 
</div>

0

試してみてください。

<div class="main-container"> 
    <article> 
    <section> 
     <div class="container" style="position: relative;"> 
     <img style="" src="https://www.w3schools.com/images/w3schools_green.jpg"> 
     <span style="position: absolute; padding-left: 13px; top: 50%;">Hello World! My name is James polar Good morning</span> 
     </div> 
    </section> 
    </article> 
</div> 
0

にあなたのspanタグを変換する必要があり、このために

section p { 
    display: table; 
} 

section p span { 
    display: table-cell; 
    vertical-align: middle; 
} 

section p img { 
    margin-top: 25px; 
} 
関連する問題