1

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?

Salketer
  • 14,263
  • 2
  • 30
  • 58
Thev
  • 1,105
  • 2
  • 13
  • 24
  • Your divs are styled as a `table-cell` so it acts like a table. Try to take a look here: https://stackoverflow.com/questions/10525744/css-table-cell-equal-width – Jax-p Sep 11 '17 at 11:09
  • You just have to put `"display:table"` in first div. – Himanshu Sep 11 '17 at 11:21

5 Answers5

1

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>
Salketer
  • 14,263
  • 2
  • 30
  • 58
1

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>
Valdrinium
  • 1,398
  • 1
  • 13
  • 28
0

Try changing

<div style="display: table-row;">

to

<div style="display: table; width:100%;">
kinggs
  • 1,162
  • 2
  • 10
  • 25
0

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>
Salketer
  • 14,263
  • 2
  • 30
  • 58
enno.void
  • 6,242
  • 4
  • 25
  • 42
0

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>
lumio
  • 7,428
  • 4
  • 40
  • 56