How to use CSS flex to horizontally center an anchor and a paragraph next to each other on a page?
I'm trying to avoid using float
on the anchor and paragraph though I've not exactly mastered flex.
The anchor/image should appear to the left of the paragraph though they as a whole should remain centered.
What I've got to thus far:
section
{
align-items: center;
display: flex;
flex-direction: column;
}
<section>
<a href="#">
<img alt="Example" src="https://jsfiddle.net/img/logo.png" />
</a>
<p>Description goes here...</p>
</section>
https://jsfiddle.net/eondkrpd/
section {
display: flex;
justify-content: center;
}
<section>
<a href="#">
<img alt="Example" src="https://jsfiddle.net/img/logo.png" />
</a>
<p>Description goes here...</p>
</section>
display: flex
creates the flex containerflex-direction: row
, a default setting (i.e., it can be omitted), makes the main axis horizontal, aligning the children (flex items) in this directionjustify-content: center
will center the flex items along the main axisMore details here: Methods for Aligning Flex Items
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With