【问题标题】:Stop text horizontal overflow停止文本水平溢出
【发布时间】:2021-11-07 07:17:52
【问题描述】:

我有一个div,这个div 里面是文本,这个文本水平溢出,我想停止它。

不幸的是,这比最初听起来要复杂一些,因为我想创建一个有两列的div,并且我希望右列比左列宽 30px。

我知道我可以通过创建两个divs 来解决这个问题,但是如果有一种方法可以用一个div 来解决这个问题,那就太棒了。通过使用父元素和子元素并将margin-right: -30px; 添加到子元素,我或多或少能够做到这一点,但不幸的是,即使它存在,这个边距似乎也塌陷到子元素中并且文本完全忽略它。

如果我能解决这个问题,我会节省很多工作,有什么想法吗?

小提琴:https://jsfiddle.net/qxnc3fts/3/

-> 目标: 确保文本不会溢出浅灰色div

编辑: 有一个类似的问题:Is there a way to specify different widths for columns in CSS3?,@Quentin 有一个很好的答案肯定对我有帮助,但这个答案不会使文本中断,我希望发生。 ——但这似乎是不可能的。至少在当前的 CSS 版本中。

.ref {
  width: 200px;
  height: 10px;
  background-color: black;
}

.parent {
  background-color: lightgrey;
  width: 400px;
}

.child {
  column-count: 2;
  column-gap: 30px;
  margin-right: -30px;
}
<div class="ref"></div>

<div class="parent">
  <div class="child">
    i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i 
  </div>
</div>

【问题讨论】:

  • 你可能需要word-break: keep-all;
  • @ZohirSalak 我试过了,但不幸的是它没有用。
  • 不可能使用列布局为每列选择宽度,不幸的是,您无法避免使用多个 div。这回答了你的问题了吗? Is there a way to specify different widths for columns in CSS3?
  • 你能给我们你的真实用例吗?我怀疑你会有一个以i 为内容的元素
  • @ZohirSalak 谢谢! – 是的……看起来你做不到。但这很好,有一些方法可以解决它,在未来的 CSS 版本中仍然是一个很酷的特性。 :)

标签: css


【解决方案1】:

您将框设置为绝对宽度并且内容不会溢出。
这是一个可行的解决方案:

.ref {
  width: 200px;
  height: 10px;
  background-color: black;
}

.parent {
  background-color: lightgrey;
  width: 400px;
}

.child {
  column-count: 2;
  column-gap: 3.5%;
  margin-right: -30px;
  overflow-x: auto, hidden;
  padding: 8px 30px;
  word-break: keep-all;
  display: inline-block;
  text-align: center;
}
<div class="ref"></div>

<div class="parent">
  <div class="child">
    i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i 
  </div>
</div>

【讨论】:

  • 列的宽度仍然相等
猜你喜欢
  • 2013-09-17
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2011-03-16
相关资源
最近更新 更多