Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
From the author of Making Layout Changes

Making Layout Changes

For this section, I've made significant changes to the original example from the book. Download the updated example to follow along with the changes.

For this version, I've added another media query that causes a significant layout change when the browser width decreases below a certain level. I started by creating a wider layout, with the images floated next to the paragraphs. First, I wrapped each image/paragraph pair in a div so that the float will work properly:

<div class="article"></div>

Next, I floated the images (and their captions) to the left, using clear on the article wrapper to make sure that the different parts stay aligned sensibly:

.image_wrapper {
   position: relative;
   float:left;
   margin-right: 1em;
}
.article {
   clear: both;
}

That's really the only major change, although I've made a few minor changes such as widening the content wrapper to accommodate the floated content. The layout now looks like Figure 3.

Figure 3 A new floated layout for the page.

Let the media queries begin! The first one I added is as follows:

@media all and (max-width: 900px) {
  .image_wrapper {
     float: none;
  }
  #wrapper {
     width: 20em;
  }
  #navigation_menu {
    left: 29em;
  }
}

The first rule here simply gets rid of the float. The second and third rules reduce the width of the content to suit a narrower layout, pulling the navigation menu back over a bit (reducing the left positioning from 42.5 ems to a mere 29 ems). This only happens when the width of the browser window goes below 900 pixels. When that happens, the layout changes to the version shown in Figure 4.

Figure 4 A narrower layout with float removed.

Moving the Menu to the Top

The next media query is pretty much the same as the original media query that you saw previously:

@media all and (max-width: 660px) {
  #navigation_menu {
    top: 2.6em;
    left: 0em;
    z-index: 4;
    min-width: 600px;
  }
  #header {
    min-width: 600px;
  }
  #navigation_menu li {
     display: inline;
  }
  .shifted {
    right: 0px;
  }
}

It does the same job as before—making the menu horizontal and shifting it up to the header. Go below 660 pixels in width, and the layout looks like Figure 5.

Figure 5 Moving the menu up to the header when it no longer fits on the side.

Making the Site More Mobile-Friendly

The last change I'll cover here is designed to make the site more mobile-friendly, by adding a suitable narrow-width media query:

@media all and (max-width: 320px), all and (max-device-width: 320px) {
  #wrapper {
    margin: 0;
    padding: 0 5px;
    min-width: 0;
    width: 100%;
    background-color: white;
    border-left: 0px solid black;
    border-right: 0px solid black;
  }
  #header {
    min-width: 0;
    height: 150px;
  }
  h1 {
    font-size: 1.1em;
  }
  #navigation_menu {
    top: 1.5em;
  }
  #navigation_menu li {
    font-size: 0.8em;
    display: block;
  }
  #first {
   padding-top: 140px;
  }
}

Let's run through this media query very quickly:

  1. The first rule gets rid of all centering, minimum width, margins, borders, and most other special effects, occupying a nice round 100% of the width of the screen.
  2. The second rule gets rid of minimum width on the header and increases its height a bit so that the menu will fit into it.
  3. The third rule decreases the top-level heading's font size a bit.
  4. The fourth and fifth rules reintroduce display: block; to make the menu a vertical list again, bring it a bit closer to the top, and reducing the font size to fit.
  5. The final rule increases the padding at the top of the content, to allow for the taller header.

Notice that the media query test actually chains together two media queries, either of which can be passed for the rules inside to be executed. They look very similar, but there is a difference—I've included max-width in the first media query to target desktop browsers as before, and max-device-width in the second media query to target mobile device screens. Figure 6 shows the first media query in action (Opera 10.5), and Figure 7 shows the second media query in action (Opera Mini running on iPhone).

Figure 6 The narrow device view on the desktop.

Figure 7 The narrow device view on a mobile device.

  • + Share This
  • 🔖 Save To Your Account