3 Math basics for CSS
This chapter covers
- CSS measurement units
- Calculating values in CSS
- CSS math functions and variables
- The math of CSS inheritance
- The mathematics of the box model
CSS is, in a word, tricky. You carefully hand-code each declaration, combine them with precision into a style rule, refresh your browser, and the result is all too often an unholy mess that looks nothing like the clean, professional design you imagined in your head. Welcome to the frequently frustrating, always adventuresome world of CSS!
It’s true that CSS can make even the most Zen-like designer become a puller-of-hair or a gnasher-of-teeth. But if you’ve come to believe that wrestling with CSS is a no-win situation, may I offer some good news? Many, perhaps even most, CSS problems are math related and eminently fixable. Surely, there must be a catch! And there is! You have to learn the basics of how CSS uses math, which is what this chapter is all about. Here you’ll learn all about CSS measurement units and operators, the most useful CSS math functions, how to wield CSS variables in math operations, the ins and outs of box model math, and much more.