CSS Tutorial

Fluid Layouts

Fluid layouts are adaptable to varying screen sizes, ensuring a seamless user experience across devices. They utilize CSS principles like percentage-based widths, flexible units (ems and rems), and relative font sizes to achieve dynamic layouts.

Key Concepts:

  • Percentage-based Widths: Assign widths (and heights) as percentages of the parent element, allowing elements to expand or shrink proportionally.
  • Flexible Units (ems and rems): Define font sizes and spacing using ems and rems, which scale relative to the user's preferred font size. This enhances accessibility for users with visual impairments.
  • Relative Font Sizes: Use units like "em" (based on current font size) and "rem" (based on root font size) to scale font sizes relative to other elements.

Practical Steps:

  • Set Percentage-based Widths:
    <div style="width: 50%;"></div>
    
  • Use Flexible Units:
    <p style="font-size: 1.25rem;">...</p>
    
  • Apply Relative Font Sizes:
    <span style="font-size: 1.5em;">...</span>
    

CSS Example:

body {
  font-size: 1rem;
}

.container {
  width: 100%;
  margin: 2rem;
}

.content {
  width: 75%;
  font-size: 1.25rem;
}

This CSS creates a layout where the content area is 75% of the screen width and uses a font size of 1.25rem, which scales dynamically with the user's preferred font size.