即使在使用自动换行后,flex项也会因长字而溢出容器

Sac*_*hin 44 html css css3 flexbox responsive-design

<div class="parent">
   <div class="child1">
     question
   </div>
  <div class="child2">
  somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething
Run Code Online (Sandbox Code Playgroud)

<div class="parent">
  <div class="child1">
    question
  </div>
  <div class="child3">
   somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.parent{
  width:100%;
  display:flex;
  flex-direction:row;
  flex-wrap:nowrap;
  padding:1em;
  background-color:red;
  box-sizing:border-box;
}
.child1{
  background-color:mistyrose;
  padding:1em;
}

.child2{
  background-color:powderblue;
  padding:.5em;
  word-wrap:break-word;
  max-width:500px;
}
.child3{
  background-color:powderblue;
  padding:.5em;
  word-wrap:break-word;

}
Run Code Online (Sandbox Code Playgroud)

与上面的代码的主要问题是,child3溢出,但如果我给一个max-widthchild2它不会溢出parent.在我使用的两种情况下word-wrap: break-word;

您可以在这里查看代码 http://jsfiddle.net/vbj10x4k/

我需要知道它为什么会发生以及如何在不使用max-width/width固定像素值的情况下解决它.我需要它才能做出回应.

Pau*_*aul 70

不要为flex项设置最大宽度,而是使用最小宽度声明.

默认情况下,如果未设置最小宽度,则假定项目的内容最小宽度,并且弹性项目的宽度将永远不会更小.通过设置例如50%的最小宽度,项目将缩小到最多50%的flex父级.

.child2, .child3 {
  min-width: 40%;
}
Run Code Online (Sandbox Code Playgroud)

.parent{
  width:100%;
  display:flex;
  flex-direction:row;
  flex-wrap:nowrap;
  padding:1em;
  background-color:red;
  box-sizing:border-box;
}
.child1{
  background-color:mistyrose;
  padding:1em;
}
.child2{
  background-color:powderblue;
  padding:.5em;
  word-wrap: break-word;
  overflow-wrap: break-word;
  min-width: 40%;
}
.child3{
  background-color:lightyellow;
  padding:.5em;
  word-wrap: break-word;
  overflow-wrap: break-word;
  min-width: 40%;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <div class="child1">
    question
  </div>
  <div class="child2">
   somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething

  </div>
  <div class="child3">
    Works well with long URLs: <a href="#"> https://thisisadamnlongurlcontainingneitherhyphensoradditionalperiods.com</a>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

请参阅上面的代码片段或外部演示:http://jsfiddle.net/vbj10x4k/5/

  • 您也可以将它设置为非常小的东西,例如`min-width:1%;`使用`word-wrap:break-word;`的元素`并且它可以工作. (5认同)

Gau*_*wal 28

而不是word-wrap:break-word使用word-break:break-all

CSS

.child1{
  background-color:mistyrose;
  padding:1em;
  word-break:break-all;
}
.child2{
  background-color:powderblue;
  padding:.5em;
  max-width:500px;
  word-break:break-all;
}
.child3{
  background-color:powderblue;
  padding:.5em;
  word-break:break-all;

}
Run Code Online (Sandbox Code Playgroud)

这里是工作小提琴链接http://jsfiddle.net/yudi/vbj10x4k/3/

  • 这个解决方案甚至会破坏child1元素中的"问题"这个词,这可能不是故意的.并且您仍然使用固定的最大宽度,这与响应式设计不相符. (7认同)

Roc*_*Lee 5

我正在使用这样的组合,它适用于 Chrome、Safari 和 Firefox。

.myOverflowableText {
  word-break: break-word; /* Chrome, Safari */
  overflow-wrap: anywhere; /* Firefox */
}
Run Code Online (Sandbox Code Playgroud)

该站点表示 Chrome 和 Safari 支持断字功能https://caniuse.com/#feat=word-break

但是我发现Firefox的解决方案应该overflow-wrap: anywhere在这个站点上:https : //developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap

我对 IE 还不太确定...也许word-wrap: break-word;可以在 IE 上使用?

我的目标是这样的:

Hello this is some |
text. I am writing |
some text.         |
Especially long    |
words go to the    |
next line like     |
this. This is a    |
veryveryveryveryve |
ryveryverylongword |
.                  |
Run Code Online (Sandbox Code Playgroud)


bas*_*ald 5

通过顺风添加min-w-0为我解决了这个问题。

  • 虽然这有帮助,但这个答案是特定于平台的。由于这个问题既没有引用也没有用 Tailwind 标记,OP 很可能没有使用它。[`min-w-0` 只是 CSS `min-width: 0px;`](https://tailwindcss.com/docs/min-width) 的抽象。 (4认同)
  • 任何看到我的答案并使用 Tailwind 的人都可以利用此信息。当我寻找解决方案时,这出现在谷歌“flex box break word”的顶部搜索结果中。这种情况也发生在其他线程中,例如,我正在查找 MySQL 列类型的纬度/经度,有人发布了 Laravel 迁移,这对我很有帮助,因为我无论如何都在使用 Laravel。仅仅因为这个问题是特定于平台的,并不意味着它不会帮助那些可能不使用它所适用的特定平台的人。 (2认同)