Forum Moderators: not2easy

Message Too Old, No Replies

Problem with center div

         

poelske88

9:49 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



Hello,

I would like to center a <div> to the middle of my page (horizontally).

To do this i use the next CSS-code

width:800px;
height:100px;

position:absolute;
left:50%;
margin-left:-400px;


When I do this, all seems to be all right.
But a problem occurs when you make your browser screen (Internet Explorer, Chrome, ...) smaller. Then the left part seems to vanish and even with the scrollbar you cant find it.

I think it has something to do with the -400 for margin-left, but i dont know how to solve this.

Any suggestions.

rocknbil

3:43 am on Feb 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try

width:800px;
height:100px;
margin auto;

Absolute positioning will present many . . . challenges.

poelske88

2:22 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



okay thanx,
but this doesnt work in Internet Explorer. It does howerver work in Chrome.

here is the code
<html>
<head>
<title>test</title>
<style type="text/css">

#test
{
width: 150px;
height:200px;
margin:auto;
position:relative;
background-color:green;
}


</style>
</head>



<body>

<div id="test">
test
</div>


</body>
</html>

birdbrain

4:23 pm on Feb 24, 2010 (gmt 0)



Hi there poelske88,

if you start your coding with a valid template you will experience fewer problems, especially with IE. ;)

Your code example puts IE into "Quirks Mode" and it does not recognize margin:auto in this mode.

"Quirks Mode" is initiated in IE if the document does not have a full dtd

Here is a template which will ensure that IE will be in "Standards Compliance Mode"...

[blue]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">[/blue]
<html lang="en">
<head>

<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">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>untitled document</title>

<style type="text/css"></style>

<script type="text/javascript"></script>

</head>
<body>

<div></div>

</body>
</html>


birdbrain