html - 两个带文本的内联 block 自动换行

标签 html css

我有两个内联 block 样式的 div,它们都包含具有不同字体大小的文本。它们应该在同一行上,然后第二个应该断开,但只有当第二个 div 比 body 的宽度短时它们才会断开。

JSFiddle

div {
  display: inline-block;
  font-family: Arial;
}
#a {
  font-size: 30px;
  margin-right: 50px;
}
#b {
  /* No new line at b!! */
  font-size: 40px;
}
<div id="a">Fruits:</div>
<div id="b">Banana (3), Apple (2), Kiwi (5), Orange (1), Pear (11)</div>

这里我说明问题:

看起来的样子:

Fruits:
Banana (3), Apple (2), Kiwi (5), Orange (1), Pear (11)

应该的样子:

Fruits:  Banana (3), Apple (2), Kiwi (5),
         Orange (1), Pear (11)

再一次,再举一个例子,说明它不应该的样子,但如果 div 是内联的,它可以:

Fruits:  Banana (3), Apple (2), Kiwi (5),
Orange (1), Pear (11)

我能解决这个问题吗?

我尝试过使用 white-space: nowrap 和 word-wrap: break-word,但它们似乎都不起作用。

最佳答案

你可以使用display: table-cell,但是你应该使用padding而不是margin,因为:

Margin applies to all elements, except elements with table display types other than table-caption, table and inline-table.

JSFiddle

div {
  display: table-cell;
  font-family: Arial;
}
#a {
  font-size: 30px;
  padding-right: 50px;
}
#b {
  /* No new line at b!! */
  font-size: 40px;
}
<div id="a">Fruits:</div>
<div id="b">Banana (3), Apple (2), Kiwi (5), Orange (1), Pear (11)</div>

关于html - 两个带文本的内联 block 自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34909634/

相关文章:

javascript - 将无序列表(ul 标签)导航转换为下拉导航(选择标签)

html - Div 仅在 Firefox 中向右移动

javascript - 如果不是图像,如何让 HTML 元素重复以适合整个背景

php - 在对话框 jquery 中加载 php 页面时,CSS 不应用于表单元素

javascript - 当使用 id 而不是值单击单选按钮时,jQuery 显示 div

html - 按标签和类别抓取数据

javascript - Uncaught ReferenceError : changeLayer is not defined

html - 如何用百分比将页面分成两个区域(div)

css - 类等于另一个类

javascript - impress.js - 元素符号列表