2015-06-21 26 views
0

尽管我使用了float,但我的两个div类并不想并排排列。怎么做? 基本上整个宽度是520px,每个框的宽度是250px,在20px的框之间有一个空白。并列两个div类

<div id="car-box"> 
<div class="well-car"> 
       <div class="add_box"> 
        <h1 class="add_heading">car model</h1> 
       </div> 
       </div> 

       <div class="car-brand"> 
        <a class="button" href="www.placehold.it">car brand</a> 
       </div> 
</div> 

和CSS:

.car-box { 
    width:520px; 
    height:500px; 
    border:5px dashed blue; 
    margin-right:10px; 
    float:left 
} 

.well-car { 
    width:250px; 
    height:250px; 
    border:10px solid red; 
} 
.car-brand { 
    width: 250px; 
    height:250px; 
    border:10px dashed blue; 
    font-size: 20px; 
    float:left 
} 

这里小提琴...... Fiddle

+0

似乎每个人都应该是'#car-box'而不是'.car-box'或'class =“car-box”'而不是'id' - 你拥有它的方式你根本没有设计外部div,所以它不* 520 *宽(但这不是唯一的问题) – CupawnTae

回答

1

你的边框宽度被添加到内容的宽度。 250+2*10 + 250+2*10 == 540

(你可以在这里阅读https://developer.mozilla.org/en/docs/Web/CSS/box-sizing浏览器如何计算块元素的大小)

为您的自定义样式通常最好设置box-sizing: border-boxhttp://www.paulirish.com/2012/box-sizing-border-box-ftw/

编辑:是的,还float:left上正如其他人指出的那样,这个课程是.well-car

+0

是的,但'250 + 2 * 5 + 250 + 2 * 10 == 530' –

+0

哦,哎呀,我忘了边界是在双方。顺便说一下,计算将是'250 + 2 * 10 + 250 + 2 * 10 == 540'。但是,更新它仍然不能解决问题,原因有几个。 – Anonymous

+0

啊,是的,我瞥了一眼错误的边缘;)你是对的,它有更多的问题。 –

0

你只需要添加浮动:左;与您的分类“好车”。

.well-car { 
width:250px; 
height:250px; 
border:10px solid red; 
float: left; 
} 
0

你没有正确地浮动你的元素。类wellcar应浮动到left和类car-brand应漂浮到right。下面的代码应该可以工作。

#car-box { 
    width:520px; 
    height:500px; 
    border:5px dashed blue; 
    margin-right:10px; 
} 

.well-car { 
    width:250px; 
    height:250px; 
    border:10px solid red; 
    float: left; 
} 

.car-brand { 
    width: 250px; 
    height:250px; 
    border:10px dashed blue; 
    font-size: 20px; 
    float:right; 
} 
+0

谢谢。我编辑了我的答案来解决这个问题。 – 2015-06-21 19:53:55