CSS: Fundamentals

Learning Goals

  • Use Chrome Dev Tools to debug CSS
  • Demonstrate understanding of:
    • selectors and specificity
    • sharing styles across multiple elements
    • the box model
  • Start to use CSS to match a provided comp

Vocabulary

  • CSS Cascading Style Sheets
  • HTML element A building block that makes up the structure of a web page
  • id / class attributes on HTML elements that allow us to identify HTML elements in our CSS
  • property (CSS property) The name of a style property of an HTML element (e.g., color, border)
  • value The value that is paired with a given style property
  • declaration A property/value pair within a CSS rule
  • declaration blocks multiple declarations
  • selector used to target HTML elements on our web pages that we want to style
  • rule selector(s) and a declaration block come together to create a rule

What is CSS?

CSS can do SO MUCH more than just add color or make things pretty. CSS can be used to create artwork like this project by Kassandra Sanchez. CSS can also make complex processes more intuitive and accessible users, like TurboTax and airbnb. CSS is a powerful tool for frontend developers. Let’s jump in to see how it works!

It’s a “style sheet language” that lets you style the HTML elements on your page. CSS works with HTML and controls the positioning, sizing, colors, and specific fonts on your page.

CSS can be implemented as inline css, internal css or external css. We will always implement CSS externally with a css file that gets linked in the HTML document - the approach used by modern applications. Why don’t we want to use inline or internal CSS styling?

Anatomy of a Basic CSS Rule

CSS Rule

Selectors

There is a class and id attribute available to use on every HTML element. In addition to the plain old element names themselves, these attributes are selectors that allow you to “target” and manipulate HTML elements in CSS and in JavaScript.

We can target HTML elements in CSS in many ways:

// by element name
h1 {
  color: red;
}

// by class name
.primary-font {
  font-family: "Montserrat", sans-serif;
  font-size: 45px;
}

// by id name
#about-page {
  background-color: lightgrey;
}

// by a combination of above
.primary-font p {
  font-size: 15px;
  color: darkgrey;
}


Sharing styles across multiple elements

Remember: an HTML element can have zero, one, or multiple classes applied to it. Class attributes can help us write cleaner and more DRY (don’t repeat yourself) CSS because we can apply the same class to multiple elements in our HTML that share certain styles. Then, in our CSS, we simply apply all the shared styles to that shared class. We can also apply CSS declarations to multiple elements at once by stacking the selectors.

For example:

<h1 class="italic">Hello, World!</h1>
<nav>
  <button class="nav-button">Home</button>
  <button class="nav-button">About</button>
  <button class="nav-button">Contact Us</button>
</nav>
<p>Very important text</p>
<input type="text" id="name" name="name-input">
<button class="submit-button italic" type="submit">Submit</button>
<p class="italic">Yes, yes, very very important info here</p>
<button class="delete-button">Delete</button>
//stack selectors and separate with commas
.nav-button, 
.submit-button,
.delete-button {
  border-radius: 5px;
  color: blue;
  padding: 10px;
}

//individually apply styles that are not shared
.nav-button {
  background-color: black;
}

.submit-button {
  background-color: grey;
}

.delete-button {
  width: 100px;
}

//Apply italic styling to the h1, a p tag and the submit button all at once via this shared class
.italic {
  font-style: italic;
}

Unique IDs

IDs help us accomplish a similar goal as classes do. The difference is that IDs should be unique, never used more than once. Think of it like this:

  • A class of students has a code like 2408. All students share a piece of information that applies to them.
  • An individual student has a personal ID number, like a drivers license or SSN, etc. that is unique to them.

Specificity

CSS Specificity is a mechanism within the CSS cascade that helps browsers resolve conflicting rules. Suppose you have two (or more) conflicting CSS rules that are being applied to the same element. In that case, the browser follows some rules to determine which one is most specific and that particular CSS declaration will take precedence, with the others being overridden.

You don’t need to memorize the specificity rules, but its worth doing some additional research to wrap your head around the concept. The CodePen below will demonstrate some examples of this.

See the Pen CSS Rules by Kayla Wood (@kaylaewood) on CodePen.

Explore

With your partner, use the CodePen above to explore.

  • Uncomment each line in the CSS file at a time (starting at the top). Before you uncomment, make a prediction about what you will see on the page.
  • Do you notice a pattern for what styling is taking precedent?
  • Write down any questions that come up!

Dev Tools and CSS

Developer Tools, or Dev Tools, are available to use in every browser. They help us debug, experiment, and test assumptions in our HTML, CSS, and JavaScript quickly and with a low-risk in the browser. They’re your friend when it comes to understanding how CSS works (or untangling why it isn’t working) – get in the habit of using them early and often!

Editing CSS

To the right of the HTML pane, there’s a small sidebar that gives us styling information for the currently selected element. Similar to the HTML pane, we can add or remove styles and adjust CSS property values from this pane. You can click on any style property associated with the selected element and change its value. You can also use the blue checkbox to toggle the style on or off.

Editing CSS

Dev Tools Challenge

Directly from the CSS pane, we can edit the CSS and see the changes reflected immediately.

Let’s make the following edits on this lesson page:

  • Make the title of this page have a font size of 200 pixels.
  • Change the left border color of all elements with a .call-to-action class to purple.
  • Make all of the h2 elements red.
  • Change the font of the whole page to serif

Discussion Questions:

  • Experiment with deselecting a few CSS styles. What happens?
  • What do you think it means when a style is crossed out?
  • Refresh the page. What happened to your changes? Given that they don’t persist, why/when would using the Dev Tools be useful?

The Box Model

Each element’s content is in a rectangular box. CSS leverages the box model to control layout and design. An HTML element is comprised of its content and the padding, borders, and margins surrounding it. Boxes are “stacked” in the order they appear in your HTML. You can stack them horizontally, vertically, and in the z-plane.

This diagram can be found in the CSS Dev Tools and the same color coding is used when we hover over elements in the browser with the inspector selected.

Box Model

Tip: When trying to get more visibility into a specific element, put a border around it. This is particularly helpful when trying to see the spacial parts of an element like the padding and margins.

Recreating Comps

We can apply all the margin and padding we want, but when it comes down to it, many times Front End Developers are tasked with re-creating something a designer has provided them with, commonly called “comps”. Let’s use our tools to build something professional-grade.

Throughout both Mod 1 CSS lessons, you are going to work on recreating a piece of the Imperfect Foods website.

Today you will work on iteration 1 of this activity. You will work on iteration 2 after you’ve had the CSS Flexbox lesson.

Wrap Up

Solo Journaling

  • How does CSS differ from HTML?
  • Explain the Box Model
  • What questions do you still have about CSS?

Additional Resources

Lesson Search Results

Showing top 10 results