Media Queries

Projected Time

45-60 minutes

Prerequisites

Motivation

Which companies use media queries? Everyone. Some companies with very responsive sites are Etsy and Hitachi.

Looking at an example website

Objectives

Participants will be able to:

Specific Things to Learn

Materials

Lesson / Guided Practice

Practice implementing a Media Query

  1. Create a very simple project, or follow along in an existing project.
  2. Create an HTML file with a linked css file, a title, 3 images, and a paragraph like this:

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="index.css">
        <title></title>
      </head>
      <body>
        <h1>Under the Sea</h1>
        <p>
          Halfbeak Blind shark, Australian herring filefish elver bent-tooth Russian sturgeon koi, mud cat; Celebes rainbowfish tilapia swordtail. Tang flagtail, pompano dolphinfish jewel tetra thornyhead; mako shark. Wallago hussar, longnose whiptail catfish, "scup, yellow perch cutthroat trout Blind shark driftwood catfish Atlantic cod Blenny river loach?" Splitfin rocket danio barbeled houndshark velvetfish sand tiger golden shiner cowfish snake mackerel, "baikal oilfish porgy fusilier fish creek chub mud catfish." Coelacanth bent-tooth spearfish: soapfish mahseer Russian sturgeon fire bar danio kelp perch. Blind goby shortnose greeneye Celebes rainbowfish bigmouth buffalo: yellowtail, earthworm eel ghost knifefish amur pike. Gray mullet aholehole steelhead Australasian salmon barfish Siamese fighting fish shortnose chimaera inanga yellow bass opaleye.
          Leopard danio pencilsmelt thorny catfish razorfish boarfish barreleye, sand tiger. Walking catfish tailor--cuchia prickly shark chain pickerel Port Jackson shark sawtooth eel turkeyfish slimehead South American Lungfish torrent catfish. Ayu bull trout trumpeter; hussar, buri shad, lanternfish coolie loach, "mud cat: crappie pike eel; man-of-war fish." Shiner bamboo shark Black angelfish Rabbitfish Russian sturgeon coolie loach sablefish king-of-the-salmon. Brook lamprey Pacific herring prickly shark shiner sawtooth eel eel cod medaka. Redfish bala shark flounder, Old World knifefish Black prickleback large-eye bream. Long-whiskered catfish Manta Ray paradise fish deep sea smelt vendace skilfish sea raven brotula. Hagfish Long-finned sand diver tilefish knifefish Ganges shark New Zealand smelt."
        </p>
        <img src="humpback.jpg" alt="whale">
        <img src="yellow-fish.jpg" alt="yellow fish">
        <img src="red-fish.jpg" alt="red fish">
      </body>
    </html>
  3. Create a CSS file, and style your images:

    img {
      width: 32%
    }
  4. Refresh your page, and then change the window width to see how the page is affected. (spoiler: all widths follow the same rules)
  5. Add a media query section:

  6. Give it the parameter of max-width: 1080px. That means from 0 to the max of 1080px window width, this rule will apply. Anything over 1080 will fall back to the rules before the media query.

  7. Let’s have the views less that 1080px wide show a full-width photo.

  8. Refresh your webpage and change the width to see your media query in action!
  9. See the dimensions of your window by pressing command+option+i. Keep an eye on the upper right of your window as you change its width and a little dimensions box should appear.

    min v max width

  10. OK, we got to try “max-width”, now let’s experiment with min-width. Since we have everything 1080px width and less specified, we’ll add something crazy for “min-width: 1081px”, that is, everything 1081px and wider. css @media (max-width: 1080px) { img { width: 100%; } } @media (min-width: 1081px) { body { background-color: red; } }
  11. Save and change your html page window width again to see your red background at 1081px and wider.

    Overriding

  12. Add another media query after your red one, but make the background green starting at min-width: 1200px.

  13. Refresh you webpage and change the width to see your new styles. You should see:
  14. Next, highlight that 1200 media query, hold command+ctrl, and use the up arrow to move it before the 1081 rule. Refresh and try your webpage now.

  15. Practice now by going to your site window. Press command+option+i to open the inspector to check out what styles are being applied in the Elements tab. The green rule is not being applied.
  16. Click the ‘index.css:’ link by your style to see where the rule being followed is in your code. This should take you to to Sources tab, and show you which line it’s on.

Media Query Syntax

Independent Practice

Challenge

  1. Pair up with a peer and discuss what changes you would like to make to your recipe page project using media queries. Be specific about which widths you would like to use, which elements you’d change. Sketch your ideas to help you remember.
  2. Spend about 20 minutes applying your media queries to your recipe page!

Check for Understanding

Form small groups and discuss: