内联块元素是线破坏似乎没有理由? [英] inline-block elements are line breaking for seemingly no reason?

查看:104
本文介绍了内联块元素是线破坏似乎没有理由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常基本的HTML / CSS不能正常工作。基本上我有我的身体设置为400px宽。然后我有两个内部的身体与显式宽度为300px和100px的div。此外,这两个div都设置为 display:inline-block 。由于某些原因,100px的div突破了body的内容区域,并出现在它的下面。我不知道为什么会发生这种情况。如果我将宽度从100px设置为96px,它的工作原理。但是,如果我将它设置为97px,98px,99px或回到100px,它不工作。我发现这种行为很奇怪。有人可以解释发生了什么问题吗?

I have some pretty basic HTML/CSS that isn't working as I expect. Basically I have my body setup to be 400px wide. I then have two divs inside of the body with explicit widths of 300px and 100px. Additionally, both of these divs are set to display: inline-block. For some reason, the 100px div breaks out of the body's content area and appears below it. I don't know why this is happening. If I set the width from 100px to 96px, it works. However, if I set it to 97px, 98px, 99px, or back to 100px, it doesn't work. I find this behavior very odd. Can someone explain what is going wrong?

请注意,我正在Chrome(测试版)上测试此功能。代码如下。

Note that I am testing this on Chrome (Beta Channel). Code is below.

CSS:

body {
    margin: 4px;
    width: 400px;
    height: 250px;
    border: 1px solid black;
}

.list-container {
    display: inline-block;
    width: 300px;
    height: 100%;
    background-color: red;
}

.button-container {
    display: inline-block;
    width: 100px;
    height: 100%;
    background-color: blue;
}

HTML:

<body>

<div class="list-container">
</div>

<div class="button-container">
</div>

</body>


推荐答案

如果你从两个 div 元素,一切正常:

If you remove the line-breaks from between the two div elements, everything's fine:

<div class="list-container">
</div><div class="button-container">
</div>

JS Fiddle演示

您也可以注释掉 div s:

<div class="list-container">
</div><!--

--><div class="button-container">
</div>

JS Fiddle demo

或者将 font-size $ c> body 元素(但是你必须为子元素重新定义它:

Or even set the font-size to zero for the body element (but you'll have to redefine it for the child elements, obviously:

body {
    margin: 4px;
    width: 400px;
    height: 250px;
    border: 1px solid black;
    padding: 0;
    font-size: 0;
}

JS Fiddle demo

这篇关于内联块元素是线破坏似乎没有理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆