Styling Components in React
Lecture
Section titled “Lecture”In Lesson 09, we used component composition to assemble our resource finder. PageLayout provides the page structure, while App supplies the header and the components we want to display.
Let’s take another look at that page. Three columns might work well on a wide screen, but what happens when we have less room? Stacking everything works on a phone. Somewhere between those two sizes, we could give Filters its own row and keep Results and Details together beneath it.
In this lesson, we’ll use Tailwind CSS 4 to make those layout decisions explicit. We’ll work with the components and data already in the starter kit.
Learning Outcome Guide
Section titled “Learning Outcome Guide”At the end of this class, you should be able to…
- Apply Tailwind utility classes through React’s
classNameattribute. - Explain how unprefixed utilities and responsive variants work together.
- Use grid columns and column spans to create three responsive layouts.
- Keep page layout decisions separate from a component’s internal styling.
- Check layout transitions and readability at different viewport widths.
Target behaviour
Section titled “Target behaviour”| Viewport | Filters | Results and Details |
|---|---|---|
Below md |
First in a vertical stack | Each occupies its own row |
md up to lg |
Spans all three columns | Results spans two columns; Details spans one |
lg and above |
Occupies one column | Each occupies one column beside Filters |
The order stays the same: Filters, Results, Details. We’re changing how much space each section occupies.
What we will build
Section titled “What we will build”We’ll make changes in two places:
PageLayout.jsxdefines the grid and the spacing around its children.App.jsxcomposes the page and wraps each child to control its column span.
The existing Card component continues to provide the shared border, background, and heading styles. Filters, Results, and Details keep their existing content.
Reading the existing utilities
Section titled “Reading the existing utilities”Open frontend/src/components/layout/PageLayout.jsx. The active <main> element already has these classes:
<main className="mx-auto grid max-w-7xl grid-cols-1 gap-4 px-6 py-6 md:grid-cols-3"> {children}</main>Let’s read the layout rules before changing them.
| Utility | Responsibility |
|---|---|
grid |
Makes the element a grid container |
grid-cols-1 |
Starts with one column |
md:grid-cols-3 |
Uses three equal columns from the medium breakpoint upward |
gap-4 |
Adds space between grid items |
px-6 py-6 |
Adds horizontal and vertical padding inside the container |
max-w-7xl mx-auto |
Limits the container’s width and centres it horizontally |
Notice that className contains ordinary strings. React renders those classes onto the element, and Tailwind supplies the CSS rules.
Start small, then add space
Section titled “Start small, then add space”An unprefixed utility supplies the starting rule. A prefix such as md: applies a rule from that breakpoint upward. In the default Tailwind 4 theme, md starts at 48rem (normally 768px) and lg starts at 64rem (normally 1024px).
That means md:grid-cols-3 still applies at large widths. It doesn’t stop when we reach lg. Also, sm: means “from the small breakpoint upward”; our narrowest layout uses unprefixed classes. See Tailwind’s responsive design reference.
We’ll use that same idea for column spans:
<div className="md:col-span-3 lg:col-span-1"> <Filters /></div>At medium widths, this grid item occupies three columns. At large widths, it occupies one. Below md, neither span rule applies, so it fits in the single-column grid. Tailwind’s grid-column reference lists the available span utilities.
Why the wrappers belong in App
Section titled “Why the wrappers belong in App”PageLayout accepts children. It doesn’t need to know which child is Filters or which child should take more room.
The parent already knows what it is placing on the page. By adding wrappers in App, we can give those items different spans while keeping PageLayout reusable. The wrappers become the grid’s direct child elements.
We’ll use plain <div> wrappers for this layout job. The existing cards already contain a <section> and a heading.
Activity
Section titled “Activity”Starter Kit Code
Section titled “Starter Kit Code”Grab the starter kit code for your Workbook by running the following in the VS Code terminal at the root of your workbook.
pnpm dlx tiged --disable-cache --force DG-InClass/SDEV-2150-A01-Sep-2026/sk/day-10/example/lesson-10-starter ./src/lesson-10pnpm dlx tiged --disable-cache --force DG-InClass/SDEV-2150-A02-Sep-2026/sk/day-10/example/lesson-10-starter ./src/lesson-10If you already have today’s starter, continue with that copy. From your workbook root, install and run the frontend:
cd src/lesson-10/frontendpnpm installpnpm devOpen the URL printed by Vite. This lesson uses the hard-coded resource data already imported by the components; we don’t need to run the backend.
Code Walkthrough
Section titled “Code Walkthrough”The file paths below start inside src/lesson-10/frontend. In PageLayout.jsx and App.jsx, use the active children implementation. You can remove the commented-out alternative that uses left, middle, and right props.
-
Check our starting point.
Open the app in your browser and use DevTools’ responsive mode to try widths of 390px, 800px, and 1280px.
The starter stacks the cards at 390px. At 800px and 1280px, it puts all three cards beside each other. We already have the narrow and wide arrangements; the intermediate arrangement is the part we’re missing.
Open
src/App.jsx. Notice that Filters, Results, and Details are passed directly toPageLayoutas children. -
Give the grid more space at medium widths.
In
PageLayout.jsx, addmd:gap-6to the active<main>element.src/components/layout/PageLayout.jsx <main className="mx-auto grid max-w-7xl grid-cols-1 gap-4 px-6 py-6 md:grid-cols-3"><main className="mx-auto grid max-w-7xl grid-cols-1 gap-4 px-6 py-6 md:grid-cols-3 md:gap-6">{children}</main>Resize the browser again. The gaps should increase at
md, but the cards still use the same arrangement. Changing the space between items doesn’t change how many columns each item occupies. -
Make room for Filters above the other cards.
In
App.jsx, replace the three direct children with these wrappers. Keep the existing imports andPageLayoutheader prop.src/App.jsx <PageLayout header={<Header tagline="Find the right resources, right away" />}><Filters /><Results /><Details /><div className="md:col-span-3"><Filters /></div><div className="md:col-span-2"><Results /></div><div className="md:col-span-1"><Details /></div></PageLayout>At 800px, Filters should occupy the first row. Results and Details should share the next row, with Results taking two of the three columns.
Check 390px as well. The cards should still stack. We only applied the spans from
mdupward.Now try 1280px. Filters still occupies the entire first row. Why? Our
md:rules remain active at larger widths. We need another rule to change those spans. -
Restore three cards beside each other at large widths.
Add
lg:col-span-1to the Filters and Results wrappers.src/App.jsx <div className="md:col-span-3"><div className="md:col-span-3 lg:col-span-1"><Filters /></div><div className="md:col-span-2"><div className="md:col-span-2 lg:col-span-1"><Results /></div><div className="md:col-span-1"><Details /></div>At 1280px, each card should now occupy one column. Details already spans one column from
mdupward, so it doesn’t need a different rule atlg.Notice that the grid itself still has three columns. We changed how its children use those columns.
-
Check the complete layout code.
Your layout component should now look like this:
src/components/layout/PageLayout.jsx export default function PageLayout({ header, children }) {return (<div className="min-h-screen bg-gray-100"><header className="border-b border-sky-600 bg-white px-6 py-4">{header}</header><main className="mx-auto grid max-w-7xl grid-cols-1 gap-4 px-6 py-6 md:grid-cols-3 md:gap-6">{children}</main></div>);}And the parent should compose the page like this:
src/App.jsx import Header from './components/Header';import Filters from './components/Filters';import Results from './components/Results';import Details from './components/Details';import PageLayout from './components/layout/PageLayout';function App() {return (<PageLayout header={<Header tagline="Find the right resources, right away" />}><div className="md:col-span-3 lg:col-span-1"><Filters /></div><div className="md:col-span-2 lg:col-span-1"><Results /></div><div className="md:col-span-1"><Details /></div></PageLayout>);}export default App; -
Check the transitions, not just the three examples.
Use the viewport width in DevTools to check both sides of each breakpoint.
Width in CSS pixels Expected arrangement with the default breakpoints 390 and 767 Three stacked cards 768 and 1023 Filters above Results and Details 1024 and 1280 Three cards beside each other Slowly resize between those widths. Check that headings, labels, and resource text remain readable and that the page doesn’t require horizontal scrolling. Use the keyboard to move through the controls and confirm their focus indicators remain visible.
If Filters stays full-width on a large screen, check its
lg:span. If the narrow layout develops extra columns, check that the multi-column spans have anmd:prefix. If nothing changes at a breakpoint, check the class spelling and that you edited the active JSX.
What we’ve accomplished
Section titled “What we’ve accomplished”We’ve used the same components to create three page arrangements. PageLayout owns the grid, App controls the placement of its children, and the cards retain their shared appearance.
Before moving on, explain why Results needs lg:col-span-1 but Details doesn’t. Being able to predict the result is more useful than remembering a string of classes.
Practice
Section titled “Practice”In the starter kit, homework.md describes the continuation of your Users and Posts homework from Lesson 09. Apply today’s layout ideas to that existing project.
Your task is to make its layout readable at small, medium, and large widths, using Tailwind utilities. Keep the existing components and hard-coded data; decide which section should span the medium layout based on the content in your page.
Your solution should:
- Stack the main sections on narrow screens.
- Give the primary section a full-width row at medium widths, with the remaining content below it.
- Arrange the sections horizontally at large widths.
- Adjust spacing at a breakpoint to improve readability.
- Keep grid rules in the layout component and span rules on the composition wrappers that place its children.
- Preserve the existing content, semantic markup, and visible keyboard focus indicators.
Use the breakpoint checks from the walkthrough to verify your work. Add a short explanation to your homework’s README.md describing which section gets the extra space and why. Keep the existing CSS setup, but implement these layout changes with utility classes rather than new custom CSS or inline styles.
Stretch your understanding
Section titled “Stretch your understanding”Try adding an optional layout prop to PageLayout to choose between two arrangements. Work out both the grid’s column count and the children’s spans before writing the code.
Keep complete Tailwind class names in each alternative, such as 'md:grid-cols-3' and 'md:grid-cols-4'. Tailwind detects classes by scanning source text, so constructing a class from fragments can prevent it from generating the CSS you need. See detecting classes in source files.
For each arrangement, check the same viewport widths and explain what makes it useful for your content.
Answered: 0 / 0 (0%)