teaching machines

CS 318: Lab 13 – Responsive Design

March 14, 2018 by . Filed under cs318, lectures, spring 2018.

Dear students,

Today we begin our investigation into responsive design, the central mechanic of which is selectively applying styles based on the viewing context. You have one style for large desktop displays, another for tablets, another for phones, and so on. In general, we might do the following on smaller screens:

In general, on a mobile device, we tend to focus on the main content. We have limited space, and we eliminate excess. It turns out that focusing on content is also a pretty good idea for desktop browsing. This has led many web designers to take a mobile-first approach. Our default styles will be applied to make our sites present well on a mobile device, but we add extra rules that will kick in when we have more space.

Let’s do a quick example. Suppose we’ve got this style:

body {
  background-color: pink;
}

When the viewport grows beyond a threshold, let’s switch to cyan. We can use a media query to selectively apply that rule:

@media (min-width: 42em) {
  body {
    background-color: cyan;
  }
}

Now if we resize the browser, we can see the page switch between styles. However, the background color is probably not a meaningful property to adjust. Let’s add this content:

<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Gold_Star.svg/2000px-Gold_Star.svg.png" class="star">1</div>
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Gold_Star.svg/2000px-Gold_Star.svg.png" class="star">2</div>
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Gold_Star.svg/2000px-Gold_Star.svg.png" class="star">3</div>
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Gold_Star.svg/2000px-Gold_Star.svg.png" class="star">4</div>
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Gold_Star.svg/2000px-Gold_Star.svg.png" class="star">5</div>
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Gold_Star.svg/2000px-Gold_Star.svg.png" class="star">6</div>

And style it like so:

div {
  font-size: 48pt;
  border: 2px solid black;
  text-align: center;
  margin: 5px;
  float: left;
}

img#star {
  width: 50px;
  margin-right: 10px;
  display: none;
}

When the page grows, let’s add those stars back in:

@media (min-width: 42em) {
  /* ... */

  img#star {
    display: inline;
  }
}

Neat, huh? Through this media query mechanism, you can make a single site that is navigable by many different kinds of audiences. This is a pretty big deal if the success of your organization is based on its reach.

Here’s your TODO list:

See you next time!

Sincerely,

Lab

First, finish whatever tasks you have left from last lab. Ask questions if something’s not working like you think it should.

Then, for today’s lab, your goal is typeset a book that will display differently on screens of different size.

Create a lab13 directory. Add a partners.html that includes your first names and last initials.

Book

The final book should behave like this:

Here’s your checklist to make it happen:

You should now have a page that adapts to different screens. Welcome to the future!

Publish and Validate

Commit and push your work. Verify that all pages have been uploaded by visiting https://USERNAME.github.io/cs318/lab13 in your browser.

Validate your pages with the W3C Validator. Paste in the URLs to your pages, one at a time, and address all concerns raised by the validator. Fix your changes on the local machine, commit, and push until the validator reports no errors or warnings.