0

I have 2 divs and I want to align them in the same line. The html code is:

<div class="box">
<div class="boxleft"> </div>
<div class="boxright"> </div>
</div>

I tried 'float: left;' and 'float: right;' but the background is going crazy, it apears just on ~30px of the height. I tried to put a height('till then I didn't use height in CSS). It didnt' work. I tried 'display: inline-block' too, but without succes.

Thanks.

CSS:

.box {
width: 956px;
margin-left: auto;
margin-right: auto;
background: #584231;}

.boxleft {
width: 706px;
margin-right: auto;
border-right: 2px solid black;}

 .boxright {
width: 250px;
margin-left: auto;
float: right;}
1
  • 1
    inline-block ought to work; what's the rest of your CSS? Commented Jul 18, 2013 at 12:22

7 Answers 7

1

Float: left should do the trick depending on the width of the parent boxand the width of boxleft and boxright. If the parent box has width: 500px; and boxleft and boxrightboth have width: 250px; float:left;. You should be fine.

1

Have a look at the css properties float:left and clear:both.

http://www.w3schools.com/css/css_float.asp

1

I put some colors on each background to make it clear, you're maybe lacking a width and height for each element..

.boxleft , .boxright {
    float : left;
    width : 200px;
    height : 100px;
    margin : 10px;
}
.boxleft {
    background : yellow;
}
.boxright {
    background : blue;
}

http://jsfiddle.net/n9mHX/

1
  • You don't need to specify width and height in order to make float work. Commented Jul 18, 2013 at 12:32
0

On most modern browsers nowadays display: table-cell is the better alternative to floating.

0
0

You may use

display:inline-block;

or

float

or as per latest browser out you may use

display: table-cell

or you may use

clear: both
0

If you're not a "CSS guy", look at http://twitter.github.io/bootstrap/. With bootstrap, put two div on the same line is done this way :

<div class="row-fluid box">
    <div class="span6 boxleft"></div>
    <div class="span6 boxright"></div>
</div>
0

You need to clear the floats via clearfix on the parent container.

.box {
    width: 956px;
    background: #584231;
}

/* clearfix */
.box:after {
     content: '';
     display: block;
     clear: both;
     visibility: hidden;
     line-height: 0;
     height: 0;
}

.boxleft {
    width: 704px;
    border-right: 2px solid black;
    float: left;
}

.boxright {
    width: 250px;
    float: right;
}

The border is adding 2px to your divs width. That's why I specified it with 704px.

Using inline-block as display for the left and right box should work too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.