tags:

views:

401

answers:

3

I have to use a float div (the main window of my application), and I'd like to center that floated DIV based on the client's screen width. How can I accomplish that?

Right now I'm using a left:20% but that's not always centered depending on the user's screen resolution

A: 

You can try to use

.mainWindow {
    margin: 0 auto;
}

then make sure the parent element is text-align: center;

jkelley
`text-align: center` will not work in modern browsers to center elements. only text.
Jason
@Jason That's not entirely true; if you set the `display` of the element to `inline-block` or `inline` it will behave as the text does.
Nathan Kleyn
A: 

I usually use an auto centered container div and then put any other containers (like your floated div) inside that container. Is there any particular reason you can't do that?

Example CSS:

#container {
width: 960px;
margin: 0 auto;
position: relative;
}
Shea Daniels
Yes I can't use static widths on any container...that's why I use a float:left element, so the DIVs can grow automatically. So if I used an outer centered container it would have to grow along with the inner divs
Albert
Interesting requirement, I've never heard of anything quite like that before.
Shea Daniels
+2  A: 
Kevin C.