The horizontal centering is fine, however I do have problem with the vertical centering.
Does any one know a good way to implement vertical centering.
Guess you didn't take the time to analyze the dead centering coding and attempt to use it. It can't be easier than just following the method and adapting it to your image.
Guess you didn't take the time to analyze the dead centering coding and attempt to use it. It can't be easier than just following the method and adapting it to your image.
OK I am learning it right.
But can you tell me why in chrome and firefox the image stick to the top and not 20% lower?
there is one slight drawback to your centering method.
When reducing the height of the page, there will come a point where the top of the #container div disappears.
The following method avoids this problem...
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<base href="http://nopeople.com/CSS%20tips/vertical%20center%20with%20CSS/">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>centered vertically and horizontally</title>
<style type="text/css">
html,body {
height:100%;
min-height:210px; /* same as the #container height */
margin:0;
background:#fc6;
font:normal 11px 'comic sans ms',sans-serif;
color:#333;
}
#vertical {
float:left;
width:100%;
height:50%;
margin-top:-105px; /* half the #container height */
}
#container {
clear:both;
width:300px;
height:210px;
margin:auto;
background:#ccc;
}
#container p {
margin:0;
text-align:center;
line-height:25px;
}
#container img {
width:195px;
height:175px;
}
</style>
</head>
<body>
<div id="vertical"></div>
<div id="container">
<p>This is vertically & horizontally centered.
<img src="boat.jpg" alt="fishing boat">
</p>
</div>
</body>
</html>
Bookmarks