Skip to content

Instantly share code, notes, and snippets.

@mjj2000
Created May 21, 2013 03:23
Show Gist options
  • Save mjj2000/5617292 to your computer and use it in GitHub Desktop.
Save mjj2000/5617292 to your computer and use it in GitHub Desktop.
[CSS]Center div in div
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style>
#table{
display:table; /* must be 'table' */
}
#table-cell{
position:relative; /* must be 'relative' */
width:500px; /* must be assigned an absolute value */
height:500px; /* must be assigned an absolute value */
display:table-cell; /* must be 'table-cell' */
vertical-align: middle; /* center content div vertically */
background:blue;
}
#div{
position:relative; /* must be 'relative' */
margin:auto auto; /* center div horizontally*/
width:50%;
height:50%;
background:red;
}
</style>
</head>
<body>
<div id="table">
<div id="table-cell">
<div id="div"></div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment