Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text not wrapping inside a flex container

I managed to use the Flexible Box Layout Module for a lot of things, however when it comes to paragraphs I'm experiencing this issue:

  • When I use a small amount of text everything works as expected:

    enter image description here

  • When I use a large amount of text the layout breaks:

    enter image description here

Here is the code I am using:

body {
  width: 90%;
  margin: 3em auto;
  background-color: #aaa;
}
section {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
  border: 1px solid black;
}
.title {
  flex: 1 0 100%;
  text-align: center;
  background-color: #eee;
}
.image {
  flex: 0 0 auto;
  background-color: #ccc;
}
.image img {
  display: block;
  max-height: 100%;
  max-width: 100%;
}
.text {
  flex: 6 0 auto;
  background-color: #96AED1;
}
<section>
  <div class="title">
    <h1>Title here</h1>
  </div>

  <div class="image">
    <img src="http://www.macovei-sculptor.ro/img.jpg">
  </div>

  <div class="text">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer felis velit, ullamcorper eu ornare sed, vulputate quis diam. Duis ultrices rutrum sapien, in condimentum nibh sagittis sit amet. Morbi sit amet rhoncus dui, at pharetra nisi. Nunc et
      lacus porttitor, pretium nisl at, sollicitudin velit. Donec felis nisl, consequat vitae egestas vestibulum, egestas non tortor. Cras mattis non sem nec aliquam. Sed dignissim sit amet sapien vitae feugiat. Pellentesque vel luctus ante, quis ornare
      purus. Nulla in arcu sapien. Aenean tempor arcu ac lacinia pellentesque. Quisque vulputate maximus augue, non aliquet ligula gravida in. Donec a leo justo. Integer velit eros, blandit sit amet elit eget, efficitur mollis nulla. Suspendisse tempor
      ligula facilisis scelerisque ullamcorper. Ut vehicula ligula ipsum, cursus condimentum sapien pretium ac.</p>
  </div>
</section>

Why is the <p> tag not wrapping properly when using a large amount of text?

like image 248
Mihai Avatar asked Aug 29 '15 07:08

Mihai


People also ask

How do you wrap text in Flex container?

As you only want the text itself to wrap you need to use flex-wrap: nowrap; to keep . right on the same line. The text will automatically wrap when there is not enough space.

What does the flex-wrap wrap rule do?

The flex-wrap property allows enabling the control direction in which lines are stacked. It is used to designate a single line or multi-line format to flex items inside the flex container.

How do I force text to not wrap?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).


2 Answers

The proper solution is either to put

white-space: initial;

or

min-width: 0;

Check out the link here

*I am aware the link is about truncating texts. However, the same solution applies to wrap the texts around.

like image 147
Kiong Avatar answered Sep 20 '22 15:09

Kiong


the .text's parent has flex-direction: row, so .text should have shrink set on 1 otherwise the text inside won't wrap

body {
  width: 90%;
  margin: 3em auto;
  background-color: #aaa;
}
section {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
  border: 1px solid black;
}
.title {
  flex: 1 0 100%;
  text-align: center;
  background-color: #eee;
}
.image {
  flex: 0 0 auto;
  background-color: #ccc;
}
.image img {
  display: block;
  max-height: 100%;
  max-width: 100%;
}
.text {
  flex: 6 1 auto;  /* FLEX-SHRINK: 1; */
  background-color: #96AED1;
}
<section>
  <div class="title">
    <h1>Title here</h1>
  </div>

  <div class="image">
    <img src="http://www.macovei-sculptor.ro/img.jpg">
  </div>

  <div class="text">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer felis velit, ullamcorper eu ornare sed, vulputate quis diam. Duis ultrices rutrum sapien, in condimentum nibh sagittis sit amet. Morbi sit amet rhoncus dui, at pharetra nisi. Nunc et
      lacus porttitor, pretium nisl at, sollicitudin velit. Donec felis nisl, consequat vitae egestas vestibulum, egestas non tortor. Cras mattis non sem nec aliquam. Sed dignissim sit amet sapien vitae feugiat. Pellentesque vel luctus ante, quis ornare
      purus. Nulla in arcu sapien. Aenean tempor arcu ac lacinia pellentesque. Quisque vulputate maximus augue, non aliquet ligula gravida in. Donec a leo justo. Integer velit eros, blandit sit amet elit eget, efficitur mollis nulla. Suspendisse tempor
      ligula facilisis scelerisque ullamcorper. Ut vehicula ligula ipsum, cursus condimentum sapien pretium ac.</p>
  </div>
</section>
like image 40
Xiaolin Wu Avatar answered Sep 18 '22 15:09

Xiaolin Wu