0

Possible Duplicate:
How to align a <div> to the middle of the page

I have a div tag with hight set to 800px, I want that when the browser width is greater than 600px it shouldn't stretch the div but it should bring it to the middle of the page

How can I achieve this?

can I use the following code? centered content

Community
  • 1
  • 1
chetan
  • 51
  • 3
  • 8

2 Answers2

1

Short Answer:

with margin set to auto

<html>
<head><title>Your Title</title></head>
<body>
<div style="width:600px; margin:auto; border:1px solid red">
</div>
</body>
</html>

Long answer:

You might want to set an id for that div, and give appropriate css selector with the rules rather than using inline style like that.

Also, for that to work correctly in ie 6 and 7, you need to give the doctype declaration, otherwise it won't work because ie will work in quirks mode http://www.satzansatz.de/cssd/quirksmode.html

So the complete answer should look like

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
       <title>Your Title</title>
       <style>#container { width:600px; margin:auto; border:1px solid red }</style>
    </head>
    <body>
       <div id="container">
       </div>
    </body>
    </html>
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
0

put in the css the following properites :

max-width:600px;
position:relative;
margin:auto;

one thing I am not clear is do you want the div to take the width of the browser if the browser width is less than 600px?

Tejasva Dhyani
  • 1,342
  • 1
  • 10
  • 19