理解CSS文本位置

时间:2016-05-22 11:20:48

标签: html css vertical-alignment

我想垂直对齐文本相对于它旁边的div位置,如下所示: enter image description here

但是在尝试这样做时,我注意到包装文本和立方体的包装div的高度比它应该高一点,如下所示: https://jsfiddle.net/x7sr9mb1/1/



.table-keys-wrapper {
  display: inline-block;
}

.key-color {
  display: inline-block;
  background: black;
  width: 20px;
  height: 20px;
}

<div class="table-key">
  <div class="table-keys-wrapper">
    <span id="key-sow" class="key-color"></span>
    <span class="key-text">Sowing</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-plant" class="key-color"></span>
    <span class="key-text">Planting</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-harvest" class="key-color"></span>
    <span class="key-text">Harvesting</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-storage" class="key-color"></span>
    <span class="key-text">Storage</span>
  </div>
</div>
&#13;
&#13;
&#13;

结果:

enter image description here

为什么外部div不能获得立方体的高度,因为它的高度设置为height: auto;(默认值)

另一个问题是,将文本相对于立方体垂直对齐的最佳方法是什么?

非常感谢帮助

3 个答案:

答案 0 :(得分:2)

这是因为.key-text范围默认为vertical-align:baseline

如果您要向孩子添加vertical-align:middle,则会占用父母的全部身高。

&#13;
&#13;
.table-keys-wrapper{
  display: inline-block;
}

.table-keys-wrapper * {
  vertical-align:middle;  
}
.key-color{
  display: inline-block;
  background: black;
  width: 20px;
  height: 20px;
}
&#13;
<div class="table-key">
  <div class="table-keys-wrapper">
    <span id="key-sow" class="key-color"></span>
    <span class="key-text">Sowing</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-plant" class="key-color"></span>
    <span class="key-text">Planting</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-harvest" class="key-color"></span>
    <span class="key-text">Harvesting</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-storage" class="key-color"></span>
    <span class="key-text">Storage</span>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

inline-block个元素就像文字一样,位于baseline

baselinevertical-align的默认值。

将此值重置为vertical-align:middle框的inline-block

.key-color{
  display: inline-block;
  vertical-align:middle;/* reset */
  background: black;
  width: 20px;
  height: 20px;
}

答案 2 :(得分:0)

让我们尝试位置:绝对;

&#13;
&#13;
.table-keys-wrapper{
  position: relative;
  display: inline-block;
}

.key-color{
  display: inline-block;
  background: black;
  width: 20px;
  height: 20px;
  position: absolute;
}

.key-text{
  padding-left: 22px;
}
&#13;
<div class="table-key">
  <div class="table-keys-wrapper">
    <span id="key-sow" class="key-color"></span>
    <span class="key-text">Sowing</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-plant" class="key-color"></span>
    <span class="key-text">Planting</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-harvest" class="key-color"></span>
    <span class="key-text">Harvesting</span>
  </div>
  <div class="table-keys-wrapper">
    <span id="key-storage" class="key-color"></span>
    <span class="key-text">Storage</span>
  </div>
 </div>
&#13;
&#13;
&#13;