Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center one <div> and float other right [duplicate]

Tags:

html

css

floating

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;
}
like image 220
hplodnarmas Avatar asked Oct 19 '12 04:10

hplodnarmas


People also ask

How do I center a div with float?

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.

How do I center two divs side by side?

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.

How do I center a div on top of another div?

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.

How do you float a div to the right?

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.


2 Answers

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

like image 131
rlay3 Avatar answered Oct 22 '22 02:10

rlay3


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

like image 2
user10 Avatar answered Oct 22 '22 02:10

user10