Vertical Align Middle using CSS

Vertical Align Middle using CSS

Sometimes it gets challenging to vertically align the element in middle of page. Two main CSS properties used in this tutorial are Position property and Transform property.

First of all make a container for the element you want to align vertically. If your element is directly inside body tag then you can apply same styling to body tag, to vertically align your element in the middle.


In this example, there is a container div element with an Id outer. Then there is main element inside this container with an Id inner. Inside this you can put anything you want. I have used a heading tag h1.

Note: You can use Inline or Inline-Block elements or Block level elements.

<!DOCTYPE html>
<div id="outer">
<div id="inner">
<h1>This is Our Main Div Area.</h1>

CSS Code for Vertical Alignment

Outer Element CSS

Then set the Position Property of outer element to fixed which will make it static, with 100% width and height.

position: fixed;
width: 100%;
height: 100%;

Inner Element CSS

Then set the Position Property of inner element to absolute which means that it's position will now depend on outer element. I have set it's position values of Top and Left to 50%.

This will move inner element but it won't be exactly in the center. Now use Transform Property to translate it by -50% for both X and Y axis as shown in code below.

That's it, your element will be in middle now both vertically and horizontally and will always be in middle no matter what.

position: absolute;
top: 50%;
left: 50%;
background-color: #ddd;
border: 2px solid #000;
padding: 10px 10px;
transform: translate(-50% , -50%);


Video Tutorial

Watch video tutorial here.

Social Media Menu with Popup Tooltip using HTML and CSS Horizontal Navigation Menu with HTML and CSS Responsive Sidebar Menu with CSS and JavaScript Cool CSS Button with Hover Animation Pure CSS Custom Radio Buttons Custom Emoji Radio Buttons with CSS Color Picker Design with HTML CSS and JavaScript Custom Radio Buttons with CSS Engraved HTML Buttons With CSS Neumorphic Buttons Design with CSS Cool Profile Card Design with HTML CSS Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Simple Price Range Slider With HTML CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Circle With Two Colors using CSS Simple FAQ Accordion With HTML CSS and JavaScript