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

Auto Save

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

              
                <p>
  Here's a card component whose width and height default to the size of its contents, thanks to `width: fit-content` and the default `height: auto`:
</p>

<div class="card">
  Hello world
</div>

<p>
  Here are two cards side-by-side, thanks to flexbox:
</p>

<div class="container" style="display: flex; max-width: 500px; border: 1px solid black;">
  <div class="card">
    Hello world
  </div>
  <div class="card">
    This card has more content than the first one, so much that it wraps to multiple lines. Notice how that makes the first card's height stretch to match this one.
  </div>
</div>

<p>
  So a card's height is stretchable, but can its width be, as well? Yes, with `flex-grow`!
</p>

<div class="container" style="display: flex; max-width: 500px;">
  <div class="card" style="flex-grow: 1;">
    Hello world
  </div>
  <div class="card" style="flex-grow: 1;">
    Hello world
  </div>
</div>

<p>
  Now let's try to have a grid of cards. `flex-grow` doesn't have any effect in CSS grid, so let's use `justify-self: stretch` instead:
</p>

<div class="container" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr))">
  <div class="card" style="justify-self: stretch;">
    Hello world
  </div>
  <div class="card" style="justify-self: stretch;">
    Hello world
  </div>
  <div class="card" style="justify-self: stretch;">
    Hello world
  </div>
  <div class="card" style="justify-self: stretch;">
    Hello world
  </div>
</div>

<p>
  Hmm, not quite what we expected. Why did the cards' width not stretch to fill the grid's columns?
</p>

<p>
  Because the card component has an explicit `width` of `fit-content`. Let's try to change that to `auto`:
</p>

<div class="container" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr))">
  <div class="card" style="justify-self: stretch; width: auto;">
    Hello world
  </div>
  <div class="card" style="justify-self: stretch; width: auto;">
    Hello world
  </div>
  <div class="card" style="justify-self: stretch; width: auto;">
    Hello world
  </div>
  <div class="card" style="justify-self: stretch; width: auto;">
    Hello world
  </div>
</div>

<p>
  Great, but now here's what our card looks like when used on its own:
</p>

<div class="card" style="width: auto;">
  Hello world
</div>

<p>
  Oof, that's way too wide. But if we add any `width` or `max-width` to it, we're back to our previous problem where the width is not stretchable inside a grid.
</p>

<p>
  So is there a solution? Kinda: `inline-block`. Here's a `display: inline-block; width: auto;` card on its own:
</p>

<div class="card" style="display: inline-block; width: auto;">
  Hello world
</div>

<p>
  ...in a flex container (with `flex-grow: 1`):
</p>

<div class="container" style="display: flex; max-width: 500px;">
  <div class="card" style="display: inline-block; width: auto; flex-grow: 1;">
    Hello world
  </div>
  <div class="card" style="display: inline-block; width: auto; flex-grow: 1;">
    Hello world
  </div>
</div>

<p>
  ...and in a grid (with `justify-self: stretch`):
</p>

<div class="container" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr))">
  <div class="card" style="display: inline-block; width: auto; justify-self: stretch;">
    Hello world
  </div>
  <div class="card" style="display: inline-block; width: auto; justify-self: stretch;">
    Hello world
  </div>
  <div class="card" style="display: inline-block; width: auto; justify-self: stretch;">
    Hello world
  </div>
  <div class="card" style="display: inline-block; width: auto; justify-self: stretch;">
    Hello world
  </div>
</div>

<p>
  But it is quite unexpected for a card to be `inline-block`, and can cause surprises and annoying issues with alignment. For instance, we might expect `&lt;div class="card"&gt;...&lt;/div&gt;&lt;div class="card"&gt;...&lt;/div&gt;` to render two cards on top of each other, but:
</p>

<div>
  <div class="card" style="display: inline-block; width: auto;">
    Hello world
  </div>
  <div class="card" style="display: inline-block; width: auto;">
    Hello world
  </div>
</div>

<p>
  And more importantly, what if we want the default width to be something other than `fit-content`, like `180px`?
</p>

<p>
  What I would love is a new `default-width` property. Its initial value would be `auto`, and when set to anything else, it would define the width of the element when its `width` is `auto`, while allowing it to stretch in a grid context. With this property, we could replace the card's `width: fit-content` by `default-width: fit-content` and all the examples above would work.
</p>

<p>
  Note that UAs will probably want to set `default-width` to `fit-content` (or something similar) for elements where `width: auto` seems to work differently, such as `button`, form controls, and `table` (see <a href="https://github.com/w3c/csswg-drafts/issues/7427" target="_blank">this issue</a>).
</p>

<p>
  Thanks for reading!
</p>
              
            
!

CSS

              
                *, *::before, *::after {
  box-sizing: border-box;
}

.card {
  width: fit-content;
  padding: 10px;
  border: 1px solid black;
  border-radius: 10px;
}

p:not(:first-child) {
  margin-top: 50px;
}

.container {
  gap: 1px;
  padding: 1px;
  border: 1px solid black;
  border-radius: 12px;
}

              
            
!

JS

              
                
              
            
!
999px

Console