html - 由于边框, float 的 div 不能彼此相邻

标签 html css

<div id="container">
    <div id="col1">menu1</div>
    <div id="col2">menu2</div>
    <div style="clear: both;"></div>
<div>

#container
{
    width: 400px;
    background-color: green;
}

#col1
{
    float: left;
    background-color: red;
    width: 48%;
    border: 1px dotted blue;
}

#col2
{
    float: left;
    background-color: yellow;
    width: 52%;
    border: 1px dotted blue;
}

http://jsfiddle.net/4ZQpv/1/

这可能非常简单,但我想不通。当我没有 border 时,它工作正常。但是一旦我添加了一个 border,这两个 div 就不能紧挨在一起了。但我不能说“52% - 1px - 1px”之类的东西来消除边框。

最佳答案

你应该添加 box-sizing: border-box; 到每个列

一个例子:http://jsfiddle.net/4ZQpv/4/

不要忘记 box-sizing 的前缀。

关于html - 由于边框, float 的 div 不能彼此相邻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21530440/

相关文章:

html - 100% 高度与 twitter bootstrap 和 container-fluid

html - 从右到左打印表格单元格

html - 位置 :absolute in Firefox

javascript - 在 HTML 中调用 AngularJS 函数

javascript - 如何使用 HTML 和 CSS 模拟从图像/div 到另一个图像/div 的光线

html - 无论浏览器的大小如何,文本居中并在页面上对齐

html - 右对齐文本并将其旋转到右边缘

css - 如何使用渐变和图像过渡背景图像?

html - Bootstrap 中的偏移列

jquery - 为什么在打印区域中省略了 css