This gist builds upon a simpler gist that does not have a sub-title.
-
Create a flex container:
.container { display: flex; }
-
Use
justify-content
to control how items are horizontally positioned (when inrow
direction):.container { justify-content: center; }
-
Flexbox alignment (up-down when in the
row
direction) defaults tostretch
, which breaks image aspect ratio. Usealign-items
to centre the items instead:.container { align-items: center; }
Simple html page header
:
<header class="container">
<img src="https://picsum.photos/id/237/150" alt="Puppy logo">
<div>
<h1>Lucifer</h1>
<h2>The Demon Puppy</h2>
</div>
</header>
img
and div
are the Flex items. h1
and h2
are not included in the Flex context and are regular block
elements.
- HTML:
- CSS Selectors:
- Flexbox:
- MDN: Flexbox basics
- MDN: justify-content
- MDN: align-items
- Reference: Complete Guide to Flexbox