Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent wrapping lines in flexbox child element

Tags:

css

flexbox

How do I prevent line wrapping of a flexbox element? Is it even possible?

I'm attempting to create a two column grid with flexbox with the following markup:

<div class="flex-container">    <div class="no-wrap">this should not wrap</div>    <div class="can-wrap">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Totam debitis consequuntur incidunt neque vel in blanditiis id optio distinctio culpa rem voluptatibus quas architecto ratione assumenda omnis laboriosam? Earum esse.</div> </div> 

The .no-wrap element should only be as wide as it 'needs to be' such that all of the text inside does not wrap to a second line. The .can-wrap element will take up the remaining space, and wrap if need be.

I'm using this CSS (with prefrix free):

.flex-container{   display:flex; }  .no-wrap{   flex-basis:initial; } 

I thought the flex-basis:initial would prevent the element from wrapping lines - https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis

Here's a codepen: http://codepen.io/jshawl/pen/bknAc

I know I can fix the width of the .no-wrap element, but how can I make it be as wide enough to prevent line wrapping?

The float equivalent for the .no-wrap element would be:

.no-wrap{   display:block;   float:left; /* be as wide as need be! */ } 
like image 246
jshawl Avatar asked Aug 20 '13 16:08

jshawl


People also ask

How do I stop my flex from wrapping?

Use the flex-nowrap class in Bootstrap 4 to avoid wrapping the flex items.

Why is flexbox wrapping?

The flex-wrap property is a sub-property of the Flexible Box Layout module. It defines whether the flex items are forced in a single line or can be flowed into multiple lines. If set to multiple lines, it also defines the cross-axis which determines the direction new lines are stacked in.

Why are my flex items overflowing?

The initial value of the flex-wrap property is nowrap . This means that if you have a set of flex items that are too wide for their container, they will overflow it.

Which property of Flex will allow Flex items will wrap onto multiple lines from top to bottom?

The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines.


1 Answers

The property you are looking for is flex-shrink, not flex-basis.

.no-wrap{   flex-shrink: 0; } 

Note however, that IE10 is not listed as supporting this property (as it did not exist in the specification at the time they added the Flexbox implementation). You can use the flex property instead.

.no-wrap{   flex: 0 0 auto;  } 
like image 84
cimmanon Avatar answered Sep 20 '22 02:09

cimmanon