为什么内联代码块div会在悬停时更改位置?

时间:2019-05-02 07:35:37

标签: html css

我以内联块样式并排放置了两个div。使用纯CSS将悬停上的溢出从隐藏更改为可见时,为什么div会更改位置?

.overlaping {
  width: 14.2%;
  height: 50px;
  font-size: 1rem;
  display: inline-block;
  text-align: center;
  line-height: 200%;
  color: black;
  position: relative;
  background: yellow;
  overflow: hidden;
}

.overlaping:hover {
  overflow: visible;
}

.wrapper {
  height: 200px;
  width: 100%;
  background: lightblue;
}
<div class="wrapper">

  <div class="overlaping">
    Some longer text
  </div>

  <div class="overlaping">
    Other div
  </div>

</div>

我知道内嵌块是造成这种情况的原因,但是有什么方法可以减轻位置变化并同时保持显示内嵌块的作用?

3 个答案:

答案 0 :(得分:3)

默认情况下,内联块具有vertical-align: baseline,因此如果另一个高度发生变化,它会跳来跳去。要解决此问题,请添加

.overlaping {
  vertical-align: top;
}

答案 1 :(得分:0)

可能您应该更改height而不是overflow设置。

还将min-heightfloat添加到框中。

.overlaping{
  width: 14.2%;
  min-height: 50px;
  height: 50px;
  font-size: 1rem;
  display: inline-block;
  text-align: center;
  line-height: 200%;
  color: black;
  position: relative;
  background:yellow;
  overflow:hidden;
  float: left;
}

.overlaping:hover{
  height: auto;
}

.wrapper{
  height:200px;
  width:100%;
  background:lightblue;
}
<body>
<div class="wrapper">

<div class="overlaping">
Some longer text ddg dfg sdfg sdfg sdfgsdfgsdfgsdfg sdfgsdfgsd fgsd fgsd fgsdfgsdfgs dfg sert sertsertsertgs dfgsdfg dfgsdfg
</div>

<div class="overlaping">
Other div
</div>

</div>
</body>

答案 2 :(得分:-1)

如下所示修改.overlapping样式

.overlaping {
  width: 14.2%;
  height: 50px;
  font-size: 1rem;
  float:left;
  margin-right:5px;
  text-align: center;
  line-height: 200%;
  color: black;
  position: relative;
  background: yellow;
  overflow: hidden;
}