How can I vertically align elements in a div?
How can I vertically align elements in a div?
455
27-Apr-2023
Updated on 27-Apr-2023
Aryan Kumar
27-Apr-2023To vertically align elements in a div, you can use CSS flexbox or grid. Here are examples using both:
Using CSS Flexbox:
In this example, the container div is set to use flexbox with the display: flex property. The align-items: center property centers the elements vertically, and the justify-content: center property centers them horizontally. The height property of the container is also set to a fixed height so that the container takes up a specific amount of space on the page.
Using CSS Grid:
In this example, the container div is set to use CSS grid with the display: grid property. The place-items: center property centers the elements both vertically and horizontally. The height property of the container is also set to a fixed height so that the container takes up a specific amount of space on the page.
You can adjust the height of the container, the number of elements, and the CSS styles as needed to customize the appearance of the vertically aligned elements.