You're seeing this because you're a teacher 🤓

🎯 Your main goal for this lesson

This is really the first lesson wherein a problem-solving mindset is valuable. As you can see in the notes below, students will have some bumps in the road during this lesson. Your guidance in helping them understand how to work through problems will set the tone for the remainder of this bug-filled course.

🧠 Things to keep in mind when teaching

HTML, arguably, is easier than CSS. When you have your syntax written correctly, things just appear on the page. CSS can be frustrating and students will start to encounter their first real points of friction here. Help them learn how to debug by talking through what they think their code is doing vs. what it actually is doing.

🚀 Lesson Plan

Activity One: 20 minutes

Our big focus on CSS is helping students understand how to select and apply style rules to elements. This is going to take some practice by you, and a bit of riffing since everyone's classroom is different, but you're looking to replicate and repeat - several times - a process like this:

  • Look for students wearing the same color shirt or same type of pants
  • Call them out by name and have them do something goofy (may we suggest impersonating Ben Wyatt?)
  • Ask students, "Does anyone know why these students were selected?"
  • Do this a few more times with different permutations and selections of students and see if they start to catch on
  • You're selecting students that are similar and applying certain rules to them; literal selectors!
  • You can get creative with this process and start going for people sitting at the same table (siblings), etc.

Activity Two: 30 minutes

Like our previous lesson, walk students through importing and writing CSS in their project. Give them time to start developing their own classes and selectors to begin styling the appearance of their site. Our next lesson will focus on creating a layout; this may frustrate them, but encourage them to focus on the type, colors, and feel of the site.

Activity Three: 10 minutes

Take the formative assessment at the end of class. Work towards mastery.

CSS Selectors

By the end of this lesson, you'll be able to...

  • Create a CSS document for a site.
  • Utilize type and class selectors to target elements.
  • Utilize pseudo-classes to target specific child elements.

Overview

If you've reached this lesson, hopefully your site's HTML is fully scaffolded out. If so, congrats! If not, head back to the previous lesson for help and make sure you have a relatively complete skeleton for your site. Why? We're going to be learning how to style and arrange our content; without content, it's going to be hard to get things how you'd like.

Importing CSS

As we learned earlier, there are several ways of getting CSS into our page.

  • We can include a <link> tag in our <head> element that has an attribute of <href> (hypertext reference) set equal to the path of our CSS file.
  • We can write styles inline as a value of the style attribute on any HTML element.
  • We can create a <style> block on our page that contains a list of rules specific to this HTML page.
1<html> 2 <head> 3 <title>Hello, World!</title> 4 <!-- We can import CSS --> 5 <link rel="stylesheet" href="style.css" /> 6 </head> 7 <body> 8 <!-- We can write styles directly inline --> 9 <h1 style="color: blue">Hello, World!</h1> 10 </body> 11 <style> 12 /* We can write styles inside a style tag */ 13 </style> 14</html>

For the purposes of this project, we'll use our first option by importing a dedicated CSS file that will house all our style rules.

CSS Selectors

We covered selectors briefly in an earlier module. For this lesson, we'll work through ways of selecting types and creating classes. We'll even go for some less common, more specific strategies at the end.

Type

From our second module, we learned that type selectors refer to types of elements. These are the 'built-in' HTML tags. Below, you'll see a series of colors assigned to the backgrounds and texts of different HTML elements.

1/* Applied to all elements in the <body> */ 2body { 3 color: white; /* text */ 4 background-color: rgba(3, 5, 24, 0.87); /* background */ 5} 6 7/* All <h1> elements will get these styles */ 8/* Since this is more narrow in scope, these rules will override the ones applied to the <body> */ 9h1 { 10 color: green; 11 background-color: grey; 12}

Class

We also learned that HTML allows us to add classes to our HTML elements. This is handy because it enables us to differentiate between different 'components' on a page. While <div> is a super common tag, we could make a <div> with a class of something more specific. For example:

1<div class="scoreCard"> 2 <div class="teamInfo"> 3 <h2>Team One: 12 points</h2> 4 <h2>Team Two: 10 points</h2> 5 </div> 6 <div class="question"> 7 <h3>Question #6</h3> 8 <p>A really difficult trivia question?</p> 9 </div> 10</div>

With three different classes above, we can write specific rules for each class. For syntax, class selectors are prefixed with a period to denote we're writing rules for a class.

1.scoreCard { 2 padding: 40px 20px; 3 border: solid 1px black; 4 border-radius: 13px; 5} 6 7.teamInfo { 8 width: 100%; 9 height: 5vh; 10} 11 12.question { 13 padding-top: 50px; 14 text-align: center; 15}

Combining

We can even combine class selectors with type selectors to get super-specific on our elements. Let's say we wanted special rules for our <h3> element inside of the question class, we could simply write:

1.quetsion h3 { 2 color: tomato; /* Yes, real color!*/ 3}

High-brow

CSS allows us to be super specific and select only certain children via a selector. All HTML is organized in a nested fashion where certain elements are considered parents; those parents have child elements. Elements can be siblings of each other inside a parent element.

So, let's say we have a ordered list of runners on a score table. What if we want the first runner to have their name in green to signify they're first?

1<ol class="runners_list"> 2 <li>Lisa</li> 3 <li>Bart</li> 4 <li>Homer</li> 5</ol>

We'll use the class selector to grab the <ol> of runners_list and then select only the first child using something called a pseudo-class.

1.runners_list :first-child { 2 color: green; 3}

What if we want Homer to feel bad and have his text read red?

1.runners_list :last-child { 2 color: red; 3}

Pseudo-classes are great in that they focus on a special type of state, or condition of an element. These will be really handy when we start to see how we can affect the rendering and styling of elements based on certain events.


Assignments:

Your teacher may elect to give you this assignment in conjunction with this lesson or at a different time. Pay attention to your teacher's instructions for how/when to complete this assignment!

Client site: Style


Previous and Next Lessons:

Starting a Project

Creating a Layout with CSS