Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

#microtip - Center a Div Horizontally and Vertically

DZone's Guide to

#microtip - Center a Div Horizontally and Vertically

· Java Zone
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

A small tip that can be handy, say when you are creating a landing HTML page. The goal is to position or align a div in the center of the browser. There are many misleading and non working examples when you google this, so I'm pasting a simple solution that works.

HTML

<div class="outer-container">
 <div class="inner-container">
 <center>
 <h3>Hello Center!</h3>
 </center>
 </div>
</div>

CSS

.outer-container {
 position: fixed;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
}

.inner-container {
 position: relative;
 top: 40%;
}

JsFiddle: http://jsfiddle.net/31L9jdsz/

Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.

Topics:

Published at DZone with permission of Abishek Baskaran, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}