CSS (Cascading Style Sheets) is a declarative language for describing document styling. Also see CSS Cookbook and SCSS.
Core
CSS files are just a sequence of rule sets which consists of a selector and a declaration block: CSS can be applied in a few ways:
- Inline on an element, such as
<h1 style="color: green;">
. - Inside a
<style>
element. - Linked externally with
<link ref="stylesheet" href="...">
.
Selectors
A selector is a string that targets the HTML element you want to apply some styling rules to. A selector has a specificity value. When two styles conflict (which often happens because of inheritance, which style is chosen is based on which selector has a higher specificity.
Selector | Syntax | Result | Specificity |
---|---|---|---|
Element | div | All div s. | 1 |
Class | .className | All elements with class="className" . | 10 |
ID | #id | Element with id="id" | 1000 |
Universal | * | All elements. | 0 |
Attribute | div[attr] or div[attr="val"] | All divs with attr or attr="val" . | 10 |
Pseudo-class | div:pseudo-class | All div s that satisfy a certain state (pseudo-class). | 10 but depends. |
Pseudo-element | div:pseudo-element | The specific pseudo-element of all div s. | 1. |
Descendant combinator | div p | All p s inside a div . | |
Child combinator | div > p | All ps that are direct children of a div . | |
Adjacent sibling combinator | div + p | All p s that follow directly after a div . | |
General sibling combinator | div ~ p | All p s that follow after a div . |
Inheritance
When you apply div { color: green; }
, all children also implicitly have the rule color: green;
set.
Box Model
Every element follows the box model below.
height
andwidth
affect only the inner content box, so the actual dimensions of the element will take up more space if you have padding, border and margin. To change this, setbox-sizing: border-box
to use the alternative CSS box model.
Display
Boxes are either block-level or inline-level. You change this by setting:
When you do
display: flex
ordisplay: grid
, you’re changing the inner display type, meaning that the children will behave according to the Flexbox or Grid specification.
Also, when you do display: inline-flex
, you are making the container display inline rather than block, not the children.
At-Rules
At-rules are special instructions.
Name | Example | Description |
---|---|---|
import | @import "path" or @import url(...) | Brings in another stylesheet. |
font-face | @font-face { font-family: "My Font"; src: url(...) format('truetype'); } | Defines a custom font. |
media | @media screen and (min-width: 300px) { ... } | Executes a media query to conditionally apply styles. |
keyframes | @keyframes myAnim { from {...} to {...} } . See docs. | Defines myAnim which you can attach to an element by doing animation: myAnim 1s infinite; for example. |
There are way more at-rules. |
Pseudo-Class
Pseudo-classes represent special states of an element that you can select for using selector:pseudo-class
. See all of them on MDN.
Pseudo-class selector | Description |
---|---|
:hover | When mouseover on an element. |
:active | When mousedown -ed on an element. |
:focus | After an element is clicked on, or when focused on by tab. |
:first-child | The first element in a group of siblings. Eg. li:first-child selects the first li . |
:last-child | Like first-child but selecting the last. |
:nth-child | Given a linear function of n , selects every f(n) element for n = 0, 1, ... . Eg. li:nth-child(2n+3 ) selects 3rd, 5th, etc. |
Pseudo-Element
Pseudo-elements are some specific parts of an element. They’re selectable with ::pseudo-element
. See all of them on MDN.
Pseudo-element selector | Description |
---|---|
::before | |
::after | |
::first-letter |
Position
Media Query
Media queries let you conditionally apply styles based on the user’s screen size (among other things). This, Flexbox and Grid are the main tools for implementing responsive web designs.
Note: a big part of responsive design is just making elements shrink in width when the viewport shrinks in width. You can accomplish this with media queries, however it’s cleaner to just something like this:
Here, an element will default to a preferred width of 50%
but will fall back to 200px if the viewport is too small.
Flexbox
Flexbox is a layout model for getting things neatly arranged on a row or column. For layout in 2-dimension, use Grid.
To make a flex container, give it display: flex
.
(images sourced from css-tricks)
Grid
To make a grid container, do display: grid
and set the number of rows and columns with grid-template-rows
and grid-template-columns
.
Any direct child element of the grid container is a grid item and can be positioned on the grid using the following properties:
(graphics sourced from css-tricks)
Just remember:
- ‘justify’ is for horizontal/x-axis, ‘align’ is for vertical/y-axis.
- ‘content’ is for container, ‘items’ is for items.
CSS Code Style
Some simple guidelines for writing maintainable CSS code. See MaintainableCSS.
- Prefer semantic class names.
”… use values that describe the nature of the content, rather than values that describe the desired presentation of the content”
- Stick with a naming convention like this:
- Avoid using IDs just as hooks for styling.
- Avoid undoing styling rules, eg. resetting styles provided by bootstrap or by another one of your CSS rules elsewhere.
- Write comments as if you were documenting regular program source code, especially if you write rules that are meant to solve an issue. CSS is usually a huge pain for people, so please remember to help your future self, and especially others working with your code.