I want to inner div gets all width of scrollable parent div, this is my sample code:
<div class="col-6 flex-column gap-8px overflow-x-scroll">
<div class="d-flex w-100 p-3 border bg-warning rounded col-12">
<span class="col-4 bg-info m-2">col 1</span>
<span class="col-4 bg-info m-2">col 2</span>
<span class="col-4 bg-info m-2">col 3</span>
<span class="col-4 bg-info m-2">col 4</span>
</div>
</div>
and the result:
How i can fix this ?
2
Answers
To set the width of an inner
<div>
to 100% of its scrollable parent<div>
, you need to ensure that the parent<div>
is scrollable (i.e., it has content that overflows) and that the inner<div>
is styled appropriately to inherit the width.Here’s how you can do this:
Make sure the parent
<div>
is scrollableFor a parent
<div>
to be scrollable, you can set itsoverflow
property toauto
(orscroll
), and specify a fixed width or height if necessary.Set the inner
<div>
width to 100% of the parentOnce the parent is scrollable, you can simply set the width of the inner
<div>
to 100% to make it match the width of the parent.The problem is caused by using two wrappers instead of one, as per the hint from @Mike’Pomax’Kamermans.