Search code examples
htmlcsscentering

CSS "margin: 0 auto" not centering


Okay I understand that this topic has been covered. But I have looked at various solutions and have had little success with them.

I just have no clue why this margin: 0 auto; is not working. I tried compensating the padding with width: calc(33% - 40px);, but this did not work.

Any help on why this is happening, with solutions would be greatly appreciated!

.container {
  margin: 0 auto;
}

[class*='col-'] {
  float: left;
}

.col-2-3 {
  width: 66.66%;
}

.col-1-3 {
  width: 33.33%;
}

.grid:after {
  content: "";
  display: table;
  clear: both;
}

.col-word {
  width: auto;
  height: auto;
  padding: 25px;
  border: 5px #000 solid;
  border-left: 0px;
  border-right: 0px;
  background-color: #A7F4F6;
  font-size: xx-large;
  text-align: center;
}
<div class='container'>
  <div class="grid">
    <div class='grid'>
      <div class="col-1-3">
        <p class='col-word'>T</p>
        <p class='col-word'>V</p>
      </div>
    </div>

    <div class='grid'>
      <div class='col-1-3'>
        <div class='letter'>W</div>
      </div>
      <div class='col-1-3'>
        <div class='letter'>P</div>
      </div>
      <div class='col-1-3'>
        <div class='letter'>V</div>
      </div>
    </div>
  </div>
</div>

https://jsfiddle.net/xm2gvzbf/5/


Solution

  • It is working.

    The problem is you're centering a div, which is a block-level element by default, and which therefore occupies 100% width of its parent (body, in this case). So there's no space to move horizontally, hence no space to center.

    For an illustration see this revised demo which has an added border around .container.

    .container {
      margin: 0 auto;
      border: 1px solid red;
    }
    
    [class*='col-'] {
      float: left;
    }
    
    .col-2-3 {
      width: 66.66%;
    }
    
    .col-1-3 {
      width: 33.33%;
    }
    
    .grid:after {
      content: "";
      display: table;
      clear: both;
    }
    
    .col-word {
      width: auto;
      height: auto;
      padding: 25px;
      border: 5px #000 solid;
      border-left: 0px;
      border-right: 0px;
      background-color: #A7F4F6;
      font-size: xx-large;
      text-align: center;
    }
    <div class='container'>
      <div class="grid">
        <div class='grid'>
          <div class="col-1-3">
            <p class='col-word'>T</p>
            <p class='col-word'>V</p>
          </div>
        </div>
        <div class='grid'>
          <div class='col-1-3'>
            <div class='letter'>W</div>
          </div>
          <div class='col-1-3'>
            <div class='letter'>P</div>
          </div>
          <div class='col-1-3'>
            <div class='letter'>V</div>
          </div>
        </div>
      </div>
    </div>

    If you reduce the width of .container you'll see the centering work. Here's a second demo, with width: 50% applied to .container.

    .container {
      margin: 0 auto;
      border: 1px solid red;
      width: 50%;
    }
    
    [class*='col-'] {
      float: left;
    }
    
    .col-2-3 {
      width: 66.66%;
    }
    
    .col-1-3 {
      width: 33.33%;
    }
    
    .grid:after {
      content: "";
      display: table;
      clear: both;
    }
    
    .col-word {
      width: auto;
      height: auto;
      padding: 25px;
      border: 5px #000 solid;
      border-left: 0px;
      border-right: 0px;
      background-color: #A7F4F6;
      font-size: xx-large;
      text-align: center;
    }
    <div class='container'>
      <div class="grid">
        <div class='grid'>
          <div class="col-1-3">
            <p class='col-word'>T</p>
            <p class='col-word'>V</p>
          </div>
        </div>
        <div class='grid'>
          <div class='col-1-3'>
            <div class='letter'>W</div>
          </div>
          <div class='col-1-3'>
            <div class='letter'>P</div>
          </div>
          <div class='col-1-3'>
            <div class='letter'>V</div>
          </div>
        </div>
      </div>
    </div>