Reputation: 1125
My goal was to have 5 divs nested within a parent div. Each div equally wide, and side-by-side.
This is my HTML:
<div style="margin-left: auto; margin-right: auto; width: 80%;">
<div style="display: table-row;">
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
</div>
</div>
The side-by-side feature is working, but not the widths. It's all just squeezed to the left. Any ideas why?
Upvotes: 1
Views: 41
Reputation: 1416
This can be achieved very beautifully using flex-boxes.
<div style="margin-left: auto; margin-right: auto; width: 80%; display: flex">
<div style="flex: 1; text-align: center;">Left</div>
<div style="flex: 1; text-align: center;">Left</div>
<div style="flex: 1; text-align: center;">Left</div>
<div style="flex: 1; text-align: center;">Left</div>
<div style="flex: 1; text-align: center;">Left</div>
</div>
Upvotes: 1
Reputation: 7575
Use display: flex
and widen all elements to a 100%.
.wrapper {
display: flex;
}
.wrapper > div {
width: 100%;
}
<div class="wrapper">
<div>Left</div>
<div>Left</div>
<div>Left</div>
<div>Left</div>
<div>Left</div>
</div>
Upvotes: 0
Reputation: 6579
To achieve this you need to set display: table;
to the outer div:
<div style="margin-left: auto; margin-right: auto; width: 80%;display:table">
<div style="display: table-row;">
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
</div>
</div>
Upvotes: 0
Reputation: 15711
The problem is that you are using a table-row
without it being inside a table
.
<div style="margin-left: auto; margin-right: auto; width: 80%;display:table">
<div style="display: table-row;background-color:green;">
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
<div style="display: table-cell; text-align: center; width: 20%;">Left</div>
</div>
</div>
Upvotes: 1
Reputation: 1168
Try changing
<div style="display: table-row;">
to
<div style="display: table; width:100%;">
Upvotes: 0