Search code examples
htmlcssflexboxcentering

Make the parent div width the same as the children


I made this layout that seems to work, the only problem is that I would like that all che filled div are centered.

There are three possible "states":

  1. the cyan element is to the left of all the others

  2. the gold and pink elements are on two columns

  3. all elements are on a single column.

What I want: state1 state2 state3

In the first case, everything is centered and it works, in the other two it doesn't: the elements are always aligned to the left.

To center I was thinking of using margin: 0 auto but it seems the divs are bigger than their content even if I used inline-flex (look at the grey area during state #3 in the running example). Why?

How can I solve?

.container {
  outline: 1px solid black;
  max-width: 490px;
  margin: 0 auto;
}
.columns {
  outline: 1px solid black;
  display: inline-flex;
  flex-wrap: wrap;
}
.map {
  background-color: cyan;
  width: 150px;
  min-width: 150px;
  height: 150px;
  min-height: 150px;
  margin-right: 20px;
}
.content {
  outline: 1px solid black;
  background-color: lightgray;
  max-width: 320px;
}

.cards {
  outline: 1px solid black;
  display: inline-flex;
  flex-wrap: wrap;
}
.card {
  background-color: pink;
  outline: 1px solid black;
  width: 150px;
  height: 70px;
  display: inline-block;
}
.card.left {
  margin-right: 20px;
}

.texts {
  outline: 1px solid black;
  display: inline-flex;
  flex-wrap: wrap;
}
.text {
  background-color: gold;
  outline: 1px solid black;
  width: 150px;
  height: 200px;
  display: inline-block;
}
.text.left {
  margin-right: 20px;
}
<div class="container">
  <div class="columns">
    <div class="map"></div>
    <div class="content">
      <div class="cards">
        <div class="card left">card #1</div>
        <div class="card">card #2</div>
        <div class="card left">card #3</div>
        <div class="card">card #4</div>
      </div>
      
      <div class="texts">
        <div class="text left">text #1</div>
        <div class="text">text #2</div>
      </div>
    </div>
  </div>
</div>


Solution

  • use media queries in the proper way and here you go, to play with it find this fiddle link, try to resize the result window.

    .container {
      max-width: 490px;
      margin: 0 auto;
    }
    
    .columns {
      display: inline-flex;
      flex-wrap: wrap;
    }
    
    .map {
      background-color: cyan;
      width: 150px;
      min-width: 150px;
      height: 150px;
      min-height: 150px;
      margin-right: 20px;
    }
    
    .content {
      background-color: lightgray;
      max-width: 320px;
    }
    
    .cards {
      display: inline-flex;
      flex-wrap: wrap;
    }
    
    .card {
      background-color: pink;
      width: 150px;
      height: 70px;
      display: inline-block;
    }
    
    .card.left {
      margin-right: 20px;
    }
    
    .texts {
      display: inline-flex;
      flex-wrap: wrap;
    }
    
    .text {
      background-color: gold;
      width: 150px;
      height: 200px;
      display: inline-block;
    }
    
    .text.left {
      margin-right: 20px;
    }
    @media(max-width: 520px){
    .container {
    
        display: flex;
          flex-direction: column;
        align-items: center;
    }
     .columns {
        display: inline-block;
        
      }
    }
    @media(max-width: 352px){
    .container {
    
        display: flex;
          flex-direction: column;
        align-items: center;
    }
      .map {margin-right: 0;}
     
      .content {
            max-width: min-content;
      }
    .card.left {
        margin-right: 0px;
    }
    .text.left {
        margin-right: 0px;
    }
    }
    <div class="container">
      <div class="columns">
        <div class="map"></div>
        <div class="content">
          <div class="cards">
            <div class="card left">card #1</div>
            <div class="card">card #2</div>
            <div class="card left">card #3</div>
            <div class="card">card #4</div>
          </div>
          <div class="texts">
            <div class="text left">text #1</div>
            <div class="text">text #2</div>
          </div>
        </div>
      </div>
    </div>