In this article we are going to explore different ways you can implement vertical alignment in CSS along with examples and use-cases for each method
Here are the list of different ways we are going to explore
- Vertical alignment using Flexbox
- Vertical alignment using position: absolute
- Vertical alignment using Display: table
- Vertical alignment using CSS: Grid
- Vertical alignment using Line Height
- Vertical alignment using Padding
- Vertical alignment using Display: inline-block
- Vertical alignment using CSS calc() function
- Vertical alignment using margin: auto
let's go through each of these one by one
1. Vertical Alignment using flexbox
Flexbox is a layout mode in CSS3. Flexbox allows you to automatically align and resize containers based on the height and width of the screen
Flexbox is also direction agnostic unlike some other CSS frameworks. Here is how you can easily vertically align an object using flexbox
The properties that we will need to vertically align are
- display: flex
- align-items: center
You can also use flex-direction: col to align the items vertically insead of row
let us consider an example
Example 1: Aligning Nav Bar
Example: 2 Vertically center alignment of text in a hero section
In this example we will vertically align the text in a hero section.
.hero-section {
display: flex;
flex-direction: column; /* this will create a column of elements */
justify-content: center; /* The property centers the elements vertically. */
height: 100vh; /* we are giving the full viewport height */
text-align: center; /* we are aligning the text horizontally */
color: #blue;
}
2. Vertical alignment using position: absolute
position:absolute
can be used to vertically align CSS. However, it should be used with caution as it could lead to layout problems if it is not used properly.
Other CSS properties such as flexbox or Grid are preferable to using position: absolute.
Here are some of the examples of
Example 1: vertically aligning one element in the middle of a parent element
Example 2: Aligning multiple elements vertically using position: absolute
In this example we will learn how to align multiple elements vertically using **position: absolute**
3. Vertically Align items using Display: table
In this method we will use the properties of the table element to align elements vertically
Example 1: Vertically aliogn in the middle of a Parent element
let's look at the code
body {
font-family: sans-serif;
}
.parent-element {
display: table;
height: 200px;
width: 100%;
border: 1px solid black;
}
.child-element {
display: table-cell;
vertical-align: middle;
text-align: center;
}
<div class="parent-element">
<div class="child-element">Center me!</div>
</div>
In this example the parent is a table and the child is a table cell. we are then vertically aligning the child using the vertical-align: middle property
Example 2: Aligning 3 elements vertically in a Single Line
Let us look at the code first and then discuss it further
and CSS
In this case we are displaying each child element as a table cell within the table parent container
4. Vertical alignment using CSS: Grid
Let us use the CSS grid. The Grid provides a simple way to vertically align the content
Example 1 : Aligning a single child vertically inside a parent container
In this example we have a Grid class and an item class and we are giving the grid class the property of display:grid and we are aligning items in center
Note: Grid is not supported with older browsers, but many newer browsers support.
Example 2: Aligning multiple items vertically inside a container
In this example we are going to vertically align multiple items
5. Vertical Alignment using Line Height
Vertical alignment can be used to align individual lines of text. Use this method when you have fixed height only
Let us look at some of the examples to better understand
Example 1: Cantering text in a navigation bar
In this. example we are going to vertically center the links in the navigation bar using line height
Example 2: Centering text in a button
If you have a big button and you want to center the text inside the button, you can do it using line height.
The trick to center text using line height is to set it at 50%
6 Vertical alignment using padding
Many times padding can be used to align items inside a container. Remember
We have a button with fixed height and we want to center the text inside it vertically
In the above example we are giving the top and bottom padding of 10 px and left and right padding of 20px to center the text where we want to inside the button
The next example is an important one and much searched across the internet
Example 2: Vertically centering a single line of text inside of a div
Here we have 15px of padding applied on top and bottom of the div with the class box
the padding pushes the div borders 15px up and down thus vertically cantering the text in the middle
7. Vertical alignment using Display: inline-block
Display: inline-block is na important css property using which you can display the element inline (like an inline element) and you can also adjust the height and width of the element as well like an block element
It is important to note that the inline-element works in realtion to its sibling elements and not inside the element
vertical-align property can be used along with the inline-block element to vertically position the element in relation to each other
8. Vertical alignment using CSS calc() function
the calc()
in css can be used to perform calculation to deteremine property values of elements.
It can also be used to align content by settings the top or bottom positions and substracting 50% of their height
the parent element should be set to relative and the child element should be set to absolute
What are we doing here.
we are calculating the top position of the .content dev by using the calc(50% - 10px)
This moves it down the halfway point of .container
and then moves it up by half of its own height
9. Vertical alignment using margin: auto
The vertical alignment using margin:auto you can align the items vertically but this only works when the parent container has a specified height and the child container has a specified height and the parent is a flex container
let look at some examples to better
In this example the parent has a specified height and the display:flex property.
the .hero-heading and margin:auto causes it to vertically and horizontally centered within the parent container
Conclusion
In this article we have shown different ways you can align items vertically using various css methods and properties
Here are all the methods summarized for you:
- Display: table Here you create the parent element as a table, doing this the child element becomes a table cell and hence can be easily vertically aligned
- Flexbox: Here we make the parent container into a flexible container and we can easily align the child components using the align-items property of flexbox
- CSS: Grid : With CSS Grid you can create rows and columns layout. Using the CSS Grid you can easily align the items vertically
- Line-height: In the line height method you can easily align single line of text within a container. If you know the height of the container you can easily set the line-height of the child and align it with the container as you want
- Padding: You can set the padding to increase and decrease the area surrounding the element and using that you can easily align the items vertically
- Display:inline-block: You can easily align inline text and images using the inline-block element properties
- CSS calc() function : Using the CSS calc() function you can calculate the height of the container and set the height of the child to automatically align with that of the container by setting the height to 50%
- Position:absolute : You can use this property to set position: absolute on the child element to align it with the parent property
- Margin:auto: You can set margin: auto in a flex container to automatically align the item or child element vertically and horizontally with respect to the parent container.
I hope that you find the article useful. Thank you for reading.