You might want to consider utilizing CSS Grid to tackle this task:
<div class="parent">
<div class="div1">1</div>
<div class="div2">2</div>
<div class="div3">3</div>
<div class="div4">4</div>
</div>
.parent {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-column-gap: 5px;
grid-row-gap: 5px;
width: 500px;
height: 200px;
}
.div1, .div2, .div3, .div4 {
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(255,0,0,0.5);
}
.div1 { grid-area: 1 / 1 / 2 / 2; }
.div2 { grid-area: 2 / 1 / 3 / 2; }
.div3 { grid-area: 1 / 2 / 3 / 4; }
.div4 { grid-area: 1 / 4 / 3 / 6; }
Update
Considering the feedback received, here is an alternative solution using flexbox. It may not be as effective on smaller screens, but you can experiment with it by using inline-flex
.
Feel free to modify the CSS variables to adjust the size of the grid units and the gap between them.
<div class="parent">
<div class="div1">1</div>
<div class="div2">2</div>
<div class="div3">3</div>
<div class="div4">4</div>
</div>
:root {
--gap: 5px;
--unitSize: 100px;
}
.parent {
display: inline-flex;
width: calc(var(--unitSize) * 5);
height: calc(var(--unitSize) * 2 + var(--gap) * 2);
gap: var(--gap);
flex-direction: column;
flex-wrap: wrap;
}
.div1, .div2, .div3, .div4 {
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(255, 0, 0, 0.5);
}
.div1, .div2 {
width: var(--unitSize);
height: calc(var(--unitSize) + var(--gap) / 2);
}
.div3, .div4 {
width: calc(var(--unitSize) * 2);
height: calc(var(--unitSize) * 2 + var(--gap) * 2);
}