Cascading Style Sheets
Test it out with your Inspect Element.
Because elements are boxes, we can change how they behave!
display: block
or display: inline
float: left
or right or none, etc.position: absolute
or fixed, sticky, relative, or staticHow?
<link>
element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
...
USE this method.
DO NOT copy/paste this code below.
The quotation characters will cause your link element not to function. Instead, write it out manually.
<style>
element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<style type="text/css">
/* ... */
</style>
</head>
<body>
...
DO NOT use this method.
<body>
<h1 style="color: red;">
Hello World
</h1>
<p style="margin: 2em 0;">
Paragraph here
</p>
</body>
DO NOT use this method.
@import
rules@import url(normalize.css);
@import url(layout.css);
@import url(typography.css);
nav {
position: fixed;
top: 0;
left: 0;
height: 3em;
}
Use this method, when your site's big enough.
Performs worse than the <link>
!
See don’t use
@import.
property: value
{ declaration; declaration; ... }
/* single-line */
{ display: block; height: 300px }
/* multi-line */
{
display: block;
height: 300px;
}
Semicolons (;
) are only required between declarations.
Pattern matching against document trees
Selectors help you point to (select) specific HTML elements.
Basic simple selectors | Examples |
---|---|
Type selectors | HTML tags: nav , section , header , p |
Class selectors | HTML classes that uses dot scheme in CSS: .ds-body-img |
ID selectors | HTML classes that uses hash scheme in CSS: #data-story-personal |
Universal selector | Selects all-the-HTML-things: * |
Attribute selectors | Selects HTML tag attributes: [title] , [rel~="copyright"] |
Pseudo classes | Adds a CSS-defined class to an HTML element: :hover |
Combinators | Example |
---|---|
Descendent combinator (␣ ) |
.section__text_container p {...} |
Child
combinator (> ) |
ul > li {...} |
Adjacent sibling combinator (+ ) |
h1 + h2 {...} |
General sibling combinator (~ ) |
img ~ p {...} |
HTML
parent containing block rules.
Containing blocks are established by parent box-context. (See W3C spec.)
::first-line
::first-letter
::before
::after
Type | Example |
---|---|
Location | :link , :visited , :target , ... |
User action | :hover , :focus , ... |
Tree-structural | :first-child , :nth-child() , ... |
Review Wattenberger's interactive CSS Cascade reference.