如何防止仅在悬停的行上进行换行?

时间:2017-01-03 22:42:06

标签: html css

如果你将鼠标悬停在"这是破碎的!"标签,相邻的"该死的"标签换行到下一行。这是因为"这是破碎的!"标签的大小增加,并受到容器的限制。

是否可以防止这种包装?

标签和父容器大小是动态的,所以我不能轻易地将一行标签组合在一个包含元素的区域中,该元素具有" white-space:nowrap"属性。悬停在任何标记上的任何标记或任何相邻标记都可以是包含到下一行的标记。如果可能的话,我希望在悬停之前和之后保留标签之间的相同空间。



main {
  width: 160px;
}
.material-tag-wrapper {
  margin: 1px 3px 1px 0;
  display: inline-block;
}
.material-tag-wrapper.closeable:hover .material-tag {
  padding-right: 26px;
}
.material-tag-wrapper.closeable:hover .material-tag-remove {
  opacity: 1;
  padding-right: 10px;
}
.material-tag {
  background-color: blue;
  color: white;
  font-weight: 400;
  cursor: pointer;
  border-radius: 3px;
  padding: 2px 8px;
  text-transform: uppercase;
  font-size: 10px;
  transition: .1s all;
  display: inline-block;
  position: relative;
  text-align: center;
}
.material-tag-remove {
  color: #3C4858;
  cursor: pointer;
  font-size: .8em;
  position: absolute;
  top: 4px;
  right: 0;
  opacity: 0;
  text-align: right;
  text-decoration: none;
  width: 100%;
  z-index: 2;
}
.material-tag-remove:before {
  content: "x";
  color: white;
}

<main>
  <div class="material-tag-wrapper closeable">
    <div class="material-tag">
      Beautiful
      <div class="material-tag-remove"></div>
    </div>
  </div>

  <div class="material-tag-wrapper closeable">
    <div class="material-tag">
      Works
      <div class="material-tag-remove"></div>
    </div>
  </div>
  <div class="material-tag-wrapper closeable">
    <div class="material-tag">
      This is Broken!
      <div class="material-tag-remove"></div>
    </div>
  </div>

  <div class="material-tag-wrapper closeable">
    <div class="material-tag">
      Damn
      <div class="material-tag-remove"></div>
    </div>
  </div>

  <div class="material-tag-wrapper closeable">
    <div class="material-tag">
      I Work!
      <div class="material-tag-remove"></div>
    </div>
  </div>
</main>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

自从entity添加padding-right: 26px;后,您可以对material-tag-wrapper:hover(或您选择的任何包装)应用相同的规则:

main

enter image description here