To get the box to center horizontally, we need to set the parent container to display: flex; . Then we can use justify-content to center horizontally! By default, justify-content refers to the X axis (horizontal). We set this to center to get our child elements to center horizontally with flexbox.
How do I display horizontal flex alignment?
Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child. In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.
How do I horizontally center a div in Flex?
To center the inner div element we will make the parent a flex container. By adding the display: flex; property we make the section element a flex container allowing us to adjust the layout of the div which is now a flex item. To center out item horizontally we use the justify-content: center; .