This may be simple and I'm just missing it. I inherited similar markup to below, the width is set on the outer most div
and inherited by each child div
. The width is dynamic.
I want the pink wrapper
div to be the exact width as the row of blue boxes in the first row. I do not know how many blue boxes may be there, nor their widths.
I want the pink wrapper
to be centered inside the red/green div
s with the blue boxes left aligned.
I made two attempts but neither are working. Any help is appreciated. Below is an picture of what I want to achieve.
.outer {
width: 275px;
border: 4px solid red;
}
.parent-wrap {
border: 3px solid green;
}
.wrapper {
background: pink;
display: flex;
flex-wrap: wrap;
justify-content: left;
}
.box {
width: 50px;
height: 50px;
background: blue;
display: inline-block;
margin: 5px;
}
/* Attempt 2*/
.outer2 {
width: 275px;
border: 4px solid red;
}
.parent-wrap2 {
border: 3px solid green;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.wrapper2 {
background: pink;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.box2 {
width: 50px;
height: 50px;
background: blue;
display: inline-block;
margin: 5px;
}
<h2>Attempt #1</h2>
<div class="outer">
<div class="parent-wrap">
<div class="wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</div>
<h2>Attempt #2</h2>
<div class="outer2">
<div class="parent-wrap2">
<div class="wrapper2">
<div class="box2"></div>
<div class="box2"></div>
<div class="box2"></div>
<div class="box2"></div>
<div class="box2"></div>
<div class="box2"></div>
<div class="box2"></div>
</div>
</div>
</div>