Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a div and make it expand around the divs inside of it?

Tags:

html

css

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

like image 802
Sruly Avatar asked May 20 '09 16:05

Sruly


People also ask

How do I center a div inside a div using transform?

To horizontally center a block element, such as a div or graphic, use the left or right properties in combination with the transform property. The left property shifts the element's left edge to the middle of the page. The transform property is then used with the translate function.

How do I center a nested div?

To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it.

How do you center align a span inside a div?

The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.

How do I center a div without flex?

Vertical align with a full screen across Tailwind CSS h-screen : To size the container height to the viewport height. justify-center : To justify center (horizontal center). items-center : To align the items to the center (vertical center). <div…


1 Answers

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>
like image 123
Guffa Avatar answered Sep 22 '22 17:09

Guffa