Media Queries and Icons
Housekeeping
Homework
- Media Queries
- Read: Beginner’s guide to media queries
- Read: Using media queries by MDN
- Mobile-first design
- Definition: Mobile-first design
- Watch: The first 10 minutes of Are you writing responsive CSS the wrong way? by Kevin Powell
- Reference: Popular screen resolutions
- Specificity
- MDN: Specificity
- Tool: Specificity Calculator
- Icons
- Read: Font Awesome - Basic Use
- Reference: CDNJS
- Progressive Enhancement
1. Media queries
Learning Objectives
- Distinguish between mobile-first and desktop-first design patterns.
- Inspect the Cascade using the Rules tab in Developer Tools.
- Discuss the purpose and function of media queries.
- Declare conditional styles using
max-width
. - Declare conditional styles using
min-width
. - Declare conditional styles using
orientation
. - Declare conditional styles using
min-aspect-ratio
andmax-aspect-ratio
.
Materials
- Gist: Desktop-first:
max-width
- Gist: Mobile-first:
min-width
- Gist: Mobile-first:
orientation
- Gist: Squarish screens:
aspect-ratio
Key Takeaways
- Use media queries as a last resort. Flexbox and CSS Grid have responsivene features built-in that now replace many of the use cases for media queries.
- Media queries are positioned below your other declarations so that they will tend to override those styles.
- Position medium-width queries in order of progressing screen width width. Otherwise, some declarations may never trigger.
- For mobile first: default -> medium -> large
- For desktop first: default -> medium -> small
-
Although it’s optional, it’s good practice to include a media type (ex. screen) for each media query in the form:
@media media-type and (media-feature-rule) { /* Styles here */ }
- Instead of targeting common device widths (there are too many devices for this to be effective), change the design at the size where the content starts to break in some way.
- Treat layout as an enhancement and aim for a mobile-first design. Start with a simple, single-column design that works for mobile and then begin adding layout features in your media queries for larger screens.
- Try using relative widths (
em
,rem
,ch
, etc) instead of pixels. This avoids issues with devices that are zoomed in and keeps you focused on your design, not devices. - Avoid using these deprecated media features:
device-aspect-ratio
device-height
device-width
2. Font Awesome and CDNJS
There are a few options available for using Font Awesome. A popular option is to use CDNJS:
- Go to the Font Awesome library on CDNJS.
- Copy the top link that ends in
.../css/all.min.css
(or use a subset if that’s all you need). -
Link to this library using a
link
element in thehead
of your document:<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">
- See Font Awesome - Basic Use for details on how to use FA icons in your pages.
Activities
1. Basic responsive navigation
Refactor your Flexbox navigation from yesterday to make it responsive.
- Using a mobile-first method, create a default vertical navigation that will work on mobile devices.
- Using a media query for larger screens, make your navigation horizontal. Choose a screen width that makes sense for the width of your navigation.
2. Advanced: Hamburger icons
Refactor your responsive nav above to incorporate a hamburger menu:
- Using a mobile-first method, hide your vertical menu (
display: none
is easiest, but a popular alternative is to place it off the page with absolute positioning) and replace it with a hamburger menu icon. - Using your media query, replace the hamburger menu icon with your horizontal menu.
- Using the checkbox hack use CSS to show/hide your vertical menu when the hamburger icon is checked/unchecked.
3. Test your skills
- MDN: Media Queries and Responsive Design
- Note: Flexbox and/or CSS Grid knowledge will be helpful with this exercise.
Clean-up Time!
- Submit today’s Codepen/repo/gist to the Dailies section (in Assessments) in Brightspace.