Introduction to CSS
17 April 2024
Updated: 17 April 2024
ZOOOOOOM!
Is everything big enough and are we in dark mode?
An Introduction to CSS
Agenda
- Basic Syntax
- Selectors
- Cascade
- Box Model
- Flow Layout
- Debugging
Let’s get some markup
Well, we need something to style
Syntax Overview
Some things you can see in this example:
- Declarations
- Rules
- Resets/Normalizations
- Class selectors
- Psuedo-class selectors
Then - like just draw the rest of the owl
Now let’s style it
Syntax Overview
Some things you can see in this example:
- Declarations
- Rules
- Resets/Normalizations
- Class selectors
- Psuedo-class selectors
Then - like just draw the rest of the owl
Selectors
Combining Selectors (Combinators)
First child of A that is B
Other B Child
Nested B Child
Another B Child
Some other content in A
B as a sibling of A
Some other B sibling of A
Combinators
First child of A that is B
Other B Child
Nested B Child
Another B Child
Some other content in A
B as a sibling of A
Some other B sibling of A
The Cascade
- CSS stands for “Cascading Style Sheets”
- The Cascade determines which specific styles get applied to an element
- Specificity of a selector matters
- Order of rules matter
Specificity
- Used by browser to determine the most relevant declaration
- Based on the selector used
- Type of selector, combinators used, and level of impact this
Order
- Where is the style in the stylesheet
- Used to break ties in specificity
Calculated Specificity
Rules for specificity are pretty tricky, but your IDE should show this to you when hovering over a class
First child of A that is B
Other B Child
Nested B Child
Another B Child
Some other content in A
B as a sibling of A
Some other B sibling of A
Note that all content in A is now Green. The Selector with the higher specificity takes priority even though it is defined earlier in the stylesheet
Psuedo-class Selectors
- Used in addition to other selectors
- Select an element based on DOM state
Some common Psuedo-classes
The Box Model
Rectangles all the way down
before block
content
after block
Flow - Block vs Inline Block
- Default layout Mode
- Block elements are placed top-to-bottom
- Inline elements are placed left-to-right and wrap with reading width
- Layout behaves and interacts differently
- Sizing is calculated differently
- Some properties will behave differently based on this
spacer
block
block
spacer
inline
inline
spacer
inline-block
inline-block
spacer
Debugging
Firefox has the best CSS Tooling (in general)
- Hover tooltips
- CSS Overrides
- HTML Breakpoints
- Flex and Grid Tools
before block
content
after block
And if you’re still stuck
Well, you can just:
References and Further Reading
Anything by Josh W. Comeau pretty much
MDN
CSS Tricks