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.
| Symbol | Meaning | Worked value |
|---|---|---|
| W | Row width, including padding | 1000 px |
| P | Padding on each side | 24 px |
| C | Minimum card width | 220 px |
| G | Gap between adjacent cards | 16 px |
| n | Cards 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
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.
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.
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.
const cards = 17;
const perRow = 4;
console.log(cards / perRow);
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.
“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
| Change | Upper bound on k | Cards per row | Plan |
|---|---|---|---|
| W = 1000 px | 4.10 | 4 | No layout: 5 per row needed |
| W = 1000 px, G = 32 px | 3.90 | 3 | No layout |
| W = 1100 px | 4.53 | 4 | No layout |
| W = 1212 px | 5.00 | 5 | 5 per row, 5 rows |
| W = 1100 px, at most 6 rows | 4.53 | 4 | 4 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.
A virtualized product grid
Measure a row, fit the most cards, then satisfy two limits.
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.
'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
Sources
- Go: integer division truncates
- Rust: integer division
- C++: multiplicative operators
- ECMAScript: Math.floor and Math.ceil
- CSS Grid Level 1: auto-fill
The grid dimensions and plans are a worked model, not measured layouts.
Sign in to keep your read marks on every device. Sign in
Was this helpful?