Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a max-width for flex items?

Tags:

I'm trying to create a filter section on my site, the content is all generated dynamically (so I can't add extra parents to it), and our styleguide creates them using flex items. I'd like to keep this functionality for the most part.

I want my 3 flex items to have a max-width and be floated left, leaving my non flex item floated right, in the overall container that is set to a max width of 1080px sort of like this:

Option 1    Option 2    Option 3                                    Non-flex Item 

I've tried setting the flex-align values and following this answer, but that doesn't seem to work for this.

As of right now, this is the code that I am working with:

HTML

<ul class="container">   <li class="child">One</li>   <li class="child">Three</li>   <li class="child">Three</li>   <div class="non-flex">    I'm not a flex item   </div> </ul> 

CSS

.container{       display: -webkit-box;         display: -ms-flexbox;         display: flex;         -webkit-box-orient: horizontal;         -webkit-box-direction: normal;         -ms-flex-direction: row;         flex-direction: row;         -webkit-box-pack: justify;         -ms-flex-pack: justify;         justify-content: space-between;         position: relative;             max-width: 1080px;         margin: 0 auto;         padding: 0 20px;         box-sizing: content-box;     }      .child{           display: -webkit-box;         display: -ms-flexbox;         display: flex;         -webkit-box-orient: horizontal;         -webkit-box-direction: normal;         -ms-flex-direction: row;         flex-direction: row;         -webkit-box-pack: justify;         -ms-flex-pack: justify;         justify-content: space-between;         position: relative;     } 

I also made a Fiddle for you all to play around.

like image 337
knocked loose Avatar asked Dec 08 '17 13:12

knocked loose


People also ask

How do you set the max width on a Flex container?

Change the width of the flex-container to 810px and the flex-basis of each flex-item to 300px . As you can notice, the flex-basis property refers to an ideal size of the flex-items, only if there is enough space available in the flex-container.

How do you set the width of a flex item?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

Why max width is not working on Flex?

This is because flex items no longer exist in a block formatting context, where block level elements automatically take width: 100% . In a flex formatting context, elements have different defaults. Therefore, to get max-width to work on a flex item, also give the item width: 100% .

Does width work with Flex?

There is no difference between how flex-basis: will function on your element and how width: will function on your element. Width will even obey flex-shrink when using Flexbox.


1 Answers

I would apply a width or min-width to the .child items (or left/right padding that creates the distance between them) and margin-left: auto to the last item, which moves it right, independently from the others:

https://jsfiddle.net/6vwny6bz/2/

.container {    display: -webkit-box;    display: -ms-flexbox;    display: flex;    -webkit-box-orient: horizontal;    -webkit-box-direction: normal;    -ms-flex-direction: row;    flex-direction: row;    -webkit-box-pack: justify;    -ms-flex-pack: justify;    justify-content: space-between;    position: relative;    max-width: 1080px;    margin: 0 auto;    padding: 0 20px;    box-sizing: content-box;  }    .child {    list-style: none;    min-width: 80px;    padding: 10px 0;  }    .non-flex {    margin-left: auto;    padding: 10px 0;  }
<ul class="container">    <li class="child">One</li>    <li class="child">Three</li>    <li class="child">Three</li>    <div class="non-flex">      I'm not a flex item    </div>  </ul>
like image 139
Johannes Avatar answered Sep 19 '22 07:09

Johannes