文本溢出容器时的 HTML/CSS 换行符

标签 html css overflow containers

在我的网站上,Lorem Ipsum 文本溢出了容器。如何在文本到达容器边界时自动添加换行符。

我创建了 this JSFiddle 来证明我的问题。

HTML:

<body>
    <div id="wrapper">
        <div id="content">      
            <div class="flex">
                <div class="flexchild">
                    <img src="img.jpg" width="200"></img>
                </div>
                <div class="flexchild">
                    <p>
                       Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.
                    </p>
                </div>
            </div>
        </div>
    </div>
</body>

CSS

body,html {
  background: #fff;
  width: 90%;
  margin-left: auto;
  margin-right: auto;
  height: 100%;
}

#wrapper{
  background: #ccc;
  height: 100%;
}

.flex {
  width: 100%;
  height: 340px;
  display: -webkit-box;
  display: flex;
}

.flexchild {
  -webkit-flex: 1 0 auto;
  flex: 1 0 auto;
  overflow: auto;
  display: block;
}

最佳答案

您需要使用0 而不是auto:

.flexchild {
  -webkit-flex: 1 0 0;
  flex: 1 0 0;
  overflow: auto;
  display: block;
}

参见 this JSFiddle ,或运行下面的代码片段。

body,
html {
  background: #fff;
  width: 90%;
  margin-left: auto;
  margin-right: auto;
  height: 100%;
}
#wrapper {
  background: #ccc;
  height: 100%;
}
.flex {
  width: 100%;
  height: 340px;
  display: -webkit-box;
  display: flex;
}
.flexchild {
  -webkit-flex: 1 0 0;
  flex: 1 0 0;
  overflow: auto;
  display: block;
}
<body>
  <div id="wrapper">
    <div id="content">
      <div class="flex">
        <div class="flexchild">
          <img src="http://www.engraversnetwork.com/files/placeholder.jpg" width="200"></img>
        </div>
        <div class="flexchild">
          <p>Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.</p>
        </div>
      </div>
    </div>
  </div>
</body>


进一步阅读:

MDN Documentation for flex-basis, the third parameter of flex

关于文本溢出容器时的 HTML/CSS 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158498/

相关文章:

css - 我的标题出现奇怪的 CSS 问题

css - 带溢出的过渡图像

javascript - 无需重新加载页面且无需使用 Ajax 即可刷新 Div、Table 或 TR

html - CSS 立方体无法正确旋转

html - 使用文本缩进-语义好吗??

html - 使用 CSS 边框的三 Angular 形和倒三 Angular 形

javascript - 如何找到 DIV 中的类计数

c++ - 在固定宽度的消息计数器中检测回绕的好方法是什么?

javascript - 检查元素的内容是否溢出?

javascript - 使用 jQuery 的 Ajax 结果中的选择器问题