← Back to Learning Path JavaScript • Level 3 • Lesson 9 of 9
Level 3 progress 0% complete

Lesson 9: CSS-in-JS for Grids

Use modern CSS-in-JS libraries to build powerful grid systems.

What is CSS-in-JS?

CSS-in-JS libraries let you write CSS directly in your JavaScript or React code. Popular libraries include styled-components, Emotion, and Tailwind CSS. They provide powerful features like dynamic styling, theming, and scoped styles.

Why Use CSS-in-JS Component-scoped styles, dynamic theming, no naming conflicts, TypeScript support, and the ability to use props and state directly in styles.

Styled-Components Grid

Create reusable grid components with styled-components:

import styled from 'styled-components';

const Grid = styled.div`
  display: grid;
  grid-template-columns: repeat(${props => props.columns || 3}, 1fr);
  gap: ${props => props.gap || '1rem'};
  
  @media (max-width: 768px) {
    grid-template-columns: 1fr;
  }
`;

// Usage
function Gallery() {
  return (
    <Grid columns={4} gap="2rem">
      <div>Item 1</div>
      <div>Item 2</div>
      <div>Item 3</div>
      <div>Item 4</div>
    </Grid>
  );
}

Emotion Grid System

Emotion offers a powerful CSS prop and styled API:

/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';

const gridStyles = (columns, gap) => css`
  display: grid;
  grid-template-columns: repeat(${columns}, 1fr);
  gap: ${gap};
  transition: all 0.3s ease;
`;

function ResponsiveGrid({ items, columns = 3 }) {
  return (
    <div css={gridStyles(columns, '1.5rem')}>
      {items.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </div>
  );
}

Dynamic Grid with Themes

Use theme values for consistent spacing:

const Grid = styled.div`
  display: grid;
  grid-template-columns: repeat(${props => props.columns}, 1fr);
  gap: ${props => props.theme.spacing[props.gapSize]};
  padding: ${props => props.theme.spacing.lg};
`;

// Theme provider
const theme = {
  spacing: {
    sm: '0.5rem',
    md: '1rem',
    lg: '2rem',
    xl: '3rem'
  }
};

function App() {
  return (
    <ThemeProvider theme={theme}>
      <Grid columns={3} gapSize="md">
        <Card>Item 1</Card>
        <Card>Item 2</Card>
        <Card>Item 3</Card>
      </Grid>
    </ThemeProvider>
  );
}

Responsive Grid Component

Build a fully responsive grid helper:

const ResponsiveGrid = styled.div`
  display: grid;
  gap: 1rem;
  
  grid-template-columns: repeat(
    ${props => props.mobile || 1}, 
    1fr
  );
  
  @media (min-width: 768px) {
    grid-template-columns: repeat(
      ${props => props.tablet || 2}, 
      1fr
    );
  }
  
  @media (min-width: 1024px) {
    grid-template-columns: repeat(
      ${props => props.desktop || 4}, 
      1fr
    );
  }
`;

// Usage
<ResponsiveGrid mobile={1} tablet={2} desktop={4}>
  <Item>A</Item>
  <Item>B</Item>
  <Item>C</Item>
  <Item>D</Item>
</ResponsiveGrid>
Performance Tip CSS-in-JS has a runtime cost. For maximum performance, consider CSS Modules or Tailwind for production apps with lots of components.

Grid with Auto-Fit

Truly responsive grid without media queries:

const AutoGrid = styled.div`
  display: grid;
  grid-template-columns: repeat(
    auto-fit, 
    minmax(${props => props.minWidth || '250px'}, 1fr)
  );
  gap: ${props => props.gap || '1rem'};
`;

// Usage - automatically responsive!
<AutoGrid minWidth="300px" gap="2rem">
  <Card>1</Card>
  <Card>2</Card>
  <Card>3</Card>
  <Card>4</Card>
</AutoGrid>

Dashboard Layout Component

Reusable dashboard with named areas:

const Dashboard = styled.div`
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 40px;
  min-height: 100vh;
  gap: 0;
  
  @media (max-width: 768px) {
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto auto;
  }
`;

const Header = styled.header`
  grid-area: header;
  background: #1f2937;
  color: white;
`;

const Sidebar = styled.aside`
  grid-area: sidebar;
  background: #f3f4f6;
`;

const Main = styled.main`
  grid-area: main;
  padding: 2rem;
`;

const Footer = styled.footer`
  grid-area: footer;
  background: #e5e7eb;
`;

function App() {
  return (
    <Dashboard>
      <Header>My App</Header>
      <Sidebar>Navigation</Sidebar>
      <Main>Content</Main>
      <Footer>© 2026</Footer>
    </Dashboard>
  );
}

Tailwind CSS Grid Utilities

Tailwind provides utility classes for grids:

// Tailwind grid example (HTML)
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
</div>

// Or with arbitrary values
<div className="grid grid-cols-[200px_1fr_200px] gap-6">
  <div>Sidebar</div>
  <div>Main</div>
  <div>Aside</div>
</div>
Choosing an Approach
  • Tailwind: fastest development, smallest bundle
  • styled-components: best for heavy theming needs
  • Emotion: most flexible, great TypeScript support
  • Inline styles: simple, no build step needed

Combining Approaches

You can mix CSS-in-JS with inline styles for maximum flexibility:

const Grid = styled.div`
  display: grid;
  gap: 1rem;
`;

function DynamicGallery({ items, userColumns }) {
  return (
    <Grid 
      style={{ 
        gridTemplateColumns: `repeat(${userColumns}, 1fr)` 
      }}
    >
      {items.map(item => <Card key={item.id}>{item.name}</Card>)}
    </Grid>
  );
}

Course Complete

You have completed the JavaScript Grid course. You now know:

You are now equipped to build professional grid layouts in React, Vue, vanilla JS, and modern CSS-in-JS frameworks. Keep practicing and building.

Final Challenge

Build a complete dashboard using either styled-components or Emotion with named grid areas, responsive breakpoints, theme-based spacing, and smooth animations.

Final Check

Test your knowledge to complete the course. All 3 questions must be correct to finish.

1. What is the main advantage of CSS-in-JS for grids?

2. Which CSS-in-JS approach is fastest for production?

3. How do you pass dynamic columns to a styled-component?

← Previous Lesson