CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Max Width


The max-width media feature is the opposite of min-width. It applies styles when the viewport width is less than or equal to the specified value. This is often used for a "desktop-first" approach or to apply specific styles only to smaller devices.

Example 1: Collapsing a Navigation Menu

/* Hiding the full navigation and showing a hamburger icon on smaller screens */
.desktop-nav {
  display: flex; /* a one-dimensional layout method for laying out items in rows or columns */
}
.mobile-nav-icon {
  display: none; /* Hidden by default */
}

@media (max-width: 799px) {
  .desktop-nav {
    display: none; /* Hide the full nav */
  }
  .mobile-nav-icon {
    display: block; /* Show the hamburger icon */
  }
}

Explanation

This code displays a standard desktop navigation. When the screen size is 799px or less, it hides the desktop navigation and shows an icon for a mobile menu.


Example 2: Stacking Columns

/* Forcing grid items to stack vertically on smaller screens */
.grid-container {
  display: grid; /* a two-dimensional grid-based layout system */
  grid-template-columns: 1fr 1fr; /* Two columns by default */
}

@media (max-width: 600px) {
  .grid-container {
    grid-template-columns: 1fr; /* Single column layout */
  }
}

Explanation

Initially, .grid-container is a two-column grid. For viewports 600px wide or smaller, the layout changes to a single column, which is better for vertical scrolling on mobile devices.


Example 3: Reducing Margins

/* Decreasing the body margin on small screens to maximize content space */
body {
  margin: 40px; /* Default margin for larger screens */
}

@media (max-width: 480px) {
  body {
    margin: 15px; /* a CSS property that defines the margin area on all four sides of an element */
  }
}

Explanation

This CSS reduces the body's margin from 40px to 15px on screens with a maximum width of 480px. This gives more screen real estate to the actual content on smaller devices.


Example 4: Hiding Non-Essential Elements

/* Hiding a decorative sidebar on mobile devices */
.decorative-sidebar {
  display: block; /* Visible by default */
}

@media (max-width: 900px) {
  .decorative-sidebar {
    display: none; /* Hide for screens 900px and smaller */
  }
}

Explanation

The .decorative-sidebar is hidden on screens that are 900px wide or less. This is a common technique to declutter the interface on smaller devices by removing non-critical elements.


Example 5: Full-Width Buttons

/* Making call-to-action buttons full-width for easier tapping on mobile */
.cta-button {
  width: auto; /* Default width */
}

@media (max-width: 500px) {
  .cta-button {
    width: 100%; /* a CSS property that defines the width of an element */
    padding: 15px 0; /* a CSS property that is a shorthand for defining the padding on all four sides of an element */
  }
}

Explanation

On screens up to 500px wide, call-to-action buttons will span the full width of their container. This makes them easier to see and tap on mobile devices.


Example 6: Simpler Footer

/* Displaying a simplified footer on small screens */
.detailed-footer {
  display: block; /* Visible by default */
}
.simple-footer {
  display: none; /* Hidden by default */
}

@media (max-width: 767px) {
  .detailed-footer {
    display: none; /* Hide the detailed footer */
  }
  .simple-footer {
    display: block; /* Show the simple footer */
  }
}

Explanation

This example allows for two different footer versions. The detailed footer is hidden, and a simpler one is shown on screens with a maximum width of 767px.


Example 7: Font Size Reduction

/* Reducing heading font sizes on mobile to prevent text wrapping issues */
h1 {
  font-size: 3rem; /* a CSS property that defines the font size of a text */
}

@media (max-width: 400px) {
  h1 {
    font-size: 2.2rem; /* a CSS property that defines the font size of a text */
  }
}

Explanation

To improve the text layout on very narrow screens, the font size for <h1> elements is reduced when the viewport is 400px wide or less.