css - 向 div 添加文本使 div 向下移动

标签 css

我有以下代码,它并排显示 7 个 div,类似于条形图。

.wrapper {
  width: 100vw;
  height: 50vw;
  text-align: center;
}

.bar {
  width: 50px;
  height: 50px;
  margin: 10px;
  background: red;
  display: inline-block;
}
<div class="wrapper">
  <div class="bar" style="height: 200px"></div>
  <div class="bar" style="height: 130px"></div>
  <div class="bar" style="height: 180px"></div>
  <div class="bar" style="height: 230px"></div>
  <div class="bar" style="height: 150px"></div>
  <div class="bar" style="height: 160px"></div>
  <div class="bar" style="height: 190px"></div>
</div>

但是,当我在其中一个 div 添加一些文本时,整个 div 会向下移动。

.wrapper {
  width: 100vw;
  height: 50vw;
  text-align: center;
}

.bar {
  width: 50px;
  height: 50px;
  margin: 10px;
  background: red;
  display: inline-block;
}
<div class="wrapper">
  <div class="bar" style="height: 200px"></div>
  <div class="bar" style="height: 130px"></div>
  <div class="bar" style="height: 180px">some text</div>
  <div class="bar" style="height: 230px"></div>
  <div class="bar" style="height: 150px"></div>
  <div class="bar" style="height: 160px"></div>
  <div class="bar" style="height: 190px"></div>
</div>

如何阻止 div 向下移动?

最佳答案

只需设置栏的vertical-align,问题就应该得到解决。

解释:当垂直对齐设置为与基线相关的任何内容时,就会出现此问题,因此:baselinesupsub、长度和百分比值,以及默认值(基线)

解决此问题的值为:middletopbottomtext-top文本底部。您可以从其中进行选择,但您很可能只需要前三个。

.wrapper {
  width: 100vw;
  height: 50vw;
  text-align: center;
}

.bar {
  width: 50px;
  height: 50px;
  margin: 10px;
  background: red;
  display: inline-block;
  vertical-align: bottom;
}
<div class="wrapper">
  <div class="bar" style="height: 200px"></div>
  <div class="bar" style="height: 130px"></div>
  <div class="bar" style="height: 180px">some text</div>
  <div class="bar" style="height: 230px"></div>
  <div class="bar" style="height: 150px"></div>
  <div class="bar" style="height: 160px"></div>
  <div class="bar" style="height: 190px"></div>
</div>

关于css - 向 div 添加文本使 div 向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63637527/

相关文章:

html - 不包含水平溢出属性的文本

html - web2py MENU 在 bootstrap 样式中,我可以在哪里更改菜单项的文本不再是大写?

javascript - mvc 4 razor selectpicker 中的下拉菜单问题用于从隐藏的 Select 中获取下拉菜单中的数据

javascript - 如何制作div的 slider ?

css - 如何使我的圆 Angular 框交替线条颜色流向边框

html - 使用 Perl 从 HTML 文件中删除内联 CSS

javascript - 使用 css :nth-child selector 对列表项进行样式化

jquery - 悬停第 nth-child(1) 时更改其他 child

css - jQuery Mobile Radio 字段集具有不同的样式

asp.net - 如果 CSS 或 Asp.net 中的文本太宽,则剪切文本