An Intro to CSS
CSS stands for Cascading Style Sheets
CSS is the standard language/format for styling web pages, which specifies what the page’s HTML will look like in the browser.
-
CSS – MDN
MDN, as is custom. -
Basics of CSS
Another ASMR introduction from Laurel. -
Google’s
web.dev CSS Course
Different order from ours, but pretty good. -
HTML Color Codes
Too many ads, but some nice tools for color. -
Google Fonts
We’ll use this for free font families. -
Wakamai Fondue
“What can my font do?”
In our ongoing analogy, CSS is the .css
CSS came after HTML, first proposed by Håkon Wium Lie in 1994—who was working with our friend Tim at CERN and wanted more control over the presentation of web pages. (Tim was
- CSS 1, 1996
- CSS 2, 1998
- CSS 3, 1999
For the past decade or so, features have been added incrementally by browsers “within” the CSS 3 “standard” (as it was/is with HTML). That’s how it goes, these days.
The change in relationship between generator and consumer of information is going to take some getting used to.
•••
I’ll comment that style sheets constitute a wormhole into unspeakable universes. People start thinking they’ll just set up a little file […] and soon it grows uncontrollable.
Where CSS Lives
Before we get into the CSS syntax itself, let’s talk about how it is incorporated with your HTML.
There are three ways it can be added:
Inline on HTML tags themselves- Via
<style>elements in HTML documents - As separate/external
.cssfiles, via<link>elements
1. Inline with style=
This is the most straightforward way to add styles, directly as
<p style="color: red;">This text will be red!</p>
Seems obvious. However this has some downsides—imagine you want to style all of your paragraphs in the same way, and with multiple properties:
<p style="color: red; font-family: sans-serif;">This text will be red!</p>
<p style="color: red; font-family: sans-serif;">I’d also like this to be red.</p>
<p style="color: red; font-family: sans-serif;">And they are all sans-serif, too.</p>
It makes it hard to read, and hard to change and maintain—you’d have to update every single instance. (In software, we’d refer to this as
2. <style> in HTML
So the next way that was added to the standard was using a special HTML element, <style>, that wraps blocks of CSS that then apply to an entire document. They go up in the <head> of our HTML documents.
The rules are written written with selectors—more on those, below. But importantly, we can now control styling of all the paragraphs easily, at once.
<!doctype html>
<html>
<head>
<title>Page title</title>
<style>
p {
color: red;
font-family: sans-serif;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>This is third paragraph.</p>
</body>
</html>
3. External with <link>
So this is already much better, allowing us to style whole pages easily and consistently. But what about when we have
If you wanted a whole site to use the same styles, you’d have to duplicate the <style> tag over and over, updating it everywhere whenever it changes. Still brittle. So along comes the <link> element.
<!-- `index.html` -->
<!doctype html>
<html>
<head>
<title>Page title</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>This is third paragraph.</p>
</body>
</html>
And then in a separate style.css file (in this case, in the same directory as our HTML file), we can have the same rules as before—no need for the outside wrapping <style> tag.
This will apply to any page that we add the <link> to, and updating the styles will now change the color of the paragraphs for our
/* `style.css` */
p {
color: red;
font-family: sans-serif;
}
We’ll talk more about
Separation of Concerns
It’s
CSS Rules
Even though it is used to style HTML elements, the syntax of CSS is very different. CSS
-
CSS Syntax – MDN
They really need to update their diagrams. -
CSS Reference – MDN
Their exhaustive list goes into the hundreds.
The curly brackets { }
Properties are always separated from their corresponding values by a colon :, and each declaration line has to end in a semicolon ;. (It’s just how it is!) Also, there are no spaces between values and their units (like 2rem)! You will get used to it.
There are many, many, many CSS properties. We’ll go over some in our exercises, but look through these to become more familiar.
Ergonomics
Just like HTML, CSS
Capitalization id or classes as selectors, which have to match the HTML to target correctly.
Like with HTML, it’s easiest just to be consistent and stick to lowercase (and no spaces)!
p {
color: red;
font-family: 'Gorton', sans-serif;
}
/* Is the same as… */
P{COLOR:RED;FONT-FAMILY:'GORTON',SANS-SERIF;}
Basic Selectors
Selectors are used to
-
Type, Class, and ID Selectors – MDN
MDN again, as we do. -
Selectors – web.dev
Google, too.
- Elements (like
etc.)pamain - Classes (written
.class-name) - Identifiers (and
#some-id)
1. By Element Type: p a main etc.
If you want to change the styles for all instances of a given HTML element, you drop the < >
/* comment syntax */ too.
2. With a Class: .class-name
But maybe you don’t want to style all of the paragraphs. You can then use a class to target specific instances. They are added as an
The . as with .highlight and .faded.
You can use these over and over, on any kind of element. And individual elements can have
We’ll talk about how conflicting rules are handled, below.
3. With an Identifier: #some-id
You can also use an id, which is a kind of special attribute that can only be used
These are prefixed by # in CSS, as with #title and #introduction. If you remember, they can also be used as link destinations!
Fancy Selectors
Combinations and Groups: selector.selector
You can use combinations of the above
More commonly, you might apply declarations to multiple selectors, called
With Specific Attributes: selector[attribute]
You can use the various attributes as selectors too, using square brackets [ ]
Pseudo-Classes: selector:state selector:instance
These are special selectors, added to element, class, or id, separated with :, which target unique
:hover works on any element, not just links!
Other common pseudo-Class examples have to do with counts and positions. The syntax for these can be complicated, but they are very powerful:
Pseudo-Elements: selector::pseudo
Slightly different the various pseudo-::before and ::after, which let us insert things around text—or targeting first letters/lines:
: for pseudo-selectors and :: for pseudo-elements.
Finally, Combinators: > + ~
Last, you will often want to target something based on its relationship to other elements—its
Importantly, combinators can only target elements top-down, meaning that it can only “see” elements
The Golden Age of CSS
:has() Really …Has Changed Things!
For many,
CSS has finally added the :has() pseudo-class, just in the past couple years. It allows us to write much simpler, logical styles:
div:has(p) { background-color: red; }
The property is applied div) based on the presence of the child (the p). You can use any selector, in either position. This is
Oh Also, Nesting?!
While we’re on the subject of cutting-edge additions to CSS—even more recently browsers have added support for
This more straightforward style of writing descendent/child selectors was popularized by the ubiquitous SASS extension—which improved the ergonomics of CSS ahead of the language incorporating new features.
Instead of writing like this:
header,
footer { color: blue; }
header .child,
footer .child { color: teal; }
header .another-child,
footer .another-child { color: aqua; }
You can write like this:
header,
footer {
color: blue;
.child { color: teal; }
.another-child { color: aqua; }
}
…to make the hierarchical relationship self-evident, less redundant, and easier to change—especially as your stylesheets inevitably grow! Each level (generation?) can be any CSS selector.
These can dramatically improve your editing experience!
Specificity
We can’t talk about CSS without talking about
-
Specifics on CSS Specificity – CSS Tricks
A brief overview of a very complicated thing. -
Specificity Calculator
Compare selector values and see who wins.
The first three targeting methods (element, .class, #id) are listed in increasing order of id trumps a class. Identifiers are thus
You could write a
Oh Right, the Cascade
Yikes, we haven’t even talked about that first
-
Introducing the CSS Cascade – MDN
MDN is particularlydry on this one. -
The CSS Cascade
A much nicer interactive explanation from Amelia Wattenberger.
This means that when there is a tie (like two classes applying the same property), the <link> element, the lower linked document will take precedence:
And Inheritance
To add even more confusion, some CSS properties set on a parent also apply to their children—such as color or font-family. Most spacing/layout properties, like width and margin do not. (More on those, next week.)
Inheritance – web.dev
Google is better on this one.
This allows you to quickly set some properties globally, without having many brittle/redundant rules, as we did before:
body styles. Ah, finally, sans-serif.
Color and Type Properties
Alright, so all this has been about
Color
Besides the basic examples above,
CSS Colors – MDN
Come for the picker, stay for all the info.
tomato is a favorite.
Named colors are quick to work with when you know a few, but hsla (and recently, color-mix) offer a much more intuitive/human way to adjust and work with colors and transparency.
These can also all be applied to background-color (and border, but we’ll talk about that next week).
Fonts
Then perhaps most importantly, you’ll always be customizing your typography—starting with the font-family property. Remember, the web is text
Fundamental Text and Font Styling – MDN
All your properties.
Web font licensing is a
Other Type Properties
Once you’ve got a font-family in, there are many additional properties to control the typography:
Web Typography –
Interneting Is Hard
A more qualitative take.
rem, focusing on relationships. We’ll talk about other Resets
As we talked about last week, browsers have their own, built-in way that they display HTML elements. These
This is the “look” we have been seeing when we write plain HTML without any CSS—usually
Often, when you are working towards your own design, you will find yourself fighting against these built-in styles. So many designers/front-end folk instead start with
This means you have to write everything yourself, but you have more control and aren’t building on unknown foundations. And things should be (more) consistent, across browsers and platforms.
Here is a simple, modern one for your <head>:
<link href="https://typography-interaction-2526.github.io/assets/reset.css" rel="stylesheet">
This is what we use here for our course site!
The author of HTML documents has no influence over the presentation. Indeed, if conflicts arise the user should have the last word, but one should also allow the author to attach style hints.
The last point has especially been a source of much frustration among professions that are used to being in control of paper-based publishing.
This proposal tries to soften the tension between the author and the reader.