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
hiphop php

Vote for “Hack” for HipHop/HHMV support (future style PHP) in PHPStorm 8

“Hack” will come. And “Hack” will change PHP, definitly. In case you never heard of it, have a look into

Symfony devs: Creator of Symfony framework is hiring (Cologne, Germany)!

Probably one of the most interesting companies for a PHP developer is currently hiring Juniors and Seniors in Cologne, Germany.

js javascript

[video] Netflix JavaScript Talks about ECMAScript 7: The Evolution of JavaScript

php

How the PHP session garbage collector really works

Sessions in PHP are easy to handle, but have a tricky configuration underneath. The common opinion is, that when you

php

How to install PHP 7.0 on Ubuntu 14.04 LTS

You want to install PHP 7 within 60 seconds ? No problem, but first make sure you have a fresh

php

How to install the mcrypt php extension (to use Laravel 4)

When installing Laravel 4 on a fresh Ubuntu or Debian system, you’ll probably get this error message: “Laravel requires the

anti block adblock

Show ads to ad-blockering visitors – with PageFair.com

Interesting news on the dramatically decreasing web ad market: There’s a new service called PageFair that does basically exactly what

microsoft-windows-azure-cloud-hosting

Microsoft’s Azure platform gives away high money prizes for “testing out” their cloud services

Disclaimer: DEV METAL does not get any money etc. for saying this, there is no affiliate link, no deal with

ilovepreloaders – A tumblr collection of preloader animations

An excellent collection of preloaders (GIFs, Movies and real ready-to-copy&paste code) here in this wonderful blog: http://ilovepreloaders.tumblr.com/ This thing just

Microsoft announces “holographic” 3D interfaces (promo video)

Microsoft has just announces the damn future in business und consumer electronics! Interactive, “holographic” 3D environments, usable without a keyboard,

1/4

Categories

Search

O’Reilly’s Programming JavaScript Applications by Eric Elliott for free (Early Access release)
vagrant
A preinstalled Vagrant box with PHP HipHop / HHVM and Ubuntu 12.04 (Precise Pangolin)
php
Why Modern PHP is Awesome And How You Can Use It Today (Slides by Matt Stauffer)
phpstorm-8
Killer-feature in PHPStorm: Search everywhere
windows-xp-eol
Windows XP is officially dead from today. Do you know people still using it ? Punch them.
js javascript
Push database changes to all clients in real-time (!) with AngularJS and Firebase
php mvc
Preview-release of (my) “php-mvc” project (a simple php mvc barebone)
The architecture of StackOverflow
Dangerous Performance Myths in the Web (video talk by Thomas Lohner, PHPUG Latvia)
hearthbleed-ssl-bug
The SSL Heartbleed bug explained in 30 seconds
O’Reilly sells EVERY ebook for -50% right now!
composer
[Link] How to require versions of PHP, HHVM / HipHop, GD, curl, openssl etc. with Composer
Useful basic linux stuff: Show kernel version, distribution name and distribution version on Ubuntu systems
phpstorm-8
When PHPStorm’s cache eats up your harddisk space
PHPStorm: 42 Tips and Tricks (47min video talk by Mikhail Vink at Dutch PHP Conference 2015)

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