css - DIV 之间的空白会破坏布局

标签 css html

假设我有以下 HTML/CSS 包含两列 DIV,每列 50 像素宽。

<div style="width:100px;margin:0px;">
    <div style="width:50px; display:inline-block;">A</div><!-- HERE -->
    <div style="width:50px; display:inline-block;">B</div>
</div>

在这段代码中,如果我在两个内部 div 之间没有任何空格(即 <!-- HERE --> 所在的位置):

<div style="...">
    <div style="..."></div><div style="..."></div>
</div>

然后 HTML 按预期呈现为两列。但是,如果我在它们之间有任何空白,它们不会并排呈现,因为空白字符占用非零宽度。

对此的明显解决方案是在内部 DIV 之间不要有任何空格,但是,我使用 HTML 自动格式化程序来格式化我的 HTML 代码,它会自动在两个内部 DIV 之间插入空格。

是否有任何解决方案,使两个内部 DIV 按预期呈现,即使它们之间有空格?

最佳答案

添加white-space:nowrap; word-spacing:0;100px 容器

没有 float 定位

<div style="width:100px;margin:0px;white-space:nowrap; word-spacing:0;">
    <div style="width:50px; display:inline-block;background:#efefef;">A a</div><!-- HERE --><div style="width:50px; display:inline-block;background:#ccc;">B b</div>
</div>

这是 fiddle :http://jsfiddle.net/3yQ6L/2/

关于css - DIV 之间的空白会破坏布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5658409/

相关文章:

html - 使用 CSS 使图像消失而无需悬停

html - <caption> 元素可以包含链接和标题吗?

javascript - 使用 Splinter 查找按钮

javascript - 鼠标移出时动态 jQuery 隐藏 div

html - 全宽水平导航栏,悬停时全宽水平下拉菜单

javascript - 使用 href 到 div 的 id

javascript - 非常简单的 jQuery .load 示例不起作用

php - 为桌面版和移动版使用不同的 HTML 代码

html - Windows 移动 10 : white text in option tags are invisible

html - 为什么仅单击一个链接时所有链接都会改变颜色?