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
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.
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.
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.
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…
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With