css - 减小宽度时 div 内文本的新行

标签 css

我在下面有这段代码。如您所见,div 容器的 width 为 40%。当我减小宽度时,绿色的 div 下降,使 foobar's 保持在同一行。我希望单词 (foobar2, foobar3) 会出现在 foobar1 下面。为什么会出现这种行为?

这是 jsfiddle .

<div style="width: 40%; background-color: blue">
  <div style="display: inline-block; background-color: red">hello</div>
  <div style="display: inline-block; background-color: green">foobar1 foobar2 foobar3 </div>
</div>

最佳答案

就是inline-block的使用。为此,您最好使用 <span>元素(相当于将其设置为 display: inline )。

<div style="width: 40%; background-color: blue">
  <span style="background-color: red">hello</div>
  <span style="background-color: green">foobar1 foobar2 foobar3 </div>
</div>

关于css - 减小宽度时 div 内文本的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190973/

相关文章:

jQuery addClass/removeClass 仅适用于 Firefox

html - 覆盖 PrimeNG 组件 CSS

jQuery:边框的高度()

html - 如何使该网格适合初始窗口大小,但允许用户缩放? (无 react )

HTML/CSS 添加视频到 slider

HTML 页面不适合移动设备屏幕

html - 标题 DIV 之外的标题文本

html - 如何显示 JPG 图像以便首先显示非常低的分辨率?

Javascript 图像 slider 高度问题

javascript - jquery添加的内联样式为空