Dead Center Layout Using Css Or Center Align A Div Horizontally And Vertically

How many of you have searched Google for300×120 and placed it in the center of the
“dead center align” or “vertical centerbrowser. Now, we need to have some text inside
css” and found results showing more than athis wrapper div which should be again centered
million pages only to realize that most of theminside div both vertically and horizontally. In this case,
don’t give you the right solution you are lookingwe need to use some browser hacks and tricks. See
for? Well, vertical centering has always been athe html code and we have now additional div tags
nightmare for designers and developers. The solutionto wrap the text and styles targeting different
we are looking at is not just limited to aligning textbrowsers.
vertically center inside a container or a div tag. We#box>#floating { /*display:table for Mozilla & Opera*
need to vertically and horizontally center a divdisplay:table;position:static;
container in a browser. We need to align an object (a}
div or a table) whose height is unknown, inside a div.#floating { /*for IE*
The concept of aligning a div vertically center in awidth:300px;height:100%;position:relative;
browser is pretty simple. It is achieved by absolutely}
positioning a div in half of the area height and moving#floating div { /*for IE*
up by half of its height. This is possible when youposition:absolute;top:50%;left:0px;width:100%;
know the height of the div. Let’s see how it}
works. Here is the css style defined for the wrapper#floating>div { /*for Mozilla and Opera*
div with an ID “box”. The important thing todisplay:table-cell;vertical-align:middle;position:static;
note here is the “left” and “top”}
parameters. These values are defined in percentage#floating div div {position:relative;top:-50%;
values. This really helps you to position a div in the}
center even when you don’t know theThe text is vertically and horizontally aligned centeras
resolution of the browser screen. The top-left corneris the wrapper div. You can use a table as well
of the div is now sits in the center point of thewithout a height.
browser. The next thing to do is move up the divNow what do you do when you want to vertically
half of its height and move left the div half of itsalign a div or table whose height is not pre-defined?
width. This way the center point of the div is alignedSimple! Set the height of the wrapper div to 100%
to the center point of the browser. This is done byand remove “top” and “margin-top”
defining negative values for “margin-top” andattributes. Now in the above html code, remove the
“margin-left” attributes. See the css below.text with a table or a div tag which doesn’t
#boxhave a defined height. It works! Download the code
ter;and experiment it yourselves. If you found something
}more interesting, let us all know. Hope it helped.
We have formed a div with a dimension of