views:

1633

answers:

4

I have a page that I want centered, with a background and a border around the entire content.

I made a div and set it with the background color and the border settings I want.

Problem is that it has divs inside it that float and the background does not stretch around the floating divs. The only way I was able to get it to work was by setting position:absolute. Then the border did expand around the floating divs but I was unable to center them using regular html/css.

I found a javascript hack to make it center but it only centers after the page loads and it looks bad.

I am sure there is a way to have the container expand and still have it centered, I just can't figure it out.

here is a sample html page that shares my problems

<div style="background-color: Red; width: 980px; position: absolute;" id="container">
    <br />
    <br />
    <br />
    <br />
    <div style="width: 400px; background-color: Black; float: left;">
        <br />
        <br />
    </div>
    <div style="width: 400px; background-color: Blue; float: left;">
        <br />
        <br />
    </div>
</div>

and here is the Javascript that makes it work (uses Jquery)

$(function() {
        var winH = $(window).height();
        var winW = $(window).width();
        $("#container").css('left', winW / 2 - $("#container").width() / 2);
    });

There has got to be a better way.

Thanks

A: 

this is a tricky one, but I use the following code in stylesheets;

<style>div.sentor {text-align: center; height: 100%;} div.child{margin-left: auto; margin-right: auto; width: 95%; height: 100%;}</style>

then in the body;

<div class="sentor">
<div class="child">
 <div id="mycontent">

this should do the trick

simon622
+6  A: 

Use auto as left and right margin to center the element.

Put an element after the floating elements and use clear to put it below them. As it's not floating, it will affect the size of the outer div.

<div style="margin: 0 auto; background-color: Red; width: 980px;" id="container">
    <br />
    <br />
    <br />
    <br />
    <div style="width: 400px; background-color: Black; float: left;">
        <br />
        <br />
    </div>
    <div style="width: 400px; background-color: Blue; float: left;">
        <br />
        <br />
    </div>
    <div style="clear: both; height: 0; overflow: hidden;"></div>
</div>
Guffa
Thanks. I figured there was a way, But I just couldn't figure it out.
Sruly
A: 

Maybe I'm not understanding your question well enough, but I think you're looking for something similar to this. Here's our simple layout:

<html>
    <head>
        ...
    </head>

    <body>

        <div id="center">
            <!-- your content -->
        </div>

    </body>

</html>

To get the contents of div#center to move to the center of the page, we need the following CSS:

body {
    text-align: center;
}

div#center {
    text-align: left;
    width: 600px;    // Or some other arbitrary width
    margin: 0 auto;
}

This will center everything within body. However, the text-align: center; property will also be applied to the children of body, so we cancel that out with the second style rule. Alternatively, in place of div#center, you could use the following style rule:

body * {
    text-align: left;
}
Andrew Noyes
This uses a bug in Internet Explorer. It doesn't work in any other browser, as they follow the standard and don't apply text-align to block elements.
Guffa
Yeah I left out the margin: 0 auto; part. I had forgotten about that. I've even incorporated it into numerous works of my own!
Andrew Noyes
A: 

Use:

<div style="margin:0 auto; overflow:hidden; background-color: Red; width: 980px; id="container">

The margin: 0 auto; will center the red div.

The overflow: hidden; will make the red div stretch to contain the two floating divs.

Emily