0

I have a parent element which has a max-width but inside I have a table that I want to expand past that max-width, but only if it's necessary because of the content on the table.

Here's a fiddle so you can see: https://jsfiddle.net/z6nbc75g/

Try clicking the "add columns" button a few times to see how when you add too many, they start getting compressed instead of making the table wider.

Here's the code:

.parent {
  width: 100%;
  max-width: 20rem;
}

.table .row {
  display: flex;
  border: 1px solid blue;
  padding: 4px;
}

.table .col {
  border: 1px solid red;
  padding: 2px;
}
<div class="parent">
  <div class="table">
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
  </div>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
nick
  • 2,819
  • 5
  • 33
  • 69

4 Answers4

1

You can use min-width: max-content: https://developer.mozilla.org/en-US/docs/Web/CSS/width#max-content

Fiddle: https://jsfiddle.net/r84q7nvj/

Jesper Johansson
  • 599
  • 3
  • 14
0

You can make change in the style of row. Add flex-wrap property.

.table .row {
  display: flex;
  border: 1px solid blue;
  padding: 4px;
  flex-flow: row wrap;
}
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73
Ankit Shah
  • 36
  • 2
0

For what you're looking for, a way to handle this would be to make your .parent element as an inline-block (so it only occupies the width of the child elements), then prevent wrapping on the columns (assuming you want all of your content within a column to be on the same line - if you don't, the table will still expand until it reaches the width of the page, then cause the text to wrap within the columns).

Snippet below - I've added in the button from your jsfiddle so you can test this out :)

document.querySelector('button').addEventListener('click', () => {
 let rows = document.querySelectorAll('.row');
  
  rows.forEach(row => {
   let col = document.createElement('div');
    col.classList.add('col');
    col.textContent = 'lorem ipsum';
    
    row.appendChild(col);
  });
});
.parent {
  display:inline-block;
}

.table .row {
  display: flex;
  border: 1px solid blue;
  padding: 4px;
}

.table .col {
  border: 1px solid red;
  padding: 2px;
}
<div class="parent">
  <div class="table">
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
    <div class="row">
      <div class="col">lorem ipsum</div>
      <div class="col">lorem ipsum</div>
    </div>
  </div>
</div>

<button>Add columns</button>
DKyleo
  • 806
  • 8
  • 11
0

You could also solve this using display: inline-flex on the parent together with min-width: 200px and flex-basis on its children: https://jsfiddle.net/cnxgruzt/

Jesper Johansson
  • 599
  • 3
  • 14