I am struggling with a simple 2-column flex setup where I want to add some margins between the columns. However, when I try to do this, the second column ends up overflowing outside of the .flex-row container. This issue becomes more prominent when I include a <pre>
element inside the column as it doesn't seem to respect the max-width rule I have set for it.
My query consists of two parts:
- How can I add margins to my flex columns without causing overflow?
- Is there a way to control the size of a
<pre>
(or any other element) to make it fit its container?
*{
box-sizing: border-box;
}
.container {
background: #F5F5F5;
border: 1px solid #CCC;
margin: 0 auto;
padding: 0.5rem;
width: 500px;
}
.flex-row {
display: flex;
background: rgba(255,0,0, 0.1);
padding: 0.5rem 0;
}
.flex-col {
flex-basis: 50%;
flex-grow: 0;
flex-shrink: 0;
background: rgba(0,255,0, 0.1);
}
.flex-row .flex-col:last-child {
margin-left: 0.5rem;
}
pre {
border: 1px solid red;
padding: 0.5rem;
overflow-y: scroll;
max-width: 100%;
}
<div class="container">
<p>Some text in a container</p>
<div class="flex-row">
<div class="flex-col">
<p>Some text in a column. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Necessitatibus ea sint placeat deserunt, aut dolor et dolorum itaque, consequuntur minima quibusdam neque? Sequi eaque unde repudiandae, excepturi quibusdam maiores incidunt.</p>
</div>
<div class="flex-col">
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit.
Iste recusandae, repellendus ipsum accusantium rerum dolor fugiat, aperiam harum asperiores, vel debitis.
Quidem modi sapiente enim. Ipsam saepe blanditiis ipsa id?</p>
</div>
</div>
</div>
<hr>
<div class="container">
<p>Some text in a container</p>
<div class="flex-row">
<div class="flex-col">
<p>Some text in a column. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Necessitatibus ea sint placeat deserunt, aut dolor et dolorum itaque, consequuntur minima quibusdam neque? Sequi eaque unde repudiandae, excepturi quibusdam maiores incidunt.</p>
</div>
<div class="flex-col">
<pre>Lorem, ipsum dolor sit amet consectetur adipisicing elit.
Iste recusandae, repellendus ipsum accusantium rerum dolor fugiat,
aperiam harum asperiores, vel debitis.
Quidem modi sapiente enim. Ipsam saepe blanditiis ipsa id?</pre>
</div>
</div>
</div>