简体   繁体   English

具有“空白:普通”的内联块div超过具有“空白:nowrap”的父级的宽度

[英]An inline-block div with “white-space: normal” exceeds the width of a parent with “white-space: nowrap”

I'm trying to position a few elements in a row, so that they all fit in the width of the container. 我试图将一些元素连续放置,以使它们都适合容器的宽度。 To prevent them from word-wrapping I added "white-space: nowrap" to the parent, and added "white-space: normal" to the children to allow them to wrap the text (as desired). 为了防止他们换行,我在父级中添加了“ white-space:nowrap”,并在子级中添加了“ white-space:normal”,以允许他们包装文本(根据需要)。

The problem is that with this configuration the right most child sometimes exceeds the width of the parent. 问题在于,使用此配置,最右边的孩子有时会超过父孩子的宽度。

预期与实际

HTML: HTML:

<div id="container">
    <div class="child">
        child 1
    </div>
    <div class="child">
        child 2 text that might be long enough to wrap, but still exceed the parent
    </div>
</div>

CSS: CSS:

#container {
    white-space: nowrap;
    width: 200px;
    background: yellow;
    border: 1px solid brown;
    padding: 5px;
}

.child {
    display: inline-block;
    border: 2px solid red;
    vertical-align: top;
    white-space: normal;
}

http://jsfiddle.net/7e5TU/1/ (change the length of the text if the problem doesn't appear straight away). http://jsfiddle.net/7e5TU/1/ (如果问题没有立即出现,请更改文本的长度)。

I know that I can solve it with a table, and probably with a float on the left child, and "overflow: hidden" on the right, but I see no reason why this should not work. 我知道我可以用一个表来解决它,可能在左子项上使用一个浮点数,在右边使用“ overflow:hidden”,但是我看不出为什么这不起作用。

Could anyone provide some insights? 谁能提供一些见解? I'd mostly love to understand what in the box model causes this behavior. 我主要是想了解框内模型导致此行为的原因。 Thanks! 谢谢!

I agree with @hashem That's the expected behavior. 我同意@hashem这是预期的行为。 By using white-space: nowrap; 通过使用white-space: nowrap; for the parent, you've collapsed the whitespaces between inline(-block) elements. 对于父级,您已经折叠了inline(-block)元素之间的空格。 white-space treats the children, not the element itself. white-space对待孩子,而不是元素本身。

Well if you still need a fix you can add width to second child to make it fit inside container . 好吧,如果您仍然需要修复,则可以增加第二个孩子的width以使其适合container

fiddle 小提琴

eg 例如

.child2
{
    width: 70%;
}

If you are willing to use flexbox ( https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes ) you could do it like this: http://jsfiddle.net/7e5TU/6/ 如果您愿意使用flexbox( https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes ),可以这样做: http : //jsfiddle.net/7e5TU/6 /

HTML 的HTML

<div id="container">
    <div class="child1">
        child 1
    </div><div class="child2">
        child 2 text that might be long enough to wrap, 
        but still exceed the parent
    </div>
</div>

CSS 的CSS

#container {
    width: 200px;
    background: yellow;
    border: 1px solid brown;
    padding: 5px;
    display: flex;
    flex-direction: row
}

.child1, .child2 {
    display: block;
    border: 1px solid red;
    vertical-align: top;
}

.child1 {
    min-width: 50px;   
}

You can do this with CSS display:table. 您可以使用CSS display:table执行此操作。 This way no sizing details are needed. 这样就不需要尺寸详细信息。 It ensures the elements stay in a row and the text will wrap perfectly to the width of the parent container. 这样可以确保元素排成一行,并且文本将完美地环绕到父容器的宽度。

HTML 的HTML

<div class='container'>
  <div class='field'>
    <div class='data'>
      child 1
    </div>
  </div>
  <div class='field'>
    <div class='data'>
      child 2 text that might be long enough to wrap, but still exceed the parent
    </div>
  </div>
</div>

CSS 的CSS

.container {
  display: inline-table;
  border-spacing: 4px;
  background: yellow; border: 1px solid brown;
  padding: 5px;
}
.field {
  display: table-cell;
}
.data {
  border: 2px solid red;
  padding: 3px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM