I want to horizontally center two (or possibly more) inline-block elements inside a container block element. It should look like this:
-------------------------- | _____ _____ | | | | | | | | | foo | | bar | | | |_____| |_____| | |_________________________|
However, with my broken code, it is currently looking like this:
-------------------------- | _____ ____ | || | | | | || foo | | bar | | ||_____| |_____| | |_________________________|
HTML
<div> <a>foo</a> <a>bar</a> </div>
CSS
div a { display: inline-block; padding: 1em; margin: 1em; border: 1px solid black; }
The reason why the two anchors have to be inline-block and not just plain inline is because I don't want the anchor's padding and margin to overlap.
Just add margin: auto and a fixed width to the element you want to center, and the margins will force the element to center.
Try using this: margin: 0 auto; Or text-align: center; on the parent <div> ...
To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.
You can center inline-block (and inline) elements by setting text-align: center on a parent element.
Simply set text-align: center;
on the div container.
Set text-align: center;
on the parent element.
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