Using Flexbox, how can I configure the buttons so that they’re all the same width when flex-direction: column is applied for mobile? It would typically be simple, however, the buttons are two levels beneath the parent and I can’t change the HTML markup. Here’s a demo with the HTML and CSS below:
.multi-cta-group {
border: 1px solid gray;
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
gap: 16px;
margin: 20px;
padding: 20px;
}
.multi-cta-container {
border: 1px solid red;
flex: 1;
}
.multi-cta-container a {
border: 1px solid lime;
flex: 1;
}
<div class="multi-cta-group">
<div class="multi-cta-container">
<a href="#" class="btn btn-primary">CTA label for video</a>
</div>
<div class="multi-cta-container">
<a href="#" class="btn btn-primary">Shop
</a>
</div>
<div class="multi-cta-container">
<a href="#" class="btn btn-primary">Learn more
</a>
</div>
<div class="multi-cta-container">
<a href="#" class="btn btn-primary">Subscribe now
</a>
</div>
</div>
3
Answers
It’s hard to know exactly what you want because you mention that this is for "mobile" and I’m not sure if you’re trying to make it reactive. But based on your example, some points:
flex: 1
on.multi-cta-container a
doesn’t do anything because the elements are not immediate children of the container.width: 100%
on yourmulti-cta-container
divs to force them to the width of the container.display: block
Here’s a snippet with these changes in place. Comment if this is not what you’re after and I’ll revise or delete.
You can add
width: 100%;
to the parent container:.multi-cta-container
and add
display: block; width: 100%;
to.multi-cta-container a
so it can stretch same as container width.https://codepen.io/talha2k/pen/vEBpwpO
Here’s some CSS grid code that might work better for your case. You can use media queries of course to switch from
display: flex
todisplay: grid
for mobile resolutions: