This time we will focus on debugging CSS styles when working with React. We will hunt down a few example bugs with browser Dev Tools Inspector and identify good ways how to fix them. Vertical alignment, flexbox, animations, responsive designs, CSS-in-JS, overwriting default library styles and other useful tips await.
Before workshop:
git clone https://github.com/msd-code-academy/react-workshop.git
cd react-workshop/03-css-debug/
npm i
At the workshop start:
git pull # because we might do last-minute fixes
npm start
cd src/examples/
- 00 Intro
- Using browser inspector (right click > Inspect)
- No step-by-step debug
- So good class names are more important
- Or at least React component names, including HOC
- Temporary background / outline also useful
- Know thy framework / use consistent code style to simplify search in code
- Choose a library that does not use !important rules
- CSS vs CSS-in-JS => hot reload vs cleaner code over time
- Using browser inspector (right click > Inspect)
- 01 Vertical alignment
- Parent height
vertical-align
is only for inline and table cells- Know thy flexbox (or trial & error)
- 02 Z-index
- Stacking context
static
content is for elements that keep to themselves- if they need to overlap in arbitrary ways, best to go
absolute
- or
grid
(order in HTML according to Z order, manual row+column for position)
- 03 Selector specificity
- CSS Cascade and Specificity
- Simplified model - do a sum of "values" for each part of the selector:
- 1 for
div
or::before
- 10 for
.class
or:hover
- 100 for
#id
- 999 for inline
- 1000 for
!important
in the rule
- 1 for
- 04 Responsive design
- Always test, open multiple windows or connect devices
flex-wrap
,grid-template-columns
,<Col md={12} />
,min-width
, ... not just @media
- 05 Transitions
transition
property applied from the "after" / "new" selectorheight: auto
cannot be calculated, usemin/max-height
or fixed heights for transitions (or JS library)
- Q&A