Responsive Design & Advanced CSS - Complete Guide

Responsive Design & Advanced CSS - Complete Guide

Responsive Design & Advanced CSS

Complete Guide with Interactive Examples

Master Modern Web Layouts

Learn responsive design principles, advanced CSS techniques, and performance optimization strategies

Mobile-First

Design for mobile devices first, then enhance for larger screens

Performance

Optimize CSS for faster loading and better user experience

Modern CSS

Use advanced CSS features for maintainable and scalable code

Responsive Design

What is Responsive Design?

Creating web pages that look and work well on all devices and screen sizes.

/* Responsive Design Principles */ .container { width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 20px; } img { max-width: 100%; height: auto; }

Mobile-First Approach

Start with mobile styles, then add enhancements for larger screens.

/* Base styles (mobile first) */ .button { padding: 10px 15px; font-size: 14px; } /* Tablet and larger */ @media (min-width: 768px) { .button { padding: 12px 20px; font-size: 16px; } } /* Desktop */ @media (min-width: 1024px) { .button { padding: 15px 30px; font-size: 18px; } }

Media Queries

This box changes color and layout based on screen width:

Flex Item 1
Flex Item 2
Flex Item 3
Current Width: 0px Active Breakpoint: None
/* Media Query Syntax */ @media media-type and (media-feature) { /* CSS rules */ } /* Common Breakpoints */ @media (max-width: 767px) { /* Mobile */ } @media (min-width: 768px) { /* Tablet */ } @media (min-width: 1024px) { /* Desktop */ } @media (min-width: 1200px) { /* Large Desktop */ } /* Feature Queries */ @supports (display: grid) { /* CSS Grid is supported */ } @supports not (display: grid) { /* Fallback for no grid support */ }

Responsive Typography

This text scales with the viewport width using clamp()
/* Fluid Typography */ .responsive-heading { /* min, preferred, max */ font-size: clamp(1.5rem, 4vw, 3rem); line-height: clamp(1.2, 1.5, 2); } .responsive-paragraph { font-size: clamp(1rem, 2vw, 1.25rem); max-width: 65ch; /* Optimal line length */ } /* Viewport-based units */ .viewport-text { font-size: 2vw; /* 2% of viewport width */ margin: 1vh 1vw; /* Viewport height/width */ padding: 2vmin; /* Minimum of vh and vw */ }

Responsive Images

Responsive Image Example
Responsive image Art directed image

Viewport Meta Tag

<!-- Essential for responsive design --> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=0.5, user-scalable=yes">

Properties:

  • width=device-width: Matches screen width
  • initial-scale=1.0: Default zoom level
  • maximum-scale=5.0: Maximum zoom allowed
  • minimum-scale=0.5: Minimum zoom allowed
  • user-scalable=yes: Allow pinch-to-zoom

Responsive Navigation Patterns

Hamburger Menu

Hidden menu that expands on click

Priority+

Important links shown, others hidden

Dropdown

Collapses into dropdown on mobile

/* Example: Hamburger Menu */ .mobile-menu-btn { display: none; } @media (max-width: 768px) { .desktop-nav { display: none; } .mobile-menu-btn { display: block; } .mobile-nav { position: absolute; top: 100%; left: 0; right: 0; background: white; } }

Advanced CSS Concepts

CSS Variables (Custom Properties)

--primary
--secondary
--accent
--danger
/* Declare CSS Variables */ :root { --primary-color: #6366f1; --secondary-color: #10b981; --spacing-unit: 1rem; --border-radius: 8px; } /* Use Variables */ .button { background: var(--primary-color); padding: calc(var(--spacing-unit) * 2); border-radius: var(--border-radius); } /* Variable Scope */ .component { --local-color: #f59e0b; color: var(--local-color); } /* Fallback Values */ .text { color: var(--custom-color, #000000); } /* Change with JavaScript */ document.documentElement.style.setProperty( '--primary-color', '#ff0000' );

CSS Functions

calc(100% - 40px)
clamp(1rem, 2vw, 2rem)
min(200px, 50%)
max(100px, 30%)
/* calc() - Mathematical calculations */ .container { width: calc(100% - 40px); height: calc(100vh - 80px); margin: calc(20px * 2); } /* clamp() - Constrained value */ .heading { font-size: clamp(1.5rem, 4vw, 3rem); /* min, preferred, max */ } /* min() and max() */ .sidebar { width: min(300px, 100%); height: max(200px, 50vh); } /* var() - CSS Variables */ :root { --main-color: #6366f1; } .element { color: var(--main-color, #000); } /* url() - External resources */ .background { background-image: url('image.jpg'); } /* attr() - HTML attribute values */ .tooltip::after { content: attr(data-tooltip); } /* Custom Properties with Functions */ :root { --base-size: 16px; --scale: 1.5; --heading-1: calc(var(--base-size) * var(--scale) * 2); }

Advanced Selectors

Child Selector
Sibling Selector
:nth-child()
:not()
:is()
:where()
/* Child and Sibling Selectors */ .parent > .child { } /* Direct child */ .element + .sibling { } /* Adjacent sibling */ .element ~ .sibling { } /* General sibling */ /* :nth-child variations */ li:nth-child(odd) { } /* Odd items */ li:nth-child(even) { } /* Even items */ li:nth-child(3) { } /* Third item */ li:nth-child(3n) { } /* Every third item */ li:nth-child(3n+1) { } /* 1st, 4th, 7th... */ li:nth-child(-n+3) { } /* First 3 items */ li:nth-child(n+4):nth-child(-n+6) { } /* Items 4-6 */ /* :nth-of-type */ p:nth-of-type(2) { } /* Second paragraph */ /* :not() selector */ button:not(.disabled) { } /* All buttons except .disabled */ p:not(:first-child) { } /* All paragraphs except first */ /* :is() and :where() */ :is(header, footer, section) h1 { } :where(header, footer, section) h1 { } /* Difference: :where() has 0 specificity */ .component :where(h1, h2, h3) { /* These don't increase specificity */ } /* Attribute Selectors */ [data-type="primary"] { } /* Exact match */ [class^="btn-"] { } /* Begins with */ [class$="-large"] { } /* Ends with */ [class*="warning"] { } /* Contains */

CSS Architecture

/* BEM (Block Element Modifier) */ .block { } /* Standalone component */ .block__element { } /* Part of the block */ .block--modifier { } /* Different state/version */ /* Example */ .card { } /* Block */ .card__header { } /* Element */ .card__title { } /* Element */ .card--featured { } /* Modifier */ .card__button--primary { } /* Element with modifier */ /* SMACSS Categories */ /* 1. Base rules (html, body, h1-h6) */ /* 2. Layout rules (header, footer, grid) */ /* 3. Module rules (reusable components) */ /* 4. State rules (.is-active, .is-hidden) */ /* 5. Theme rules (color schemes) */ /* OOCSS Principles */ /* 1. Separate structure from skin */ /* 2. Separate container from content */ /* ITCSS (Inverted Triangle CSS) */ /* 1. Settings (variables) */ /* 2. Tools (mixins, functions) */ /* 3. Generic (reset, normalize) */ /* 4. Elements (bare HTML elements) */ /* 5. Objects (layout patterns) */ /* 6. Components (UI components) */ /* 7. Utilities (helper classes) */

Performance & Frameworks

Performance Optimization

CSS Minification

Reduce file size by 60-80%

Critical CSS

Load above-the-fold styles first

Reduce Blocking

Async loading for non-critical CSS

CSS-in-JS

Component-scoped styles

/* Performance Techniques */ /* 1. Minify CSS */ /* Original: */ .button { background-color: #6366f1; padding: 15px 30px; border-radius: 8px; } /* Minified: */ .button{background:#6366f1;padding:15px 30px;border-radius:8px} /* 2. Critical CSS */ /* 3. Remove unused CSS */ /* Use tools: PurgeCSS, UnCSS */ /* 4. Optimize animations */ .animate { transform: translateZ(0); /* GPU acceleration */ will-change: transform; /* Hint browser */ } /* 5. Use efficient selectors */ /* Fast: */ #header .nav-item { } /* Slow: */ div ul li a.nav-link:hover { } /* 6. Reduce reflows */ /* Batch DOM changes */ /* Use transform/opacity for animations */ /* 7. CSS Delivery Optimization */

CSS Preprocessors (SASS/SCSS)

/* SCSS Features */ /* Variables */ $primary-color: #6366f1; $spacing-unit: 1rem; /* Nesting */ .nav { ul { margin: 0; padding: 0; li { display: inline-block; a { color: $primary-color; &:hover { text-decoration: underline; } } } } } /* Mixins */ @mixin flex-center { display: flex; justify-content: center; align-items: center; } .container { @include flex-center; height: 100vh; } /* Functions */ @function double($value) { @return $value * 2; } .element { padding: double($spacing-unit); } /* Extends/Inheritance */ %button-base { padding: 10px 20px; border-radius: 4px; border: none; } .primary-button { @extend %button-base; background: $primary-color; color: white; } /* Conditionals */ $theme: dark; .theme-colors { @if $theme == dark { background: #000; color: #fff; } @else { background: #fff; color: #000; } } /* Loops */ @for $i from 1 through 5 { .mt-#{$i} { margin-top: #{$i}rem; } } /* Maps */ $colors: ( primary: #6366f1, secondary: #10b981, accent: #f59e0b ); @each $name, $color in $colors { .bg-#{$name} { background-color: $color; } }

CSS Frameworks Overview

Bootstrap

Most popular, comprehensive component library

<button class="btn btn-primary">

Tailwind CSS

Utility-first, highly customizable

<button class="bg-blue-500 text-white px-4 py-2">

Foundation

Enterprise-focused, mobile-first

<button class="button primary">
/* Bootstrap Example */
Success!
/* Tailwind CSS Example */
Success!
/* Foundation Example */
Success!
/* Framework Comparison */ /* Bootstrap: Best for rapid prototyping Tailwind: Best for custom designs Foundation: Best for complex applications Bulma: Pure CSS, no JS dependencies Materialize: Google Material Design */

Testing Responsive Designs

Testing Tools:

  • Browser DevTools: Device emulation
  • Responsinator: Multiple device previews
  • Lighthouse: Performance audits
  • BrowserStack: Real device testing
  • WebPageTest: Performance testing
/* Testing Strategies */ /* 1. Test on real devices */ /* 2. Use Chrome DevTools device mode */ /* 3. Test touch interactions */ /* 4. Check performance on 3G */ /* 5. Validate accessibility */ /* 6. Test print styles */ /* 7. Check different orientations */ /* Media Query Testing */ @media (max-width: 767px) and (orientation: portrait) { /* Mobile portrait */ } @media (max-width: 767px) and (orientation: landscape) { /* Mobile landscape */ } /* Print Testing */ @media print { .no-print { display: none; } a::after { content: " (" attr(href) ")"; } } /* JavaScript for Testing */ if (window.matchMedia("(max-width: 768px)").matches) { // Mobile device } // Listen for screen changes window.matchMedia('(max-width: 768px)') .addEventListener('change', (e) => { if (e.matches) { console.log('Now in mobile view'); } });

© 2024 Responsive CSS Guide. All rights reserved.

Made with for web developers