JAVASCRIPT • LEVEL 3 • LESSON 9 OF 9
0% Complete
← Back to Learning Path

🎨 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/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 (
    
      
Item 1
Item 2
Item 3
Item 4
); }

Emotion Grid System

Emotion offers 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 (
    
{items.map(item => (
{item.name}
))}
); }

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 (
    
      
        Item 1
        Item 2
        Item 3
      
    
  );
}

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

  A
  B
  C
  D

⚡ Performance Tip:

CSS-in-JS has 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!

  1
  2
  3
  4

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 (
    
      
My App
Navigation
Content
© 2026
); }

Tailwind CSS Grid Utilities

Tailwind provides utility classes for grids:

// Tailwind grid example (HTML)
Item 1
Item 2
Item 3
Item 4
// Or with arbitrary values
Sidebar
Main
Aside

✅ Choosing a 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 (
    
      {items.map(item => {item.name})}
    
  );
}

Congratulations! 🎉

You've completed the JavaScript Grid course! You now know:

You're 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
• Smooth animations

📝 Final Check (3 Questions)

1. What's 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