Austan
Austan

Reputation: 51

Converting desktop layout to single-column mobile layout

I had an entire site created, but removed all the filler and left the 3-column divs. My goal is to have these 3 columns stack vertically as the image is resized.

The additional css is included as it was used with the rest of the site, so I included it all.

I tried looking up certain media queries and tried wrapping my head around flexbox. Any help would be appreciated.

  * {
  border: 1px solid red;
  /*For layout purposes only*/
}

* {
  max-width: 980px;
  font-family: 'Lato', sans-serif;
  margin-left: auto;
  margin-right: auto;
  padding-bottom: 0px;
}

* {
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-wrap: wrap;
  overflow: hidden;
}

.row {
  width: 100%;
  display: flex;
}

.img {
  display: block;
  margin: auto;
}

.col-1 {
  width: 8.33%;
}

.col-2 {
  width: 16.66%;
}

.col-3 {
  width: 25%;
}

.col-4 {
  width: 33.33%;
}

.col-5 {
  width: 41.66%;
}

.col-6 {
  width: 50%;
}

.col-7 {
<header class="container">
  <div class="row">
    <div class="col-4">
      <img class="img" src="http://placehold.it/300x300">
    </div>
    <div class="col-4">
      <img class="img" src="http://placehold.it/300x300">
    </div>
    <div class="col-4">
      <img class="img" src="http://placehold.it/300x300">
    </div>
  </div>
</header>

Upvotes: 4

Views: 6146

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 371221

An initial setting of a flex container is flex-direction: row. That means that the children of the container ("flex items") will align horizontally, just like in your code.

To stack items vertically, switch to flex-direction: column.

.row {
    display: flex;
    flex-direction: column; /* NEW */
}

If you want flex items to stack vertically on resize, change the flex-direction in a media query:

@media screen and ( max-width: 500px ) { 
     .row { flex-direction: column; }
   }

jsFiddle demo

Upvotes: 7

Related Questions