Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <article>
  <h1>Removing the Gap Below Images</h1>
  <section>
    <h2>Cause of the Gap</h2>
    <p class="description">
      Images are inline elements. This means they go along side if text. By default images sit on the same baseline as text. In typography that uses a Latin alphabet, there are letters that have what are called <i>descenders</i>. Descenders are the parts of letters like pgj that go below the baseline. See the diagam below where descenders are highlighted in red:
    </p>
    <figure>
      <img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/Descender.png" alt="diagram of letters and numbers with descenders highlighted in red">
      <figcaption><a href="https://commons.wikimedia.org/wiki/File:Descender.png">Image</a> by <a href="https://commons.wikimedia.org/wiki/User:Nilsjohan~commonswiki">Nilsjohan~commonswiki</a></figcaption>
    </figure>
    <p class="description">To accomodate these descenders there is space added below the baseline (the line where the bottom of most letters and numbers sit).</p>
    <p>Images sit on the baseline and the gap you see is the space left for descenders.</p>
  </section>
  <section>
    <h2>Fixing the Gap</h2>
    <p>There are two ways to fix the gap:</p>
    <ol>
      <li><b>Change the image to a block element:</b> block elements, unline inline elements don't make room for descenders.</li>
      <li><b>Set the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align">vertical-align</a> property on the image:</b> this property changes where the image sits in relation to the text and can effectively move it so it doesn't rest on the baseline.</b> </li>
    </ol>

    <p class="description">Below are some examples. The first is an image with the gap. The second uses the <code>display: block</code> solution and the third uses the <code>vertical-align</code> property solution. Look at the CSS to see the solutions.</p>

    <div class="examples">
      <div class="gap example">
        <p><img src="https://www.place-hold.it/100x100&text=gap" alt="with gap" /></p>
      </div>
      <div class="block example">
        <p><img src="https://www.place-hold.it/100x100&text=block" alt="with block css" /></p>
      </div>
      <div class="align example">
        <p><img src="https://www.place-hold.it/100x100&text=valign" alt="with vertical-align css" /></p>
      </div>
    </div>

  </section>
  <section class="references">
    <h2>References</h2>
      <ul>
        <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align">vertical-align: Mozilla Developer Network Docs</a></li>
        <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align#vertical_alignment_in_a_line_box">Vertical alignment in a line box: Mozilla Developer Network Docs</a></li>
        <li><a href="https://css-tricks.com/almanac/properties/v/vertical-align/">vertical-align: CSS-Tricks</a></li>
      </ul>
    
  </section>
</article>
              
            
!

CSS

              
                /* ===============================
  FIXES
  ================================ */
/* fixes problem by making the image a block element */
.block img{
  display:block;
}

/* fixes the problem by setting the vertical-align on the image */
.align img{
  vertical-align: middle; /* setting to other values bottom/text-top/sub also works */
}

/* ===============================
  GENERAL PAGE STYING
  not related to the fixes
  ================================ */

/* add border to all examples
  need the border to see the gap */
.example p{
  border:1px solid #333;
  padding: 0;
  margin: 0;
}

.examples{
  display: flex;
  flex-wrap: wrap;
  grid-gap: 1rem;
}

body{
  font-size: 16px;
}
p{
  line-height: 1.4;
}
li{
  margin-bottom: 1rem;
}
article{
  padding: .5rem;
}
figure{
  margin: 0;
  padding: 0;
}
figure img{
  max-width: 100%;
  height: auto;
}
figure figcaption{
  font-size: .8rem;
  text-align:right;
}
@media screen and (min-width: 640px){
  article{
    width: 36rem; /*fallback for older browsers */
    width: 60ch;
    margin: 0 auto;
  }
}
              
            
!

JS

              
                
              
            
!
999px

Console