Horizontal and vertical centering in css

For now, to set horizontal centering, you were used to do this:

margin: 0 auto;
text-align: center;

But it doesn’t work for vertical centering. The solution is to declared a height and apply this style:

.hv-center {
    //Center the element
    margin: auto;
    
    //Make the element fills the screen
    position: absolute;
    top: 0; left: 0; bottom: 0; right: 0;
}

Here, we first, centered the element with margin: auto, and then we let the element fills the screen. The result will be that the element will be centered vertically and horizontally.

That’s all 🙂

Advertisements
Horizontal and vertical centering in css

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s