by Sai gowtham

How to vertically center an element (Css)

In this tutorial, we are going to learn about two different ways to center an html element vertically with css.

Centering vertically using flex-box

To vertically center elements like (div) we need to add display:flex and align-items:center to the element css class.

Example

Html

<div class="container">
   <p>I'm vertically centered</p>
</div>

Css

.container {
    display:flex;
    align-items:center;
}

css-vertically-center-flexbox

Centering vertically using transform

We can use transform property to absolutely center an element vertically.

Example

<div class="container">
   <div class="center-vertical">
      <p>I'm vertically centered</p>
   </div>
</div>

Css

.container {
 height:200px;
 position: relative;
}

.center-vertical {
 position:absolute;
 top:50%;
 transform:translateY(-50%);
}
  1. Here we added position:absolute to the center-vertical class so that element breaks out from the normal document flow and positioned to its relative parent.

  2. The top:50% moves the element 50% down of its container height.

  3. The translateY(-50%) moves the element 50% up of its own height.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2022 (Beginner + Advanced)
JavaScript - The Complete Guide 2022 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY