Possible Duplicate:
Centering one div while another div is floated to the right?
I'm trying to center container1 and float container2 to its right so that it's flowing off of the page slightly:
Example: http://i.imgur.com/JHkfn.jpg
Unfortunately, container2 is hopping below and to the right of container1, as you can see in the site mock-up (link right below.)
SITE MOCK-UP: http://ad313.samrandolphdesign.com/
CSS:
#container1 {
margin: auto;
background-color: #FFF;
width: 80%;
height: 100%;
max-width: 600px;
padding-bottom: 15px;
display: block;
}
#container2 {
background-color: #CC9900;
max-width: 600px;
width: 80%;
height: 100%;
padding-top: 60px;
padding-bottom: 50px;
text-align: center;
float: right;
display: block;
}
You can set float div center by using margin property. you does not need to use float property but you can use margin property and set left or right margin value auto by this way.
The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.
Set the margin property to "auto" to horizontally center the <div> element within the page. The "margin: 0 auto" does the actual centering. Set your preferred colors for the outer and inner <div> elements by using the background-color property.
Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.
Try using absolute positioning instead of floating.
Something like:
#container1 {
margin: auto;
background-color: red;
width: 50%;
height: 100%;
max-width: 600px;
padding-bottom: 15px;
text-align: center;
display: block;
position: absolute;
left: 25%;
}
#container2 {
background-color: #CC9900;
max-width: 600px;
width: 50%;
height: 100%;
padding-top: 60px;
padding-bottom: 50px;
text-align: center;
display: block;
position: absolute;
right: -25%;
}
Here is a jsfiddle
EDIT: If you don't want absolute positioning for container1, just add top: 0;
to container2
wrap two divs inside another div. and make container 1 and container 2's display as inline-block.
something like this.
<div style="width: 2000px">
<div id="container1" style="width: 990px; display: inline-block">
</div>
<div id="container2" style="width: 990px; display: inline-block">
</div>
</div>
try this fiddle
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