【问题标题】:White-space: nowrap vs white-space: normal why is the effect on floating elements so big?White-space: nowrap vs white-space: normal 为什么对浮动元素的影响这么大?
【发布时间】:2012-09-13 13:35:07
【问题描述】:

Definition from w3c正常 空白序列将折叠成一个空白。文本将在必要时换行。这是默认播放 » nowrap 空白序列将折叠成一个空白。文本永远不会换行到下一行。文本在同一行继续,直到遇到
标记。

那么为什么浮动元素的外观会有这么大的不同呢?

例如比较一下:

JsBin with white-space normal showing a nicely aligned layout

<table>
    <thead>
      <tr>
        <th>
          <div style="background-color: lightblue; width: 600px; white-space: normal;">
            <span style="display: inline-block; height:20px; background-color: red; margin: 5px 3px;float: left;">
              Button
            </span>
            <span style="display: inline-block; height:20px; background-color: red; margin: 5px 3px;float: none;">
              Button
            </span>
            <span style="display: inline-block; height:20px; background-color: red; margin: 5px 3px; float:right;">
              Button right
            </span>
            <div style="clear: both" />
        </div>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr><td>note: white-space is normal here</td></tr>
    </tbody>
  </table>

White this where white-space: nowrap is used

和上面的代码一样,只是这次 white-space: nowrap;

有人知道了吗? [编辑] 正如人们评论的那样,他们看不出有什么区别,我上传了有问题的空白的屏幕截图:nowrap 我在 Firefox 10.0.4

【问题讨论】:

  • 你为什么要做white-space: nowrap;?没有它也可以工作:little link.
  • 我实际上没有看到任何区别。此外,您没有任何可以首先换行的文本。除非您的文本通常会换行到另一行,否则您不会看到差异。
  • white-space: nowrap对于使用text-overflow: ellipse也很重要
  • @Scrimothy text-overflow: ellipse; 在带有浮动 divs 的元素上?我不明白这一点。
  • @Abody97,在这种情况下你不需要它。我只是把它扔进去了,因为总体问题是关于 white-space:nowrapwhite-space:normal 之间的差异。

标签: css alignment


【解决方案1】:

我有点收回我上面最初的评论。如果您在 white-space: nowrap div 中有 divs floated 并且您的父 div 具有固定宽度,您会得到我在评论中提到的内容。但是,如果您的孩子是inlineinline-block,那么那些孩子divs 将继续超出右侧overflow(就像内联文本所做的那样)。通常,当您float: left 时,子代到达父代的末尾并换行。

看看这个fiddle 插图。

【讨论】:

  • 我接受这个答案,因为它是一个非常好的小提琴,而且我的问题似乎是特定于浏览器的。
【解决方案2】:

white-space: nowrapfloat: right 是 Firefox 的已知“错误”:https://bugzilla.mozilla.org/show_bug.cgi?id=488725

我在菜单中询问有关右对齐三角形的问题:Align to right "left triangle" in menu element。我准备使用padding-right、否定margin-leftposition: absolute 的修复程序。

【讨论】:

    【解决方案3】:

    我想添加这个 http://jsfiddle.net/cstephe/tNHLL/ 显示顺序如何在 Firefox 10 中也影响到这一点,至少当我回到家使用更新的版本时会检查这个。

    编辑:这就是我在本地的样子。

     <h2>float first: nowrap / Children: floating blocks</h2>
    <div class="wrapper nowrap">
         <div class="inlineBlock floating"></div>
         <div class="inlineBlock floating float-right">r</div>
         <div class="inlineBlock">something</div>
    
    </div>
    <div class="clearfix"></div>
    <h2>typical order: nowrap / Children: floating blocks</h2>
    <div class="wrapper nowrap">
         <div class="inlineBlock floating"></div>
    
         <div class="inlineBlock">something</div>
        <div class="inlineBlock floating float-right">r</div>
    </div>
    

    【讨论】:

    • 在 chrome 22 和 firefox 10 中看起来是一样的。
    • 我会在我的家用电脑上进行测试,但到目前为止,我已经看到了 ff10 和移动 safari 的区别
    • 在 chrome 版本 22.0.1229.94 m 和 ff 10.0.9 中对我来说看起来一样:对你来说有什么不同?你到底是什么意思?
    • 这可能是 FF 10.0.10 特有的东西,我一直忘记在家查看。我想我们可能能够摆脱的一件事是,如果你看到像这样的行为改变订单可能会为你做这件事,但情况很糟糕。
    猜你喜欢
    • 2014-04-03
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2023-03-14
    • 2017-08-11
    相关资源
    最近更新 更多