
On line 4 of the CSS code, I define the display as flex, which enables the flexbox layout for the container.Here is the CSS code for vertically centering the text: For the HTML part, we only need a simple container, so let’s consider the following: Introduced with the CSS3 specification, the display: flex property/value makes it easier to design flexible responsive layout structures without using floats or positioning.Īlong with display:flex, you can easily align anything from table cells to inline elements with the align-items, align-self, and justify-content properties.Ĭlick here to view the demo for this technique. This article demonstrates various CSS vertical alignment techniques: Using a Flexbox, using positioning + transform, using vertical padding, and using line-height. Nowadays, vertically centering text or any element using CSS is a simple task. Unlike horizontal alignments, which can be achieved easily using the text-align property, vertical alignments are often much more tricky to put into action.

As long as CSS has been around, centering elements vertically has always been a frustrating task for many front-end web developers. While this may seem more intuitive for rows, the same justify-content property can be applied to Flexbox columns too! For example, check out how space-around presents on a column. How about we test out each value to see what happens? justify-content valuesĭo you see how the elements align differently depending on the value of justify-content? With one simple property, we can intelligently align elements the way we want. Space-around : elements are spread out along the axis, but there's also space around the edges

Space-between : elements are spread out along the axis (there's space between each) To change their alignment, use justify-content, which can accept the following values:įlex-start : aligned at the start of the containerįlex-end : aligned at the end of the containerĬenter : aligned in the center of the container We'll start with elements that are aligned horizontally because that's the case by default. Main and cross axes Alignment along the main axis

If the elements are arranged vertically in a column (or columns), the main axis is vertical, and the cross axis is horizontal. If the elements are arranged horizontally in a row (or rows), the main axis is horizontal, and the cross axis is vertical. The perpendicular direction is therefore the cross axis. This "main" direction is what we call the flex items' main axis. Items in Flexbox are arranged horizontally or vertically depending on whether you specify row or column for your flex-direction.
