Skip to content

Instantly share code, notes, and snippets.

@scottgruber
Created March 27, 2019 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottgruber/c676747f9a9e4b2c8676fc5007dc468f to your computer and use it in GitHub Desktop.
Save scottgruber/c676747f9a9e4b2c8676fc5007dc468f to your computer and use it in GitHub Desktop.
The min-content keyword gives us the width of the largest unbreakable element inside the box (i.e., the widest word or image or fixed-width box). This is exactly what we need! Now, giving our figures an appropriate width and horizontally centering them as simple is two lines of code: Fall back for older browser doesn't work unless you know exact…
figure {
width: min-content;
margin: auto;
}
<p>Some text […]</p>
<figure>
<img src="adamcatlace.jpg" />
<figcaption>
The great Sir Adam Catlace was named after
Countess Ada Lovelace, the first programmer.
</figcaption>
</figure>
<p>More text […].</p>
/**
* Intrinsic sizing
*/
figure {
max-width: 300px;
max-width: min-content;
margin: auto;
}
figure > img { max-width: inherit }
/* Basic styling */
figure {
padding: 10px;
border: 1px solid silver;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment