Home > OS >  Why does image and text both move when I apply padding to just the text?
Why does image and text both move when I apply padding to just the text?

Time:09-24

I've been playing around to learn and have kinda hit a wall which I can't seem to answer. I want to try and have the text 3/4s the way down of the image.

I've determined that it must be something to do with inline-block as the padding works when the display is block but the text is below the image so it's not as useful.

<img src="img" alt="Image" width="100" height="67"/>
<p style="display: inline-block; padding-top: 20px;">Test</p>

Before:

Before Image

After:

enter image description here

CodePudding user response:

By default, elements that are displayed inline or inline-block are aligned to the bottom of the line. Therefore, as you increase margin-top to p, the bottom of the p moves down causing the img to move as well. You can prevent this by adding vertical-align: top;, which affects elements that are inline with the p.

<img src="img" alt="Image" width="100" height="67"/>
<p style="display: inline-block; padding-top: 20px; vertical-align: top;">Test</p>

  •  Tags:  
  • css
  • Related