CSS Tutorial

Flexbox

Introduction

Flexbox is a flexible layout model for creating responsive layouts. It allows you to align and distribute elements within a container in a flexible and dynamic way.

Key Concepts

  • Flex Container: The parent element that holds the flex items.
  • Flex Items: The child elements within the flex container.
  • Flex Direction: The direction of the flex items (row or column).
  • Flex Wrap: Controls whether the flex items wrap to multiple lines.
  • Align Items: Aligns the flex items vertically within the container.
  • Justify Content: Aligns the flex items horizontally within the container.

Steps to Use Flexbox

  • Mark the flex container with display: flex;.
  • Set the flex-direction property to 'row' or 'column'.
  • Set the flex-wrap property to 'wrap' or 'nowrap'.
  • Use align-items to position the flex items vertically within the container.
  • Use justify-content to position the flex items horizontally within the container.

CSS Example

.flex-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
}

Benefits of Flexbox

  • Responsive: Adapts to different screen sizes and orientations.
  • Flexible: Allows for easy alignment and distribution of elements.
  • Cross-browser compatibility: Supported by all major browsers.
  • Powerful: Can create complex layouts with ease.