NexusBerry Training & Solutions
    NexusBerry Training & Solutions

    NexusBerry provides cutting-edge training and solutions for modern technology needs.

    About Us

    • Privacy Policy
    • Blog
    • Ruls and Regulations
    • Forms

    NexusBerry

    • Contact Us
    • About Us
    • Job Openings
    • Payment Methods

    Resources

    • Internship
    • Tutorials
    • Fee Structure
    • Admission Form

    AI & Data Science

    • Data Science Professional with Python & Applied Analytics
    • Full Stack Python Developer
    • Advanced Data Analytics Professional with Python and BI Tools
    • Agentic AI Engineer – GenAI, RAG & Autonomous Agents
    • AI Machine Learning Engineer with Python & Deep Learning
    • Python Programming for Software & AI Careers

    Software Engineering

    • Modern Frontend Developer with React & Next.js
    • MEAN Stack Mastery: Empower Your Web Development Skills for Modern Applications
    • Advanced Web Development with ASP.NET Core and Angular.js
    • Unlocking the Potential of PHP Laravel with Vue.js: A Comprehensive Training Course for Web Development Success
    • Django & React: Build Full-Stack Modern, Scalable Web Applications
    • Full Stack JavaScript Developer Specialization: Building Web and Mobile Apps with MERN Stack and React Native
    • Next.js: The Modern Path to Full Stack Web Development
    • Python Full Stack Web Development With Django
    • MERN Full Stack Web Development With Nextjs & GenAI

    Mobile App Development

    • Flutter App Development Course: Masterclass with Real-World Projects
    • React Native for Android and iOS App Development

    Digital Marketing

    • Social Media Marketing Course
    • SEO (Search Engine Optimization) Course
    • Full Stack Digital Marketing Course

    Creative Design

    • Master Video Editing: From Beginner to Pro Storyteller
    • Full Stack Designing & Video Editing Course
    • UI/UX Designing
    • Professional Graphic Design Masterclass: Master Visual Branding & AI Tools

    IT Infrastructure & Security

    • The Ultimate Cybersecurity Course: Web App Protection from Scratch
    • DevOps and Cloud Computing
    • CCNA Course

    © Copyright 2025, All Rights Reserved By NexusBerry Training & Solutions

    1. Home
    2. Tutorials
    3. Web Development Fundamentals
    4. Lecture 4: Flexbox and CSS Grid Mastery
    css 2/3

    Lecture 4: Flexbox and CSS Grid Mastery

    Subscribe to our channel

    Master the two layout systems that power every modern website — and never struggle with CSS alignment again.


    What You'll Learn

    Learn when and how to use the two most powerful CSS layout tools in web development:

    • 📐 Flexbox — for one-dimensional layouts like navbars, button groups, and card rows
    • 🔲 CSS Grid — for full two-dimensional page structures like dashboards and page templates
    • ✨ The responsive pattern — build a card grid that adapts to any screen size with a single line of CSS and zero media queries

    Why This Matters

    These aren't just concepts for assignments — Flexbox and Grid are the industry standard for layout in every professional project. Once you understand them, you'll stop guessing with CSS and start building with confidence.


    Before You Watch

    Make sure you're comfortable with:

    • Basic CSS properties (width, height, margin, padding)
    • How parent/child elements relate in HTML

    📎 The cheatsheet for this lecture is attached below the video. Keep it open while you watch!

    Flexbox & CSS Grid Mastery Cheatsheet

    Quick reference for all layout concepts covered in Lecture 4.


    Flexbox Container Properties

    Apply these to the parent element (display: flex).

    Property

    Values

    Example

    display

    flex

    display: flex;

    flex-direction

    row (default), column, row-reverse, column-reverse

    flex-direction: column;

    flex-wrap

    nowrap (default), wrap, wrap-reverse

    flex-wrap: wrap;

    justify-content

    flex-start, flex-end, center, space-between, space-around, space-evenly

    justify-content: space-between;

    align-items

    stretch (default), flex-start, flex-end, center, baseline

    align-items: center;

    gap

    Any length value

    gap: 16px;

    Key Rule: justify-content = main axis (horizontal in row). align-items = cross axis (vertical in row).


    Flexbox Item Properties

    Apply these to the child elements inside a flex container.

    Property

    Values

    Example

    flex-grow

    Number (default: 0)

    flex-grow: 1;

    flex-shrink

    Number (default: 1)

    flex-shrink: 0;

    flex-basis

    Length or auto (default)

    flex-basis: 200px;

    flex

    Shorthand for grow/shrink/basis

    flex: 1; (= 1 1 0%)

    align-self

    auto, flex-start, flex-end, center, stretch

    align-self: flex-end;

    order

    Integer (default: 0)

    order: -1; (moves first)

    The 90% Shorthand:

    1.item { flex: 1; }
    2/* Expands to: flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */

    CSS Grid Container Properties

    Apply these to the parent element (display: grid).

    Property

    Values

    Example

    display

    grid

    display: grid;

    grid-template-columns

    Sizes (px, fr, %, auto)

    grid-template-columns: 250px 1fr;

    grid-template-rows

    Sizes (px, fr, %, auto)

    grid-template-rows: 60px 1fr 40px;

    gap

    Any length value

    gap: 16px;

    Shorthand with `repeat()`:

    1grid-template-columns: repeat(3, 1fr);
    2/* Same as: 1fr 1fr 1fr */

    CSS Grid Item Properties

    Apply these to child elements inside a grid container.

    Property

    Values

    Example

    grid-column

    start / end

    grid-column: 1 / 3; (spans 2 cols)

    grid-column

    1 / -1

    Full width (first to last line)

    grid-column

    span N

    grid-column: span 2;

    grid-row

    start / end

    grid-row: 1 / 3; (spans 2 rows)


    The fr Unit

    The fr unit represents a fraction of the remaining space in the grid container.

    1grid-template-columns: 1fr 2fr 1fr;
    2/* On an 800px container (no gap):
    3 Total fr = 1 + 2 + 1 = 4
    4 1fr = 800 / 4 = 200px
    5 Columns: 200px | 400px | 200px */

    Mix with fixed values:

    1grid-template-columns: 250px 1fr;
    2/* 250px sidebar, remaining space for main content */

    The Magic Responsive Pattern

    One line. No media queries. Responsive card grid:

    1.grid {
    2 display: grid;
    3 grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    4 gap: 16px;
    5}

    How it works:

    • auto-fit: Create as many columns as will fit
    • minmax(250px, 1fr): Each column is at least 250px, grows to fill remaining space
    • Result: 4 columns on wide screens → 3 → 2 → 1 on narrow screens — automatically!

    Flexbox vs Grid Decision Guide

    Scenario

    Use

    Why

    Navigation bar (logo + links)

    Flexbox

    One-dimensional row

    Page layout (header, sidebar, main, footer)

    Grid

    Two-dimensional structure

    Equal-width card row

    Flexbox

    flex: 1 on items

    Responsive card grid (auto-wrapping)

    Grid

    auto-fit + minmax()

    Centering a single element

    Both

    Flexbox or Grid — both work

    Sidebar + main content

    Grid

    grid-template-columns: 250px 1fr

    Button group / tag list

    Flexbox

    Wrapping row of items

    Full page dashboard

    Grid + Flexbox

    Grid for layout, Flex for components

    Rule of thumb: Grid for the big picture (macro layout), Flexbox for the details (micro layout).


    Common Mistakes to Avoid

    1. Flex Properties on Wrong Element

    1/* WRONG — justify-content goes on the CONTAINER */
    2.item { justify-content: center; }
    3
    4/* CORRECT — put it on the parent */
    5.container {
    6 display: flex;
    7 justify-content: center;
    8}

    2. Invalid align-items Value

    1/* WRONG — space-between is for justify-content only */
    2.container {
    3 display: flex;
    4 align-items: space-between;
    5}
    6
    7/* CORRECT — use a valid value */
    8.container {
    9 display: flex;
    10 align-items: center; /* or flex-start, flex-end, stretch, baseline */
    11}

    3. Using fr Outside of Grid

    1/* WRONG — fr only works with grid-template-columns/rows */
    2.item { width: 1fr; }
    3
    4/* CORRECT — use fr in grid templates */
    5.container {
    6 display: grid;
    7 grid-template-columns: 1fr 2fr;
    8}

    4. Forgetting display: grid/flex

    1/* WRONG — template properties do nothing without display: grid */
    2.container {
    3 grid-template-columns: 1fr 1fr 1fr;
    4}
    5
    6/* CORRECT */
    7.container {
    8 display: grid;
    9 grid-template-columns: 1fr 1fr 1fr;
    10}

    VS Code Shortcuts

    Shortcut

    Action

    Type df + Tab

    Emmet: display: flex;

    Type dg + Tab

    Emmet: display: grid;

    Type jcc + Tab

    Emmet: justify-content: center;

    Type aic + Tab

    Emmet: align-items: center;

    Ctrl + Space

    CSS property autocomplete

    Ctrl + /

    Toggle comment

    Ctrl + D

    Select next occurrence

    Alt + Up/Down

    Move line up/down

    Shift + Alt + Down

    Duplicate line


    Chrome DevTools Layout Tools

    Flexbox Inspector

    • Select a flex container in Elements panel
    • Look for the flex badge next to the element
    • Click the badge to toggle the Flexbox overlay
    • Shows main axis direction, item boundaries, and gap visualization

    Grid Inspector

    • Select a grid container in Elements panel
    • Look for the grid badge next to the element
    • Click the badge to toggle the Grid overlay
    • Shows grid lines, track sizes, line numbers, and area names

    Layout Panel

    • Open DevTools → Layout tab
    • Lists all Flex and Grid containers on the page
    • Toggle overlays for multiple containers simultaneously
    • Customize overlay colors and display options

    What's Next?

    In Lecture 5: Advanced CSS & Responsive Design, you'll learn:

    • Media queries for different screen sizes
    • Mobile-first vs desktop-first strategy
    • Responsive units (rem, em, vw, vh)
    • Making your dashboard truly responsive on every device

    Keep this cheatsheet handy while working on your assignment!

    All Web Development Fundamentals Tutorials