Thev
Thev

Reputation: 1125

Why is the width of divs not working as defined?

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

Answers (5)

Valdrinium
Valdrinium

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

lumio
lumio

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

enno.void
enno.void

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

Salketer
Salketer

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

kinggs
kinggs

Reputation: 1168

Try changing

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

to

<div style="display: table; width:100%;">

Upvotes: 0

Related Questions