CSS (Cascading Style Sheets) is the code you use to style your webpage. CSS lets you apply styles selectively to elements in HTML documents.
Here's a simple example of using CSS to make all paragraph elements red.
p {
color: red;
}
Yale Architecture with CSS
Yale Architecture without CSS
As we've discussed before, you should take a moment to read a good intro to CSS, such as the MDN page So what is CSS, really?.
As a very short introduction, here are the parts to a CSS rule:
p
, or the name of an HTML element.color: red;
.color
, margin
, width
, and background
.red
is one way to write a color value, along with hex code (#000000
) and rgb (rgb(255, 255, 255)
) colors.In general, the best approach to learning CSS is to learn a handful of selectors and a handful of properties (along with their common values). You can then gradually look up additional properties and expand your knowledge as your design requires more advanced styling.
Here are a few resources to learning CSS properties:
There are way more CSS properties than you could ever memorize. Don't worry about it! Just look up the ones you need when you are working on a page design. You'll be surprised how quickly you start to memorize the common ones.
You can put CSS in a style
tag or reference an external .css
file.
<!DOCTYPE html>
<html>
<head>
<title>My Styled Page</title>
<link rel="stylesheet" type="text/css" href="css/custom.css">
<style>
p {
color: red;
}
</style>
</head>
<body>
</body>
</html>
The most common ways to select an element are by element name, class, or id.
<div id="cancel" class="button large">Cancel</div>
An id
is a unique name for a single HTML element. Elements can only have one id and an id can only refer to one element.
A class
is a name for one or more HTML elements. Elements can have many classes.
In this case, I wouldn't use div
as a selector for this element since I probably have many other divs on the page.
I could use #cancel
to select just this element (note the hashtag which signifies an id selector), but I probably want my button styles to apply to all buttons on the page.
In that case .button
is probably the best way to style this element and other buttons (note the dot which signifies a class selector).
.button {
background-color: lightgray; // selects all elements with button class
}
.button.large {
font-size: 18px; // selects all elements with button and large class
}
#cancel {
color: red; // selects just the element with cancel id
}
Since styles cascade or add up, the above CSS would make the above button element show a lightgray background, a large font size, and red text.
Colors can be represented 1 of 4 ways. Usually a color picker tool will show you all 4 values:
red
#ff0000
← this is the easiest to copy and paste when you're lazy.rgb(255, 0, 0)
← this is probably the best since it's precise and readable.hsl(0, 100%, 50%)
For more info, see color | MDN.
A variety of properties require specifying a length value. There are a lot of types of length values, but here are the common ones:
100px
50%
← this is useful for some things like columns that are a percentage of the width of their container1.5em
<html>
element e.g. 1.5em
← this is the most flexible since things will change sizes naturally as you zoom in and out of the page50vh
← this is useful for stuff that needs to be fullscreenFor more info, see length | MDN.
Many CSS properties are self-explanatory. You can probably figure out how to use font, color, background, border, box-shadow, text-decoration, and opacity.
My favorite overlooked CSS properties are white-space and letter-spacing for making precise typography style changes.
Layout in CSS is unfortunately less intuitive. It is, however, quite predictable once you learn some simple rules.
Conceptually, every element is structured like a rectangular box:
box-sizing: border-box;
is the most intuitive box model because the width and heigh of the element include its border and padding. Unforunately, it's not the default one. To opt-in add this to the top of your CSS file:
*,
*:before,
*:after {
box-sizing: border-box;
}
For more info, see box-sizing | MDN.
There are a number of display
values, but the most common ones are:
display: block;
Take up any available width, regardless of content & begin on a new linedisplay: inline;
Take up only the width of their content & continue on the same line display: flex;
Like block elements except with the ability to conveniently position child elements inside (see below)display: none;
Removes the item from the page completelyAll HTML elements have a default display type. Can you guess which common elements are block and which are inline?
For more info, see display | MDN.
There are a number of position
values, but the most common ones are:
position: static;
Default, doesn't do anythingposition: relative;
Nudge an element in any direction relative to where it startedposition: absolute;
Nudge an element in any direction relative to the nearest parent element with position: relative;
position: fixed;
Position an element relative to the browser window and make it stay thereIn order to "nudge" an element in a given direction, use the top
, bottom
, right
, and left
properties.
For more info, see Positioning | MDN.
Flexbox is a relatively new set of CSS properties that makes it easy to do a lot of things that were previously difficult in CSS. That power comes at the expense of a large number of options.
You should use flexbox any time you need to layout a number of items with specific intent about how things are centered and aligned.
Pick and choose options from a guide like the CSS-Tricks Guide to Flexbox.
For more info, see Flexbox | MDN or try the Flexbox Froggy game.
One of the most common uses of flexbox is to organize your page into columns:
.container {
display: flex;
justify-content: space-between;
}
.container .columns {
width: 33.3333%;
}
If you see CSS layout examples using something called a float
, you should try to avoid it at all costs. Flexbox largely supersedes float-based layout and will save a lot of unnecessary frustration with previous limitations of CSS.
Different browsers have different default styles for elements. It's helpful to start with a clean slate by applying a normalizing css "reset."
For example, Chrome applies padding: 2px 6px 3px;
to default button
elements while Firefox applies padding: 0 8px;
.
You can read more about resets in general here. It's good practice to include something like normalize.css in your html page before your custom css.
In order to make designs change in response to different environments or screen sizes, you can use media queries to conditionally apply different styles.
You can use CSS to animate styles and create motion on your page. Here is an introduction on MDN: Using CSS animations
Not all browsers support all CSS features. You can use these resources to see how widely the feature is supported:
The most common CSS property that requires prefixing is transform
:
.example {
-webkit-transform: rotate(30deg);
-ms-transform: rotate(30deg);
transform: rotate(30deg);
}
DevTools is indispensible for seeing the results of CSS changes live.
See the updated tools guide for DevTools tips and tricks.
Writing lots of CSS by hand can take a long time for complex designs. You can use a CSS framework to instantly add a polished theme to your page.
Check out these frameworks to begin:
Download the updated class folder from GitHub and open 2-style/
in Sublime Text.
Note: if you want to explore how developers share code, try to figure out how to install Git / GitHub Desktop to sync updates from the class folder automatically.
Understand the CSS behind the page and modify it change the layout.
Continue building your project and add CSS to start giving your pages structure.
Find a website that has an interesting layout. Try to understand (broadly) how the site uses CSS to accomplish the effect.
Come to class ready to discuss examples together.