Logo
  • PHP
    • HipHop / HHVM
    • Modern PHP
    • PHPStorm
    • LAMP
    • Laravel
    • Composer
    • PDO
  • JavaScript
    • node.js
    • AngularJS
  • CSS
    • SASS
    • “CSS4” (CSS level 4)
  • HTML
  • Git
  • LAMP
  • Vagrant
  • UI / UX
  • Architecture of …
  • Off-Topic
With ♥ from Berlin
September 26, 2013
Chris
CSS
Comments Off on How to center a div vertically and horizontally (modern methods, without fixed size!)

How to center a div vertically and horizontally (modern methods, without fixed size!)

PreviousNext
css

“How to center a div” (inside another div or inside the body itself) is one of the most discussed questions ever, and to my surprise there are masses of tutorials which totally over-complicated this issue, usually also with lots of disadvantages, like a fixed height and width of the content div. However, there’s a simple, non-hacky, clean, responsive-ready and crossbrowser-safe method that also does not need any fixed pixel size div settings:

  1. Totally crossbrowser-safe. Works in all browsers (IE8 and higher).
  2. Totally liquid, no div size needed
  3. Totally clean, no weird hacks. All code is used the way it should be used.

 

Method 1:
Center a div in the middle of the viewport

CSS

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    display: table;
}
.container {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.content {
    background-color: red; /* just for the demo */
    display: inline-block;
    text-align: left;
}

HTML

<div class="container">
    <div class="content">
        content content content 
        <br/>
        moooooooooooooooore content
        <br/>
        another content
    </div>
</div>

Result

center-a-div-horizontally-and-vertically

See the JSFiddle here: http://jsfiddle.net/panique/pqDQB/35/

 

Method 2:
Center div vertically and horizontally inside an other div

This is very useful for situation when you have to center content inside a div that don’t have a certain pixel size. Please note that this really doesn’t need ANY pixel size definition. For demo purposes we give the parent div demo pixel sizes, just to create a visible example.

CSS

.parent {
    display: table;
    /* optional, just for the demo */
    height: 300px;
    background: yellow;  
}
.child {
    display: table-cell;
    vertical-align: middle;
    /* optional, just for the demo */
    background: red;	
}
.content {
    /* optional, just for the demo */
    background: blue;
}

HTML

XXX

Result

See the Pen baity by Panique (@Panique) on CodePen.

Related articles across the web

  • From Designer to Developer: A Brief Guide
  • uncss
  • Video: CSS Backgrounds
  • 15 Captivating Parallax Effects from CodePen
  • Using CSS4’s validity pseudo-classes to make forms epic.
  • Use Upcoming CSS4 Features Right Now
  • Add CSS image-orientation: from-image
CSSCSS3
Share this
steam sale coupon voucher

-30% to -90% on Steam and Origin

Soft off-topic, but as most developers like to play from time to time, this might be your change to grab

O’Reilly’s Programming JavaScript Applications by Eric Elliott for free (Early Access release)

O’Reilly currently offers the entire book Programming JavaScript Applications by Eric Elliott for free (as an online-readable version) while its

php

How to setup / install PHP 5.6 on Ubuntu 14.04 LTS

Ubuntu 14.04 LTS ships with PHP 5.5 by default, but if you want to use PHP 5.6, then it’s just

How Snapchat wants to earn money (by establishing vertical videos)

Snapchat is worth 15-19 billion dollars. An insane number, making everybody ask: How the hell is this possible, why has

css3-chrome-font

[Link] Retinafy your Site / Device by Nijiko Yonskai

A simple one-page Gist with all the information you need to make your sites retina-ready: Nijiko Yonskai – Retinafy your

pdo-debug

Debug PDO with this one-line function. Yeah!

Update: This article is a little bit older, the tool has changed gently. Everthing this article still works exactly like

hack-php

The first micro framework written in Hack is there: hack-mvc !

It has been just a few months ago Hack has been first announced, but some awesome people have already developed

bash-command-line-tutorial

Best introduction to unix command line / bash ever (by André Augusto Costa Santos)

css4

Angelina Fabbro talks about “CSS4” in this excellent conference video

A very interesting talk about the future of CSS – let’s name it “CSS4” as we talk about spec level

“Fuck you. Pay Me.” How to deal with clients, the professional way. An excellent talk with Mike Monteiro.

Excellent (and very useful) speech by Mike Monteiro about how to deal with clients, how to create a professional relation

1/4

Categories

Search

css3-chrome-font
Google rolls out Chrome 37, finally fixes horrible font-rendering
php
How to prevent PHP sessions being shared between different apache vhosts / different applications
phpstorm-8
Ignore .idea folder from git in PHPStorm
hack-php
The first micro framework written in Hack is there: hack-mvc !
ilovepreloaders – A tumblr collection of preloader animations
Perfect HTML email templates for perfect HTML emails (outlook!) with INK
A quick history of Comic Sans, the most wrongly used font ever
php-legacy-code
Interesting talk on modernizing a legacy PHP codebase
php mvc
Preview-release of (my) “php-mvc” project (a simple php mvc barebone)
Test GZIP compression of your server easily
How Snapchat wants to earn money (by establishing vertical videos)
php
Awesome list of Design Patterns with PHP code examples
Increase your HTML / CSS coding speed with EMMET
DEF CON 18 – When your computer got stolen and you can still SSH into it: “Pwned by the 0wner” (22min conference talk)
php
[Link] Excellent PHP best practices, 2014 style

Tags

apache bash centos composer conference coupon CSS debian fonts framework git GitHub hack HHVM HipHop HTML HTML5 IDE JavaScript JS LAMP laravel linux mod_rewrite MVC MySQL Nginx optimization PHP PHP 5.5 PHP 5.6 phpmyadmin PHPStorm security server SSD Ubuntu UI UX vagrant video virtual machine voucher VPS wordpress
Side-Project: Wordle-Solver:
www.wordle-helper.info

Pages

  • Privacy Policy