Search code examples
cssmargincentercentering

All margins are equal, but some margins are more equal than others


div {
    width: 100px;
    margin: 0 auto;
}

This CSS horizontally centers a block element.

Is it possible to offset auto margins' proportions? For instance, make the left margin twice as big as the right margin?


Solution

  • You could use some empty divs and "flex" the left side at twice the rate:

    body {
      display: flex;
    }
    
    #left {
      flex: 2
    }
    
    #content {
      width:100px;
      background-color: pink;
    }
    
    #right {
      flex: 1
    }
    <div id="left"></div>
    <div id="content">content</div>
    <div id="right"></div>