html - 为什么环绕文本会导致 div 框向上移动?

标签 html css

我想要的实际上是有 1 行或最多 2 行文本,如果用省略号截断的话。它没有显示,并且出于某种原因,当它在 .cell div 中超过 2 行文本时,该框就会跳起来。

body {
  background-color: transparent;
  overflow-y: hidden;
  margin: 0;
}

div {
  font-size: 20px;
  text-overflow: ellipsis;
}

.container {
  white-space: nowrap;
  overflow-x: hidden;
  overflow-y: hidden;
  font-size: 0;
  height: 100vh;
}

.row {
  white-space: nowrap;
  overflow-x: hidden;
  font-size: 0;
  background-color: cyan;
}

.cell {
  display: inline-block;
  white-space: normal;
  width: 200px;
  height: 200px;
  background-color: lime;
  margin: 5px;
  padding: 0px;
  z-index: -1;
}

.cellDummy {
  display: inline-block;
  white-space: normal;
  height: 200px;
  background-color: blue;
  margin: 0px;
  padding: 0px;
  z-index: -1;
}
<div class="container">
  <div class="row" id="row0">
    <div class="cellDummy" style="width: 740.5px;">boo</div>
    <div class="cell">1 ahsdjkha sdjha kshd kajhs dajhsdk</div>
    <div class="cell">2</div>
    <div class="cell">3</div>
  </div>
</div>

enter image description here

所以问题是即使有 2 行文本,如何排列 .cell 框?如果文本不能容纳 2 行(或框高度的大约 10%),如何制作省略号?

更新 现在更改垂直对齐后 .cellDummy 框会跳起来。

enter image description here

最佳答案

因为内联元素的默认垂直对齐方式是 baseline,这就是您所看到的。尝试将其更改为 top:

body {
  background-color: transparent;
  overflow-y: hidden;
  margin: 0;
}

div {
  font-size: 20px;
  text-overflow: ellipsis;
}

.container {
  white-space: nowrap;
  overflow-x: hidden;
  overflow-y: hidden;
  font-size: 0;
  height: 100vh;
}

.row {
  white-space: nowrap;
  overflow-x: hidden;
  font-size: 0;
  background-color: cyan;
}

.cell {
  display: inline-block;
  white-space: normal;
  width: 200px;
  height: 200px;
  background-color: lime;
  margin: 5px;
  padding: 0px;
  z-index: -1;
  vertical-align:top;
}

.cellDummy {
  display: inline-block;
  white-space: normal;
  height: 200px;
  background-color: blue;
  margin: 0px;
  padding: 0px;
  z-index: -1;
}
<div class="container">
  <div class="row" id="row0">
    <div class="cellDummy" style="width: 740.5px;">boo</div>
    <div class="cell">1 ahsdjkha sdjha kshd kajhs dajhsdk</div>
    <div class="cell">2</div>
    <div class="cell">3</div>
  </div>
</div>

要设置省略号,您需要定义 text-overflowoverflow 属性。

关于html - 为什么环绕文本会导致 div 框向上移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43193714/

相关文章:

html - 如何在html表格的列中并排显示文本和图像

html - 无法格式化苹果网站的 "clone"

html - 我应该在 link 元素或其子元素 img 元素上使用 CSS transform 属性吗?

html - Word-wrap 不适用于 chrome 中 fieldset 内的 div

javascript - 隐藏下一个类名被多次使用的 Div?

html - 引导多选 js 中的问题

html - 绝对类在 IE 中不起作用

javascript - iCloud 用户名和密码自动完成提示

javascript - 如何移动多个div框的背景?

html - 倒 Angular/圆 Angular 图像 HTML