Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically centering a UL inside a DIV

Tags:

html

css

I have the following:

<div style="background: Red; height: 100px;">
  <ul>
    <li><a href="/">Home</a></li>
  </ul>
</div>

I would like to vertically center the ul in the div but I'm not sure how. Fiddle demo Can anyone advise me on this.

Marilou

like image 629
Robert Wagner Avatar asked May 19 '11 03:05

Robert Wagner


2 Answers

You could always use display: table-cell; in combination with vertical-align: middle;:

HTML:

<div>
  <ul>
    <li><a href="/">Home</a></li>
  </ul>
</div>

CSS:

div {

    background: red;
    height: 100px;
    width: 500px;
    display: table-cell;
    vertical-align: middle;
    text-align: center;

}

See working jsFiddle demo

like image 137
Code Maverick Avatar answered Sep 29 '22 22:09

Code Maverick


CSS does not support vertical centering of arbitrary block elements. However, there are many "hacks" that will let you "make it work".

You can set the parent div to display:inline. However, you will then have issues with your height and background color.

W3C Link: http://www.w3.org/wiki/CSS/Properties/vertical-align

One hack that will do it: (has a great explanation too) http://phrogz.net/css/vertical-align/index.html

like image 34
Jonathan Avatar answered Sep 29 '22 21:09

Jonathan