9.1 CSS Flexbox & Responsive Design

Lesson 5: CSS Flexbox & Responsive Design

Objective:

Learn to create flexible layouts using Flexbox and media queries for responsive design.

  1. Introduction to Flexbox

Flex Container
  • Flex Containers and Flex Items: A flex container is an element that enables Flexbox layout, and its direct children are considered flex items. To start using Flexbox, define a container as a flex container:

  • Key Flexbox Properties:

    • flex-direction: Defines the direction of the flex items within the container. Values include row (default), column, row-reverse, and column-reverse.

    • justify-content: Controls how flex items are distributed along the main axis (horizontal or vertical depending on flex-direction). Common values: flex-start, flex-end, center, space-between, space-around.

    • align-items: Aligns flex items along the cross axis (perpendicular to the main axis). Common values: flex-start, flex-end, center, stretch.

    • flex-wrap: Controls whether flex items wrap onto multiple lines. By default, flex items are on a single line (nowrap), but this can be set to wrap for multi-line layouts.

    • flexbox display: the display value`flex`defines the container as a block element, however, the value `inline-flex`defines the element as inline element.

  1. Building a Flexbox Layout

  • Creating a Basic Responsive Layout Using Flexbox: To demonstrate Flexbox, let’s create a simple layout with three columns that adjust according to screen size.

  • Understanding Alignment and Spacing with Flexbox: By adjusting justify-content and align-items, you can control the horizontal and vertical alignment of flex items within the container.

  1. Responsive Design with Media Queries

  • Using Media Queries to Create Responsive Designs: Media queries allow you to apply different styles based on the viewport size (or other characteristics). For example, you can adjust the layout for screens narrower than 600px:

  1. Combining Flexbox and Media Queries

  • Creating a Mobile-First Design with Flexbox and Media Queries: In a mobile-first approach, you design for smaller screens first and then progressively enhance the layout for larger screens using media queries.

Last updated