Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <main class="main-content">
  <h1 class="post-title">The Image Alignment Conundrum</h1>
  <div class="entry-content">
    <p class="intro">The challenge seems simple on paper but hard in CSS: Without changing the HTML markup, allow for various alignments in a single view, including centered elements, floated elements, half-bleed elements, and full-bleed elements. It seems the best approach to half- and full-bleed is using CSS Grid, but that stands in the way of floats.</p>
    <p><em>Current solution: Regular content is aligned using standard <code>max-width</code> and <code>margin: 0 auto;</code>. Special alignment <code>&lt;figure&gt;</code> elements are turned into grid containers spanning the full width of the available space using a ridiculous <code>calc</code> formula, and the images and captions within are placed on the individual grids. Salient CSS bits start at line 69.</em></p>
    <p class="intro">Descriptions of each alignment are found under each image.</p>
    <p class="intro">Please fork and send your suggested solutions to <a href="https://twitter.com/mor10">@mor10 on Twitter</a>.</p>
    <h2>Centered</h2>
    <figure class="align-center">
      <a href="https://unsplash.com/@andozo?photo=vMRMYoLQzwo" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/vMRMYoLQzwo/1200x800" alt="">
      </a>
      <figcaption class="caption">Oslo. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-center</code> are centered with a max width matching the max width of the main content. If an element is narrower than the width, it is center aligned leaving equal space on both sides.</p> 
    <h2>Left-aligned</h2>
    <figure class="align-left">
      <a href="https://unsplash.com/@andozo?photo=xkMArBuwK44" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/xkMArBuwK44/400x400" alt="">
      </a>
      <figcaption class="caption">Pyramid. Photo by Andreas Rønningen</figcaption>
    </figure>
  <p>Elements with the class <code>align-left</code> have a width of 50% of the available space and are floated to the left with adjacent content filling the empty space on the right.</p>
<p><em>Some dummy text to allow text wrapping:</em> Preserved defective offending he daughters on or. Rejoiced prospect yet material servants out answered men admitted. Sportsmen certainty prevailed suspected am as. Add stairs admire all answer the nearer yet length. Advantages prosperous remarkably my inhabiting so reasonably be if. Too any appearance announcing impossible one. Out mrs means heart ham tears shall power every. </p>
<p><em>More dummy text:</em> Preserved defective offending he daughters on or. Rejoiced prospect yet material servants out answered men admitted. Sportsmen certainty prevailed suspected am as. Add stairs admire all answer the nearer yet length. Advantages prosperous remarkably my inhabiting so reasonably be if. Too any appearance announcing impossible one. Out mrs means heart ham tears shall power every. </p>
    <h2>Right-aligned</h2>
    <figure class="align-right">
      <a href="https://unsplash.com/@andozo?photo=lr81rOg-gys" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/lr81rOg-gys/400x400" alt="">
      </a>
      <figcaption class="caption">Barn owl. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-right</code> have a width of 50% of the available space and are floated to the right with adjacent content filling the empty space on the left.</p>
    <p><em>Some dummy text to allow text wrapping:</em> Preserved defective offending he daughters on or. Rejoiced prospect yet material servants out answered men admitted. Sportsmen certainty prevailed suspected am as. Add stairs admire all answer the nearer yet length. Advantages prosperous remarkably my inhabiting so reasonably be if. Too any appearance announcing impossible one. Out mrs means heart ham tears shall power every. </p>
<p><em>More dummy text:</em> Preserved defective offending he daughters on or. Rejoiced prospect yet material servants out answered men admitted. Sportsmen certainty prevailed suspected am as. Add stairs admire all answer the nearer yet length. Advantages prosperous remarkably my inhabiting so reasonably be if. Too any appearance announcing impossible one. Out mrs means heart ham tears shall power every. </p>
    <h2>Half-bleed</h2>
    <figure class="align-half">
      <a href="https://unsplash.com/@andozo?photo=KUfkX6gVwBU" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/KUfkX6gVwBU/2600x800" alt="">
      </a>
      <figcaption class="caption">Oslo. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-half</code> are displayed as "half-bleed" meaning they extend on the left and right to fill <em>half the available space</em> between the outer edge of the <code>main</code> container and the <code>entry content</code>.</p>
    <h2>Full-bleed</h2> 
    <figure class="align-full">
      <a href="https://unsplash.com/@andozo?photo=i9FLJwYhVQs" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/i9FLJwYhVQs/2600x800" alt="">
      </a>
      <figcaption class="caption">Lake. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-full</code> are displayed as full-bleed meaning they extend on the left and right to fill <em>all available space</em> between the outer edge of the <code>main</code> container.</p>
    <h2>Align full left</h2> 
    <figure class="align-full-left">
      <a href="https://unsplash.com/@andozo?photo=i9FLJwYhVQs" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/i9FLJwYhVQs/2600x800" alt="">
      </a>
      <figcaption class="caption">Lake. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-full-left</code> are extgended to the left edge and end at the right border of the content container with the caption appearing to the right.</p>
    <h2>Align full right</h2> 
    <figure class="align-full-right">
      <a href="https://unsplash.com/@andozo?photo=i9FLJwYhVQs" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/i9FLJwYhVQs/2600x800" alt="">
      </a>
      <figcaption class="caption">Lake. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-full-right</code> are extgended to the right edge and end at the left border of the content container with the caption appearing to the left.</p>
    <h2>Align half left</h2> 
    <figure class="align-half-left">
      <a href="https://unsplash.com/@andozo?photo=i9FLJwYhVQs" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/i9FLJwYhVQs/2600x800" alt="">
      </a>
      <figcaption class="caption">Lake. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-half-left</code> are extgended to the left edge and takes up half the available space on the right</p>
    <h2>Align half right</h2> 
    <figure class="align-half-right">
      <a href="https://unsplash.com/@andozo?photo=i9FLJwYhVQs" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/i9FLJwYhVQs/2600x800" alt="">
      </a>
      <figcaption class="caption">Lake. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-half-right</code> are extgended to the right edge and takes up half the remaining space on the left;</p>
    <h2>No alignment</h2>
    <figure class="card align-none">
      <a href="https://unsplash.com/@andozo?photo=S2YssLw97l4" target="_blank" rel="nofollow">
        <img src="https://source.unsplash.com/S2YssLw97l4/2600x1600" alt="">
      </a>
      <figcaption class="caption">Sparklers and Warm Souls. Photo by Andreas Rønningen</figcaption>
    </figure>
    <p>Elements with the class <code>align-none</code> have no alignment and should appear as part of the normal content flow.</p>
    <p><em>Dummy text to have additional text to show:</em> Blind would equal while oh mr do style. Lain led and fact none. One preferred sportsmen resolving the happiness continued. High at of in loud rich true. Oh conveying do immediate acuteness in he. Equally welcome her set nothing has gravity whether parties. Fertile suppose shyness mr up pointed in staying on respect. </p>
  </div><!-- .entry-content -->
</main><!-- .main-content -->
            
          
!
            
              body {
  margin: 0;
  background-color: hsl(0, 0%, 90%);
  font-size: 100%;
  font-size: 1.2em;
}

figure {
  margin: 0 0 1.5em 0;
}

.caption {
  padding-top: .5em;
  text-align: center;
  font-style: italic;
}

img {
  display: block;
  width: 100%;
  height: auto;
}

code {
  font-weight: bold;
  font-style: normal;
  font-size: 120%;
}

p {
  margin-top: 0;
  line-height: 1.3em;
}

.intro {
  font-family: sans-serif;
}

.main-content {
  background-color: white;
  position: relative;
  max-width: 80vw;
  margin: 0 auto;
  padding: 1em 0;
}

.post-title,
.entry-content {
  max-width: 40rem;
  margin: 0 auto;
}

.post-title {
  padding: 1em 0 .5em;
}

.align-left {
  width: 50%;
  margin-right: 1em;
  float: left;
}

.align-right {
  width: 50%;
  margin-left: 1em;
  float: right;
}

@media screen and (min-width: 50rem) {
  .align-half,
  .align-full,
  .align-full-left,
  .align-full-right,
  .align-half-left,
  .align-half-right {
    margin-left: calc(((80vw - 40rem) / 2) * -1);
    width: 80vw;
    display: grid;
    grid-template-columns: 
      [full-start] minmax(1em, 1fr) 
      [wide-start] minmax(0, 1fr)
      [content-start] minmax(0, 40rem) [content-end]
      minmax(0, 1fr) [wide-end]
      minmax(1em, 1fr) [full-end];
  }

  .align-half > * {
    grid-column: wide;
  }

  .align-full > * {
    grid-column: full;
  }

  .align-full-left a {
    grid-column: full-start/content-end;
  }

  .align-full-left .caption {
    grid-column: content-end/full-end;
    align-self: end;
    text-align: left;
    padding-left: 1em;
  }

  .align-full-right a {
    grid-column: content-start/full-end;
    grid-row: 1;
  }

  .align-full-right .caption {
    grid-column: full-start/content-start;
    grid-row: 1;
    align-self: end;
    text-align: right;
    padding-right: 1em;
  }

  .align-half-left a {
    grid-column: full-start/wide-end;
  }

  .align-half-left .caption {
    grid-column: full-start/content-start;
    grid-row: 2;
  }

  .align-half-right a {
    grid-column: wide-start/full-end;
  }

  .align-half-right .caption {
    grid-column: content-end/full-end;
    grid-row: 2;
  }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console