From CSS Tricks - A Complete Guide to Flexbox
The Flexbox
Layout (Flexible Box) module aims at providing a more efficient way to layout, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word “flex”).
Key word - container
It doesn't make much logical sense to flex
the img
element because it is not a container, it's a flex-item. If you had a different subset that contained the img
and the text
then it would make sense to flex
it.
Please see the notes in the CSS below:
/* nav would be the main container to position content with justify-content */
nav {
border: dotted black 3px;
padding: 1em;
display: flex;
/* justify-content justify's content (blue container) on the x-axis */
/*justify-content: center;*/ /* centered */
/*justify-content: flex-start;/* /* start */
justify-content: flex-end; /* end */
}
.nav {
border: dotted blue 3px;
padding: 1em;
/*margin: auto;*/ /* will automatically center blue container despite what `justify-content` is defined as because width: 100%; is not defined and because the parent is flexed*/
/* this is now another container because it has the img and p nested, you would use flex to get the items to align in a row if flex-direction: column; wasn't defined. */
display: flex;
flex-direction: column;
justify-content: center;
/* you'll notice if you uncomment those flex properties justify-content doesn't do anything. why? because you can see by the borders there is no where for the content to justify. The p with the pink border is spanning 100% of the container, so you would have to use text-align: center; if you had width: fit-content; on p then you would use `align-items: center;` or `margin: auto; on p. Both do the same. */
text-align: center;
}
.navphoto {
height: 500px;
border: solid orange 5px;
}
img {
max-width: 100%;
}
p {
border: solid hotpink 2px;
/*width: fit-content;*/
}
<nav>
<div class="nav">
<img class="navphoto" src="https://dummyimage.com/200/000/fff">
<p>Dummy text</p>
</div>
</nav>