You misunderstood how Flexbox works, and neither of your samples keep the same relation (70%/30%), and if you size the first enough you'll see they don't.
The reason is the min-width
, which defaults to auto
and prevent a flex items to be smaller than its content.
Change your rules to this if you want 70%/30%
.main-content {
flex: 0 0 70%; /* changed */
background-color: yellow;
min-width: 0; /* added */
}
.side-content {
flex: 0 0 30%; /* changed */
margin-left: 2px;
padding-left: 20px;
padding-right: 20px;
background-color: green;
min-width: 0; /* added */
}
Stack snippet
.content {
padding-top: 5px;
flex: 1;
display: flex;
flex-direction: row;
}
.main-content {
flex: 0 0 70%; /* changed */
background-color: yellow;
min-width: 0; /* added */
}
.side-content {
flex: 0 0 30%; /* changed */
margin-left: 2px;
padding-left: 20px;
padding-right: 20px;
background-color: green;
min-width: 0; /* added */
}
<h2>
Normal behaviour
</h2>
<div class='content'>
<div class='main-content'>
MAIN CONTENT
</div>
<div class='side-content'>
SIDE CONTENT
</div>
</div>
<h2>
Not proportional content
</h2>
<div class='content'>
<div class='main-content'>
THIS IS A MAIN CONTENT AREA THAT SHOUDN'T CHANGE WIDTH
</div>
<div class='side-content'>
THIS IS A SIDE CONTENT AREA THAT SHOUDN'T CHANGE WIDTH
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
</table>
</div>
</div>
Or like this to keep 2 of 3, 1 of 3. Do note that you need to set both flex-grow and flex-shrink, as the flex-shrink defaults to 1
and will kick in when there is no space left (distribute the negative space)
.main-content {
flex: 2 2 0; /* changed */
background-color: yellow;
min-width: 0; /* added */
}
.side-content {
flex: 1 1 0; /* changed */
margin-left: 2px;
padding-left: 20px;
padding-right: 20px;
background-color: green;
min-width: 0; /* added */
}
Stack snippet
.content {
padding-top: 5px;
flex: 1;
display: flex;
flex-direction: row;
}
.main-content {
flex: 2 2 0; /* changed */
background-color: yellow;
min-width: 0; /* added */
}
.side-content {
flex: 1 1 0; /* changed */
margin-left: 2px;
padding-left: 20px;
padding-right: 20px;
background-color: green;
min-width: 0; /* added */
}
<h2>
Normal behaviour
</h2>
<div class='content'>
<div class='main-content'>
MAIN CONTENT
</div>
<div class='side-content'>
SIDE CONTENT
</div>
</div>
<h2>
Not proportional content
</h2>
<div class='content'>
<div class='main-content'>
THIS IS A MAIN CONTENT AREA THAT SHOUDN'T CHANGE WIDTH
</div>
<div class='side-content'>
THIS IS A SIDE CONTENT AREA THAT SHOUDN'T CHANGE WIDTH
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
</table>
</div>
</div>