← Math
Math

Equations, inequalities, and constraints

How many cards fit before the row overflows?

Turn layout limits into a whole-number plan that satisfies every constraint.

Outcome

Turn a layout limit into an inequality, solve for whole-number maximums and minimums, and recognize when no layout exists.

Terminology (7)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Equation
A statement that two expressions are equal. Solving it finds the values that make it true.
Inequality
A statement that one expression is less than, at most, greater than or at least another. Its solutions usually form a range.
Constraint
A rule a solution must satisfy, such as a row not exceeding its container or every card appearing within a row limit.
Isolate
Rewrite an equation or inequality so the unknown stands alone on one side. Dividing an inequality by a negative quantity reverses its direction.
Bound
A limit on the unknown: an upper bound from “at most”, a lower bound from “at least”.
Floor and ceiling Also ⌊x⌋, ⌈x⌉
The greatest integer not above x, and the least integer not below x. A whole-number maximum uses the floor; a minimum uses the ceiling.
Feasible
Satisfying every constraint. A problem whose constraints no value satisfies is infeasible; that is different from a feasible answer of zero.

Why this matters

Layout counts, batch sizes and deadlines are limits: at most this width, at least this many rows, no later than this time. Dividing the limit by one cost and rounding produces a whole number that can still break the limit. Writing the rule as an inequality makes every cost visible and tells you which way a whole answer must round.

This lesson assumes the earlier units and ratios work and one-unknown algebra. The model is a planning check; the browser’s layout remains the authority for what renders.

Equations describe costs; inequalities bound choices

An equation states that two expressions are equal. It can define a cost, such as the width used by a chosen number of cards. An inequality compares that cost with a limit: at most the available width, or at least the capacity needed for all cards.

Each such rule is a constraint. A feasible choice satisfies every constraint and belongs to the allowed domain. When the domain is whole counts, an algebraic range must contain an allowed integer; a fractional solution alone cannot specify a number of cards or rows.

01Define the quantities

Name every cost in the row

A shop’s virtualized grid draws only the product rows on screen. To choose those rows, it first needs a column count. Its estimate rounded 1,000 px ÷ 220 px to 5 cards, but five need 1,212 px with padding and gaps. How many fit, and can that count also meet a row limit?

The category grid shows product cards in rows. Because it is virtualized, it positions each row from its own arithmetic rather than letting CSS wrap the cards. The design specification gives the numbers below, and the question is how many cards fit in one row without overflowing.

Row quantities
SymbolMeaningWorked value
WRow width, including padding1000 px
PPadding on each side24 px
CMinimum card width220 px
GGap between adjacent cards16 px
nCards in the row (the unknown)whole, ≥ 0

The model counts padding, cards and gaps. Borders, scrollbars and text that forces a card wider are outside it; add them as costs if the real component has them.

02Write the cost

Write the width a row uses

How many pixels does a row of 4 cards use?

The n − 1 is a fencepost count: posts and gaps differ by one. The empty row needs its own case, because substituting n = 0 into the first formula subtracts a gap that does not exist.

03Solve for the largest count

Solve the inequality for the largest whole count

The row must not overflow: used(n) ≤ W. Collect the n terms, then divide by the positive quantity C + G, which keeps the direction of the inequality:

Check the answer in the original rule: used(4) = 976 ≤ 1000 and used(5) = 1212 > 1000. A row that exactly fills its width also satisfies ≤. Dividing W by C instead gives 4.55, and rounding that to 5 overflows by 212 px.

Dividing or multiplying both sides by a negative number reverses the direction: −2x ≤ −6 means x ≥ 3. Here C + G is positive by definition, so no reversal occurs.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const width = 1000; // px, including padding
const padding = 24; // px on each side
const card = 220; // px minimum card width
const gap = 16; // px between adjacent cards

const used = (count: number) => 2 * padding + count * card + (count - 1) * gap;
const bound = (width - 2 * padding + gap) / (card + gap);

console.log(`used(4) = ${used(4)}`);
console.log(`used(5) = ${used(5)}`);
console.log(`max = ${Math.floor(bound)}`);

TypeScript numbers are doubles, so the bound is 4.1016949… and Math.floor turns it into the largest whole count, 4.

A compact grid has W = 754 px, P = 18 px, C = 210 px and G = 14 px. How many whole cards fit?

04Minimums round up

A minimum rounds the other way

Now the unknown is a number of rows. Seventeen cards appear four to a row, so the rows r must satisfy 4r ≥ 17, or r ≥ 4.25. This is a lower bound: the least whole r is ⌈4.25⌉ = 5. The inequality sets the rounding direction: a maximum rounds down, a minimum rounds up.

Predict what this program prints before checking how each language divides.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const cards = 17;
const perRow = 4;

console.log(cards / perRow);
What does this program print?

05Combine constraints

Two limits can leave no whole answer

A real plan has both kinds of limit. Take a 1,100 px row (same padding, cards and gaps) and 22 cards that must appear in at most 5 rows. The cards per row k must satisfy two inequalities: k ≤ 1,068 ÷ 236 ≈ 4.53 so the row does not overflow, and 5k ≥ 22, so k ≥ 4.4, to fit the cards into five rows.

Is there a layout that satisfies both limits?

“No layout” differs from “zero cards”. A 200 px row with 24 px padding fits no 220 px card: the bound is (200 − 48 + 16) ÷ 236 ≈ 0.71, so the largest count is 0. If there are no cards to show, zero rows satisfy every limit: a feasible answer of zero. If there is one card, no layout exists. A function that returns 0 for both cases loses that distinction, and dividing by a count of zero is undefined in mathematics and an error or worse in code.

06Vary inputs, then decide

Change one input at a time, then decide

22 cards in at most 5 rows, unless stated
ChangeUpper bound on kCards per rowPlan
W = 1000 px4.104No layout: 5 per row needed
W = 1000 px, G = 32 px3.903No layout
W = 1100 px4.534No layout
W = 1212 px5.0055 per row, 5 rows
W = 1100 px, at most 6 rows4.5344 per row, 6 rows

Working backward from the limit gives the decision. Five cards per row need used(5) = 1,212 px, so the five-row design needs at least 1,212 px. At 1,100 px the grid needs a sixth row instead. A 32 px gap alone drops the row to three cards.

07Plan the grid

Build one layout planner

Give the grid a cost calculation that counts only the gaps between cards. Use that cost to find its column count, then check whether the columns can show every product within the requested row limit. Each task supplies the other working helpers, so the edit stays on the decision being tested.

In the frontend Grid columns in Next.js and Svelte Optional

CSS Grid solves this inequality for you with repeat(auto-fill, minmax(220px, 1fr)): the number of columns is the largest that does not overflow the container’s content box, counting gaps. The content box already excludes padding. Unlike this lesson’s model, the specification always creates at least one column.

A virtualized grid has to know the column count in JavaScript to decide which rows to render. Read the content-box width from a ResizeObserver in a Next.js client hook, or bind it with Svelte’s bind:contentRect. Both excerpts apply n ≤ (width + G) ÷ (C + G) with padding already removed, and keep CSS Grid’s minimum of one column.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
'use client';

import { useEffect, useRef, useState } from 'react';

const CARD = 220; // px minimum card width
const GAP = 16; // px between adjacent cards

export function useColumns() {
  const ref = useRef<HTMLDivElement>(null);
  const [columns, setColumns] = useState(1);

  useEffect(() => {
    const element = ref.current;

    if (!element) return;

    const observer = new ResizeObserver(([entry]) => {
      // contentRect excludes padding: only cards and gaps remain.
      const width = entry.contentRect.width;
      setColumns(Math.max(1, Math.floor((width + GAP) / (CARD + GAP))));
    });
    observer.observe(element);

    return () => observer.disconnect();
  }, []);

  return { ref, columns };
}

Check this idea later
A load test says the service needs at least 7.2 servers’ worth of capacity. How many servers do you provision?
Solve −3x ≤ 12 for x.
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.