search

Home  >  Q&A  >  body text

The image inside the div has extra space below the image

<p>Why is the height of <code>div</code> in the following code greater than the height of <code>img</code>? There is a gap below the image, but it doesn't seem to be padding/margin. </p> <p><strong>What is the gap or extra space below the image? </strong></p> <p><br /></p> <pre class="brush:css;toolbar:false;">#wrapper { border: 1px solid red; width:200px; } img { width:200px; }</pre> <pre class="brush:html;toolbar:false;"><div id="wrapper"> <img src="http://i.imgur.com/RECDV24.jpg" /> </div></pre> <p><br /></p>
P粉523335026P粉523335026497 days ago554

reply all(2)I'll reply

  • P粉909476457

    P粉9094764572023-08-30 11:46:43

    Another option suggested in this Blog post is styling the image to style="display: block;"

    reply
    0
  • P粉222320176

    P粉2223201762023-08-30 10:08:15

    By default, the image is rendered inline, just like the letters, so it's on the same line as a, b, c, and d.

    There is a space below this line for the letters g, j, p and q.

    you can:

    div {
      border: solid black 1px;
      margin-bottom: 10px;
    }
    
    #align-middle img {
      vertical-align: middle;
    }
    
    #align-base img {
      vertical-align: bottom;
    }
    
    #display img {
      display: block;
    }
    <div id="default">
    <h1>Default</h1>
      The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt="">
    </div>
    
    <div id="align-middle">
    <h1>vertical-align: middle</h1>
      The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>
      
      <div id="align-base">
    <h1>vertical-align: bottom</h1>
      The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>
    
    <div id="display">
    <h1>display: block</h1>
      The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt="">
    </div>

    reply
    0
  • Cancelreply