liveBook · Manning

What you will learn in Math for Web Design

Precision layout and animation

by Paul McFedries

Chapter by chapter

Chapter 1: Web dev math fundamentals

1.1 Why bother learning about frontend math? · 1.2 Why math matters in frontend development · 1.2.1 The connection between mathematics and web design · 1.2.2 Real-world examples of math in UI/UX · 1.2.3 How developers use math in animations, layouts, and interactions · 1.3 The role of mathematics in CSS and JavaScript · 1.3.1 How CSS uses math · 1.3.2 JavaScript’s built-in mathematical capabilities · 1.3.3 Comparing CSS and JavaScript math approaches · 1.4 The minimum math you need to know · 1.4.1 Arithmetic and arithmetic expressions · 1.4.2 Algebra · 1.4.3 Ratios · 1.4.4 Exponents and nth roots · 1.4.5 Linear equations · 1.4.6 Inequalities and comparison expressions · 1.4.7 Geometry · 1.4.8 Trigonometry · 1.4.9 Coordinate systems · 1.5 Summary

What you learn in chapter 1 →

Chapter 2: Math basics for JavaScript

2.1 Building JavaScript expressions · 2.1.1 JavaScript arithmetic operators · 2.1.2 JavaScript arithmetic assignment operators · 2.1.3 JavaScript comparison operators · 2.1.4 JavaScript logical expressions · 2.1.5 Falsy values, default values, and nullish coalescing · 2.1.6 Operator precedence · 2.1.7 Using parentheses to avoid ambiguity · 2.2 Number types in JavaScript · 2.2.1 Integer and floating-point numbers · 2.2.2 Exponential notation · 2.2.3 Binary, octal, and hexadecimal representations · 2.2.4 NaN and Infinity in JavaScript · 2.2.5 NaN · 2.2.6 Infinity and −Infinity · 2.2.7 Number coercion · 2.3 Precision issues and floating-point arithmetic · 2.3.1 Why 0.1 + 0.2 !== 0.3 · 2.3.2 When to be concerned about precision · 2.4 Working with BigInt values · 2.4.1 BigInt to the rescue · 2.4.2 Mathematical operations with BigInt · 2.4.3 Limitations of BigInt in JavaScript · 2.4.4 When to avoid BigInt in web development · 2.5 Modulo logic and wrapping values · 2.5.1 JavaScript’s remainder operator (%) and modulo · 2.5.2 Wrapping logic in carousels, sliders, and cyclic UI · 2.6 Rounding numbers · 2.6.1 Rounding to whole numbers · 2.6.2 Rounding to a specific number of decimal places · 2.6.3 Implementing bankers’ rounding · 2.6.4 Use cases for rounding in frontend development · 2.7 Generating random numbers · 2.7.1 Generating random integers and floats · 2.7.2 Seeding and repeatable randomness · 2.7.3 Use cases in animations and UI behavior · 2.8 Solutions to exercises · 2.9 Summary

What you learn in chapter 2 →

Chapter 3: Math basics for CSS

3.1 The math of CSS measurement units · 3.1.1 Unit conversion math · 3.1.2 The cascading calculations of em units · 3.1.3 Percentage computation rules · 3.1.4 Viewport units and their responsive calculations · 3.1.5 Mixing units with calc() · 3.1.6 Combining units requires attention to scale · 3.2 CSS math functions · 3.2.1 CSS arithmetic operators · 3.2.2 Evaluating expressions with calc() · 3.2.3 Setting limits with min(), max(), and clamp() · 3.2.4 More CSS math functions · 3.3 CSS variables in math operations · 3.3.1 Basic usage · 3.3.2 Mixing units with variables · 3.3.3 Fallbacks and missing variables · 3.4 The math of CSS inheritance · 3.4.1 The inheritance cascade formula · 3.4.2 Which properties inherit mathematically? · 3.4.3 Percentage values and the inheritance math · 3.4.4 The em unit inheritance multiplier · 3.4.5 Solving the heading size puzzle · 3.4.6 Computed values vs. inherited values · 3.5 Box model math · 3.5.1 The default box model formulas · 3.5.2 How the box-sizing property affects the box model formulas · 3.5.3 Margin math and collapsing · 3.5.4 Percentages and the box model · 3.5.5 Using calc() for precise box model math · 3.6 Solutions to Exercises · 3.7 Summary

What you learn in chapter 3 →

Chapter 4: CSS Grid math

4.1 The mathematical foundations of CSS Grid · 4.1.1 Foundations of the grid formatting context · 4.1.2 The coordinate system approach · 4.1.3 The algebraic model · 4.1.4 Mathematical differences from other layout systems · 4.2 The Grid coordinate system · 4.2.1 The grid model · 4.2.2 Span notation and its mathematical meaning · 4.2.3 Working smarter with negative indexing · 4.3 The grid track sizing algorithm · 4.3.1 The algebra of grid line placement · 4.3.2 How fr units distribute available space · 4.3.3 Taking the box model into account · 4.3.4 The algorithm for calculating fr unit values · 4.3.5 Troubleshooting: When <flex> resolves to 0px · 4.4 The math of Grid’s track-sizing functions and keywords · 4.4.1 Controlling track sizes with minmax() · 4.4.2 Understanding auto in grid tracks · 4.4.3 The math of fit-content() · 4.4.4 The repeat() function and its mathematical distribution · 4.5 Solutions to exercises · 4.6 Summary

What you learn in chapter 4 →

Chapter 5: Flexbox math

5.1 The flex base size calculation · 5.1.1 The base size algorithm · 5.2 Understanding the flex factors · 5.2.1 The flex-grow calculation · 5.2.2 The flex-shrink calculation · 5.2.3 The flex shorthand · 5.2.4 Dealing with flex-grow values between 0 and 1 · 5.3 Taking min/max constraints into account · 5.3.1 Using min-width to limit shrinking · 5.3.2 Using max-width to limit growing · 5.4 Solutions to Exercises · 5.5 Summary

What you learn in chapter 5 →

Chapter 6: The mathematics of responsive design

6.1 The concept of responsiveness in web design · 6.1.1 Fixed vs. fluid: A visual thought experiment · 6.1.2 The mathematical mindset · 6.2 Media query logic · 6.2.1 Grid and Flexbox as your first responsive tools · 6.2.2 Why you might still need media queries · 6.2.3 The math behind media queries · 6.2.4 Using range syntax for cleaner media queries · 6.2.5 Media query tips · 6.3 Proportional layouts · 6.3.1 How percentage-based layouts work · 6.3.2 Nesting percentages · 6.3.3 When to mix percentages with fixed units · 6.3.4 Creating a liquid layout · 6.4 Viewport-based units · 6.4.1 The classic viewport units · 6.4.2 Logical viewport units · 6.4.3 The scrollbar problem · 6.4.4 New viewport units: the sv*, lv*, and dv* families · 6.4.5 Deciding which viewport unit to use · 6.5 Layout fluidity with CSS functions · 6.5.1 Why fluid layouts matter · 6.5.2 Using clamp() for fluid container widths · 6.5.3 The math behind custom fluid widths · 6.5.4 Calculating preferred clamp() values · 6.5.5 Fluid padding and gaps · 6.5.6 How to think about CSS math for layouts · 6.5.7 When to bring in JavaScript · 6.6 Solutions to exercises · 6.7 Summary

What you learn in chapter 6 →

Chapter 7: The mathematics of color

7.1 Color models and color spaces · 7.1.1 The RGB color model · 7.1.2 The HSL color model · 7.1.3 The LAB color space · 7.1.4 The OKLCH color space · 7.2 Converting between color models · 7.2.1 Converting RGB to grayscale · 7.2.2 Converting RGB to HSL · 7.2.3 Converting HSL to RGB · 7.3 Creating harmonious color schemes with math · 7.3.1 Complementary colors · 7.3.2 Analogous colors · 7.3.3 Triadic colors · 7.3.4 Split-complementary colors · 7.4 Specifying accessible colors with math · 7.4.1 The WCAG contrast requirements (AA vs. AAA) · 7.4.2 The contrast ratio formula · 7.4.3 Calculating relative luminance from color · 7.4.4 Calculating contrast ratios with JavaScript · 7.5 Light mode and dark mode mathematics · 7.5.1 Why basic inversion doesn’t work · 7.5.2 A better approach: Inverting perceptual lightness · 7.5.3 Hue and saturation adjustments for aesthetic consistency · 7.5.4 Generating a paired light/dark palette · 7.5.5 CSS implementation with media queries · 7.6 The math behind blend modes and opacity · 7.6.1 How blending works · 7.6.2 Alpha compositing · 7.6.3 Blending in CSS · 7.6.4 The separable blend modes · 7.6.5 The nonseparable blend modes · 7.7 Solutions to exercises · 7.8 Summary

What you learn in chapter 7 →

Chapter 8: Text and typography math

8.1 The math behind the typographic trinity · 8.1.1 General readability considerations · 8.1.2 When you know the font size you want to use · 8.1.3 When you know the line height you want to use · 8.1.4 When you know the line length you want to use · 8.1.5 Putting the formulas to work · 8.2 Fluid typography with CSS math functions · 8.2.1 Using vw and vh for scalable text sizes · 8.2.2 Setting constraints on dynamic font scaling · 8.2.3 Calculating the preferred clamp() formula for font sizes · 8.2.4 Example: Hero text for a landing page · 8.3 Applying a modular scale to text · 8.3.1 Creating a modular scale from a scaling factor · 8.3.2 Choosing a modular scale ratio · 8.3.3 Implementing typographic scales with CSS variables · 8.4 Solutions to exercises · 8.5 Summary

What you learn in chapter 8 →

Chapter 9: Trigonometry for web developers

9.1 The basics of trigonometry for web design · 9.1.1 Sine, cosine, and tangent · 9.1.2 Trigonometric units · 9.1.3 Converting between degrees and radians · 9.1.4 The inverse trig functions · 9.1.5 Trig functions in CSS and JavaScript · 9.2 Circular layouts and animations · 9.2.1 Creating a circular menu · 9.2.2 Animating circular motion · 9.2.3 Creating an animated spinner · 9.3 Wave effects · 9.3.1 Arranging elements along a wave path · 9.3.2 Animating elements with a wave effect · 9.3.3 Animating an element with a pendulum effect · 9.4 Directional calculations · 9.5 Solutions to exercises · 9.6 Summary

What you learn in chapter 9 →

Chapter 10: Vectors and geometry in UI design

10.1 Mathematical foundations: Points, vectors, and bounding boxes · 10.2 DOM geometry: Measuring and aligning elements with JavaScript · 10.2.1 Calculating the center point of an element · 10.2.2 Aligning one element relative to another · 10.2.3 Geometric hover effects · 10.2.4 Parallax scrolling with geometric precision · 10.3 Manipulating vectors in JavaScript · 10.3.1 Essential vector operations for DOM developers · 10.3.2 Using vectors to have an element follow the mouse · 10.3.3 Animating an element to a mouse-click position using vectors · 10.3.4 Dragging with geometric constraints · 10.4 Geometric clipping and motion paths · 10.4.1 Clipping elements · 10.4.2 Motion paths · 10.5 Solutions to exercises · 10.6 Summary

What you learn in chapter 10 →

Chapter 11: Using Bézier curves for smooth animation

11.1 Introduction to Bézier curves · 11.1.1 What is a Bézier curve? · 11.1.2 The mathematical construction of Bézier curves · 11.1.3 Quadratic vs. cubic Bézier curves · 11.1.4 The role of cubic Bézier curves in animations · 11.2 Cubic Bézier curves in CSS · 11.2.1 The cubic-bezier() function · 11.2.2 Understanding the four control point values · 11.2.3 Built-in CSS easing keywords · 11.3 Customizing animations with Bézier curves · 11.3.1 Entrance animations · 11.3.2 Exit animations · 11.3.3 Interactive feedback animations · 11.4 Answers to exercises · 11.5 Summary

What you learn in chapter 11 →

Chapter 12: Physics-based animations

12.1 Understanding motion in UI design · 12.1.1 How real-world physics improves user experience · 12.1.2 Why animations feel natural when physics is applied · 12.1.3 JavaScript easing functions for natural motion · 12.2 Velocity and acceleration in JavaScript · 12.2.1 Applying the velocity equation in animations · 12.2.2 Handling acceleration · 12.3 Simulating gravity and bouncing effects · 12.3.1 How to create a bouncing-ball animation · 12.3.2 Using Math.sin() for pendulum-like motion · 12.3.3 Simulating weight and elasticity · 12.4 Spring- and friction-based animations · 12.4.1 Using Hooke’s law for spring motion · 12.4.2 Simulating inertia with damping and friction · 12.5 Answers to exercises · 12.6 Summary

What you learn in chapter 12 →

Chapter 13: Interactivity and gesture-based UI math

13.1 Mouse and touch event calculations · 13.1.1 Capturing the cursor position · 13.1.2 Detecting touch gestures · 13.1.3 Converting screen coordinates to local element coordinates · 13.2 Custom slider controls · 13.2.1 Building a custom linear slider · 13.2.2 Building a custom rotary control · 13.3 Scroll-based animations · 13.3.1 Detecting scroll position · 13.3.2 Creating dynamic UI changes with intersection observers · 13.4 Solutions to exercises · 13.5 Summary

What you learn in chapter 13 →

Chapter 14: Canvas-based math and drawing

14.1 Data-driven geometry and hit detection · 14.1.1 Drawing a pie chart from data · 14.1.2 Creating an interactive bar chart · 14.1.3 Advanced hit detection on a donut chart · 14.2 Dynamic motion and simulation · 14.2.1 Simulating a simple particle system · 14.2.2 Creating a generative star field effect · 14.2.3 Creating celebration and feedback effects · 14.3 Solutions to exercises · 14.4 Summary

What you learn in chapter 14 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.