Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set min-width either by content or 200px (whichever is greater) together with max-width

Tags:

css

flexbox

I need this:

Container width is fixed width, items flowing in the row direction and wrapping at the end.

each item should be max-width: 400px, overflowing content should be cut. The minimum width of the items should be determined by the content, however: it should never be shorter than 200px.

Here is my CSS code, it does not cover the "min-content" aspect. min-content is suggested by the w3 in their Flexbox working draft, but it does not seem to work in my case:

.container {     display: -webkit-flex;     display: flex;     -webkit-flex-wrap: wrap;     flex-wrap: wrap; }  .container .box {     -webkit-flex: 1;     flex: 1;     min-width: 200px;     max-width: 400px;     height: 200px;     background-color: #fafa00;     overflow: hidden; } 

and the HTML is:

<div class="container">     <div class="box">         <table>             <tr>                 <td>Content</td>                 <td>Content</td>                 <td>Content</td>             </tr>         </table>         </div>     <div class="box">         <table>             <tr>                 <td>Content</td>             </tr>         </table>         </div>     <div class="box">         <table>             <tr>                 <td>Content</td>                 <td>Content</td>             </tr>         </table>         </div>     [...] </div> 
like image 423
Anton Harald Avatar asked Mar 26 '15 21:03

Anton Harald


People also ask

Can we set min width and max width for an element at the same time?

And min-width specify lower bound for width. So the width of the element will vary from min-width to ... (it will depend on other style). So if you specify min-width and max-width , you will set up a lower and upper bound and if both are equal it will be the same as simply specifing a width .

How do you use both min and max width in media query?

If you want to include both min and max width for responsiveness in the browser, then you can use the following: @media (min-width: 768px) and (max-width: 992px){...} @media (min-width: 480px) and (max-width: 767px) {...}

How do you use max width and min width?

Max-width and min-width can be used together to target a specific range of screen sizes. @media only screen and (max-width: 600px) and (min-width: 400px) {...} The query above will trigger only for screens that are 600-400px wide. This can be used to target specific devices with known widths.

Does Min width override max width?

Change the maximum width. max-width overrides width , but min-width overrides max-width .


1 Answers

The problem is that flex: 1 sets flex-basis: 0. Instead, you need

.container .box {   min-width: 200px;   max-width: 400px;   flex-basis: auto; /* default value */   flex-grow: 1; } 

.container {    display: -webkit-flex;    display: flex;    -webkit-flex-wrap: wrap;    flex-wrap: wrap;  }    .container .box {    -webkit-flex-grow: 1;    flex-grow: 1;    min-width: 100px;    max-width: 400px;    height: 200px;    background-color: #fafa00;    overflow: hidden;  }
<div class="container">    <div class="box">      <table>        <tr>          <td>Content</td>          <td>Content</td>          <td>Content</td>        </tr>      </table>        </div>    <div class="box">      <table>        <tr>          <td>Content</td>        </tr>      </table>        </div>    <div class="box">      <table>        <tr>          <td>Content</td>          <td>Content</td>        </tr>      </table>        </div>  </div>
like image 82
Oriol Avatar answered Sep 29 '22 12:09

Oriol