Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table inside Flexbox Wider than the Page

Tags:

html

css

I am having some trouble with having a table inside a flexbox layout bleeding outside the parent flexbox making the page wider than the browser width. This is kind of a mockup showing the similar problem that I am having.

#flex {
  display:flex;
  display:-webkit-flex;
  flex-direction: row;
}
#one {
  background-color: black;
  width: 300px;
  flex-shrink: 0;
  -webkit-flex-shrink: 0;
}
#two {
  background-color:red;
}
<div id="flex">
  <div id="one">one
  </div>
  <div>
    <table>
      <tr>
        <td>First Row</td>
        <td>Second Row</td>
        <td>Third Row</td>
        <td>Fourth Row</td>
        <td>Fifth Row</td>
        <td>Sixth Row</td>
        <td>Seventh Row</td>
        <td>Eighth Row</td>
        <td>Ninth Row</td>
        <td>Tenth Row</td>
      </tr>
      <tr>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
      </tr>
    </table>
  </div>
</div>    

You can see that even though .flex is just the width of the screen, .two is much wider.

I am using datatables inside the second div so the table size is dynamic, and even with setting max widths to the child and everything, I cannot get it to stop bleeding out. I need help figuring out how I can lock down the second div of the flexbox to let the table resize correctly (it has responsive elements, but doesn't get used b/c it just goes wide).

edit: It seems like the table is doing the full 100% width of the page, but then is going outside the margin on the right side because of the element on the left.

Thanks for the help.

like image 988
David Avatar asked Feb 26 '16 18:02

David


People also ask

Can I use flexbox with table?

For modern browsers, we all can use flexbox to create responsive tables! If you want to know more about flexbox, you can always view the superb guide from CSSTricks! First, this is what we want to do: Desktop, tablet and mobile version of this table.

How do I fill the remaining space in flex element?

You can use the flex-grow property to force an item to fill the remaining space on the main axis of a flex container. The item will expand as much as possible and occupy the free area.


1 Answers

The answer above didn't work for me, but what did work:

I had problems because the two tables were directly into the flexbox container. Apparently the tables automatically take the same size that the div surrounding it, even though this is a flexbox container...

So, Just put two div's inside your flexbox and put the table inside the div:

<div style="display:flex;">
    <div><table> table content </table></div>
    <div><table> table content </table></div>
</div>

Then if you still have problems, make sure your table has this CSS:

table{
  width: 100% !important;
  table-layout: fixed;
  word-break: break-word;
}
like image 56
mesqueeb Avatar answered Sep 19 '22 03:09

mesqueeb