Introduction to Web Technologies (HTML, CSS, JS)

Chapter 8
Responsive and Adaptive Web Design

Diploma in Full Stack Programming
Introduction to Web Technologies (HTML, CSS, JS)   •   Diploma

Responsive and Adaptive Web Design

Introduction

Web users access applications through phones, tablets, laptops, desktop monitors, televisions, embedded systems, and assistive technologies. These devices differ in screen size, pixel density, input method, network quality, operating system, browser engine, and user preferences. A web page designed for only one viewport may become difficult to read, navigate, or operate elsewhere.

Responsive and adaptive web design address this problem. Responsive design allows a layout to fluidly adjust to available space using flexible dimensions, CSS layout systems, media queries, container queries, and responsive images. Adaptive design provides deliberately selected layouts or behaviours for defined device or environment conditions. Modern practice commonly combines both approaches: a fluid foundation is enhanced with targeted adaptations.

The aim is not to reproduce a desktop layout on a small screen. The aim is to preserve content clarity, task completion, accessibility, performance, and visual hierarchy across contexts.

Learning Objectives

By the end of this chapter, students should be able to:

  • Explain the difference between responsive and adaptive design.
  • Develop a mobile-first layout using fluid sizing and CSS Grid or Flexbox.
  • Select meaningful breakpoints based on content rather than device labels.
  • Apply media queries and container queries appropriately.
  • Implement responsive images and understand their performance implications.
  • Use progressive enhancement and reusable CSS architecture.
  • Test interfaces across browsers, viewports, input methods, and accessibility settings.
  • Evaluate a responsive interface using professional design and engineering criteria.

1. Responsive and Adaptive Design

1.1 Responsive design

Responsive design describes an approach in which the same page structure and content respond continuously to the available environment. Layouts may change from multiple columns to one column, navigation may collapse, images may resize, and spacing may adjust.

Typical responsive techniques include:

  • Relative widths such as percentages and fractional grid units.
  • Flexible images with max-width: 100%.
  • Flexbox and CSS Grid.
  • Media queries based on viewport conditions.
  • Container queries based on the size of a component’s container.
  • Scalable typography using relative units.

A responsive interface does not necessarily have an unlimited number of visual arrangements. It usually has a fluid range with a small number of intentional layout transitions.

1.2 Adaptive design

Adaptive design uses predefined arrangements or behaviours for selected conditions. For example, an application may use a compact navigation pattern below 700 pixels, a two-column pattern between 700 and 1,100 pixels, and a wide layout above 1,100 pixels. Server-side device detection may also deliver different resources, although client-side CSS is generally more reliable for visual adaptation.

Adaptive design can be useful when a product has substantially different workflows, such as a point-of-sale interface, a kiosk, and a desktop administration system. Its weakness is that it may fail at intermediate widths or on new devices if the selected categories are too rigid.

AspectResponsive approachAdaptive approach
Main principleFluid adjustmentPredefined variants
Typical mechanismFlexible CSS and queriesBreakpoint-specific layouts or components
StrengthHandles many viewport sizesSupports strongly differentiated experiences
RiskComplex fluid interactionsGaps between supported conditions
Appropriate useGeneral websites and applicationsDistinct workflows or constrained devices

In professional development, these are not mutually exclusive. A site may use responsive Grid sizing, adaptive navigation at a breakpoint, and container queries for reusable cards.

2. The Mobile-First Design Model

Mobile-first development begins with the smallest practical layout and progressively adds enhancements for larger or more capable environments. The small-screen version forces the team to identify essential content and actions before adding secondary features.

A mobile-first stylesheet normally places base rules outside media queries and adds larger-screen rules later:

/* Base: small screens */
.page {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  padding: 1rem;
}

/* Enhancement: wider screens */
@media (min-width: 48rem) {
  .page {
    grid-template-columns: minmax(0, 2fr) minmax(16rem, 1fr);
    gap: 2rem;
  }
}

The minmax(0, 2fr) expression prevents a grid item from imposing an unexpectedly large minimum width. The fr unit distributes available grid space after fixed or minimum requirements have been considered.

Mobile-first does not mean “mobile only first.” It means that the base experience must be functional without depending on later enhancements. A user should be able to read content, submit a form, and navigate the site even if a large-screen arrangement is unavailable.

The viewport meta element is essential for mobile browsers:

<meta name="viewport" content="width=device-width, initial-scale=1">

width=device-width makes the CSS viewport correspond approximately to the device width, while initial-scale=1 prevents an artificial initial zoom. Omitting this element can cause a mobile browser to render the page as though it were a wide desktop page.

3. Fluid Sizing and Layout Foundations

3.1 Relative units

Responsive interfaces should avoid unnecessary dependence on fixed pixel dimensions. Common units include:

  • %: relative to an applicable containing dimension.
  • rem: relative to the root element’s font size.
  • em: relative to the current element’s font size, with inheritance implications.
  • vw and vh: relative to viewport dimensions.
  • fr: a fraction of available Grid space.
  • ch: approximately the width of the “0” glyph in the relevant font; useful for readable text measure.
  • clamp(): selects a value between a minimum and maximum while allowing a preferred fluid value.

For example:

h1 {
  font-size: clamp(2rem, 1.2rem + 3vw, 4rem);
}

.article {
  width: min(100%, 70ch);
  margin-inline: auto;
}

The first declaration creates a bounded fluid type scale. The second keeps an article within a readable measure while allowing it to shrink on narrow screens. These values are design choices, not universal laws; they should be validated with the actual typeface, language, and content.

3.2 Flexible media

A common baseline is:

img,
svg,
video {
  display: block;
  max-width: 100%;
  height: auto;
}

max-width: 100% prevents an asset from exceeding its containing block. height: auto preserves its intrinsic aspect ratio when width changes. However, this rule does not reserve layout space before an image loads. Adding width and height attributes in HTML allows the browser to calculate the aspect ratio and reduces cumulative layout shift.

<img src="team-800.jpg" width="800" height="533"
     alt="A support team working together">

3.3 Flexbox and Grid

Flexbox is primarily a one-dimensional layout system. It is effective for navigation rows, toolbars, button groups, and aligned components. Grid is two-dimensional and is often better for page regions, dashboards, and card collections.

.card-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
  gap: 1rem;
}

This pattern lets cards form as many columns as fit while preventing a card from becoming narrower than its practical minimum. It is often preferable to adding many device-specific breakpoints.

The min() function prevents the minimum card width from overflowing a narrow container. auto-fit collapses empty tracks, allowing existing cards to use available space.

4. Breakpoints and Media Queries

A breakpoint is a condition at which a design changes because the content or interaction pattern requires it. Breakpoints should not be chosen solely from lists of phone, tablet, and desktop widths. Instead, resize the interface and identify where:

  • Navigation labels no longer fit comfortably.
  • Text measure becomes too wide or too narrow.
  • Cards become unusable.
  • Controls need a different arrangement.
  • A sidebar competes with the primary content.

A media query can inspect viewport characteristics:

@media (min-width: 50rem) {
  .site-nav__links {
    display: flex;
    gap: 1.25rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

Media queries can also respond to orientation, hover capability, pointer accuracy, contrast preferences, and colour scheme. They should not be used to assume a particular device. A touchscreen laptop may support both hover and touch, and a large monitor may be used with keyboard-only navigation.

Useful examples include:

@media (hover: hover) and (pointer: fine) {
  .button:hover {
    filter: brightness(0.9);
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
  }
}

Hover must never be the only way to discover or operate a feature.

5. Container Queries

Media queries respond to the viewport. A container query responds to the size of a component’s containing block. This is valuable for reusable components that may appear in a sidebar, a main column, or a modal.

.dashboard-card {
  container: card / inline-size;
  display: grid;
  gap: 1rem;
}

@container card (min-width: 32rem) {
  .dashboard-card {
    grid-template-columns: 8rem 1fr;
    align-items: center;
  }
}

The component declares a named containment context. When that context is at least 32 rem wide, the card changes layout. The card does not need to know whether its parent is a page column or a dialog.

Container queries improve component portability, but they require careful containment design. A component should not query a container whose size is itself determined by the component in a circular manner. Developers should also provide a usable base layout for browsers without container-query support when that feature is not guaranteed.

6. Responsive Images and Media Performance

Image decisions affect both layout quality and page performance. A 2,400-pixel image may be unnecessary on a narrow screen, while a low-resolution image may appear blurred on a high-density display.

The srcset and sizes attributes allow the browser to choose among resources:

<img
  src="landscape-800.jpg"
  srcset="landscape-400.jpg 400w,
          landscape-800.jpg 800w,
          landscape-1600.jpg 1600w"
  sizes="(min-width: 60rem) 50vw, 100vw"
  width="1600"
  height="1000"
  alt="A landscape viewed from a mountain path">

The width descriptors identify the intrinsic widths of candidate files. sizes tells the browser how wide the image is expected to appear in the layout. The browser then considers viewport size, pixel density, network conditions, and its own selection algorithm.

Use <picture> when the image itself needs to change, such as when a wide crop is unsuitable on a small screen or when a modern image format has a fallback:

<picture>
  <source media="(max-width: 40rem)" srcset="portrait.avif">
  <source type="image/avif" srcset="landscape.avif">
  <img src="landscape.jpg" alt="A cyclist on a coastal road">
</picture>

Decorative images should use an empty alt attribute (alt="") so assistive technology does not announce irrelevant information. Informative images need concise alternative text. A complex chart may require a nearby textual explanation rather than attempting to place all meaning in alt text.

Use lazy loading selectively:

<img src="map.jpg" loading="lazy" alt="Campus map">

Below-the-fold images are often suitable candidates. The main visual likely to appear immediately should not be lazily loaded without a performance reason, because that can delay the perceived start of the page.

7. Progressive Enhancement

Progressive enhancement establishes a reliable baseline and adds capabilities when the browser supports them. A practical sequence is:

  1. Deliver semantic HTML and readable content.
  2. Add CSS for layout, typography, colour, and responsive behaviour.
  3. Add JavaScript for enhancements and interaction.
  4. Detect capabilities rather than guessing from device names.
  5. Preserve an understandable result when an enhancement fails.

For example, navigation links can remain visible in HTML and CSS, while JavaScript enhances them into a collapsible menu. A disclosure control should expose its state through aria-expanded, and keyboard users must be able to operate it.

Feature queries are useful for CSS enhancements:

.card-list {
  display: block;
}

@supports (display: grid) {
  .card-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
    gap: 1rem;
  }
}

This approach is preferable to browser-name detection. Browser detection becomes inaccurate as engines and devices change, whereas capability detection tests the feature actually required.

8. Reusable CSS Architecture

Responsive work becomes difficult when styles are tightly coupled to individual pages. A reusable architecture separates concerns and gives components predictable interfaces.

A modest architecture may include:

  • Tokens: colours, spacing, type sizes, radii, and shadows.
  • Base styles: document defaults and typography.
  • Layout objects: containers, stacks, clusters, and grids.
  • Components: cards, buttons, forms, navigation, and alerts.
  • Utilities: small single-purpose classes.
  • State styles: open, selected, disabled, invalid, and loading states.

Custom properties centralize design decisions:

:root {
  --space-1: 0.5rem;
  --space-2: 1rem;
  --space-3: 1.5rem;
  --content-max: 72rem;
  --brand: #1357a6;
  --text: #18202a;
}

.container {
  width: min(100% - 2rem, var(--content-max));
  margin-inline: auto;
}

Class names should describe a component or role rather than a location, such as .profile-card instead of .left-blue-box. Avoid excessive selector specificity, because it makes responsive overrides harder to maintain. Keep component styles independent of a particular page hierarchy where possible.

A responsive architecture should also account for content variation. Test long headings, translated text, user-generated values, error messages, and empty states. A component that works only with the developer’s sample content is not reusable.

9. Accessibility and Inclusive Responsiveness

Responsive design is an accessibility concern. A layout may technically fit a screen while remaining unusable for people with low vision, motor impairments, cognitive disabilities, or alternative input devices.

Important practices include:

  • Maintain meaningful heading hierarchy.
  • Use semantic landmarks such as header, nav, main, and footer.
  • Ensure keyboard focus remains visible and logically ordered.
  • Do not disable zoom or rely on tiny text.
  • Preserve adequate colour contrast and do not communicate meaning by colour alone.
  • Ensure controls have usable touch targets and sufficient spacing.
  • Support text enlargement and browser zoom.
  • Respect reduced-motion preferences.
  • Prevent horizontal scrolling caused by oversized content.
  • Keep form labels, validation messages, and instructions associated with controls.

A narrow layout should not remove important content merely because the viewport is small. Secondary information may be progressively disclosed, but users should be able to discover and access it.

10. Cross-Browser and Cross-Device Testing

Testing should be planned rather than postponed until the end. A useful test matrix includes:

DimensionExamples
ViewportNarrow phone, tablet, laptop, wide desktop
InputKeyboard, touch, mouse, screen reader
Browser engineChromium-based, Firefox, WebKit-based
ConditionsSlow network, offline state, zoom, dark mode
ContentLong text, translation, missing image, validation error
OrientationPortrait and landscape

Begin with real interaction paths: open navigation, complete a form, resize the window, rotate a device, zoom the page, and return to a previous state. Inspect overflow, focus order, sticky elements, dialogs, and fixed controls.

Developer tools provide responsive emulation, throttled networks, device-pixel-ratio simulation, and accessibility inspection. They do not replace testing on physical devices. Emulators cannot always reproduce browser chrome, keyboard appearance, touch scrolling, hardware performance, or text rasterization.

Automated tools can identify missing alternative text, colour issues, invalid ARIA, and some keyboard problems. Manual testing remains necessary for meaning, focus behaviour, reading order, content clarity, and interaction quality.

A professional workflow records the browser version, viewport, reproduction steps, expected result, actual result, and severity of each defect. Test critical journeys after every major layout or component change.

Applied Case: Illustrative Community Learning Portal

Background

A fictional community learning portal provides course listings, enrolment forms, announcements, and instructor profiles. Its initial desktop implementation uses a fixed 1,200-pixel wrapper, a horizontal navigation bar, and large 800-pixel images.

Problem

On phones, users must zoom and scroll horizontally. Course cards overflow their container, the navigation obscures the page, and images consume excessive mobile data. Keyboard focus is difficult to see after the menu is opened. The team proposes a redesign that must preserve course information and enrolment tasks.

Analysis

The team measures content rather than selecting device labels. The navigation begins to overlap at 760 pixels, and a two-column course layout becomes comfortable at approximately 680 pixels. Course cards are reused in the home page, search results, and a sidebar, so viewport media queries alone would not be suitable for their internal layout.

Approach and solution

The team:

  1. Adds the viewport meta element and replaces the fixed wrapper with a bounded fluid container.
  2. Creates a one-column mobile baseline using semantic HTML.
  3. Uses Grid with auto-fit for course listings.
  4. Adds a viewport media query to change global navigation at the point where its labels no longer fit.
  5. Adds a named container query to change the internal course-card arrangement.
  6. Supplies srcset, sizes, intrinsic dimensions, and meaningful alternative text.
  7. Keeps all course links available in the HTML and uses JavaScript only to enhance menu disclosure.
  8. Adds visible focus styles and supports keyboard escape from the menu.
  9. Tests long course titles, increased text size, dark mode, reduced motion, and slow network conditions.

Outcome

The portal becomes usable on narrow screens without horizontal scrolling. The browser selects smaller course images when appropriate, while larger screens receive sharper resources. Cards adapt correctly whether placed in a main results column or a narrow sidebar. The navigation remains understandable to keyboard and touch users.

Learning points

The case demonstrates that responsive design is a content and interaction problem, not merely a collection of CSS breakpoints. Fluid layout, component-level adaptation, performance-aware media, semantic structure, and testing must be planned together.

Challenges and Limitations

Responsive design cannot solve every product problem. A complex data table may require a transformed mobile presentation, horizontal scrolling, filtering, or an alternative summary. A map or diagram may need a separate interaction model. A narrow viewport cannot display unlimited information simultaneously, so prioritisation remains a product decision.

Browser support also changes over time. New CSS features may require fallbacks, feature queries, or a defined support policy. Excessive browser-specific workarounds increase maintenance cost. Conversely, refusing all modern features may produce unnecessarily complex code.

Performance is another limitation. Responsive CSS does not automatically make assets small. Large scripts, fonts, videos, and third-party services can dominate loading time regardless of layout quality. Measure actual performance and prioritise content visible at the start of the page.

Contemporary Practice and Professional Implications

Current responsive development increasingly combines CSS Grid, Flexbox, container queries, logical properties, fluid type scales, modern image formats, and accessibility preferences. Logical properties such as margin-inline and padding-block support different writing directions more effectively than fixed left and right properties.

Design systems formalise tokens and reusable components, allowing teams to maintain consistent responsive behaviour across products. Component documentation should state minimum usable widths, content assumptions, accessibility requirements, supported states, and testing expectations.

AI-assisted coding can suggest CSS or generate test cases, but generated code must be reviewed. An assistant may produce arbitrary breakpoints, inaccessible interaction, excessive JavaScript, or unsupported assumptions about browser behaviour. Human developers remain responsible for semantic correctness, security, privacy, performance, licensing, and inclusive design.

Best-Practice Checklist

Before releasing a responsive interface, confirm that:

  • The page has a correct viewport declaration.
  • The base layout works on a narrow viewport without horizontal overflow.
  • Breakpoints respond to content needs.
  • Components can adapt to their containers where appropriate.
  • Images have suitable dimensions, alternative text, and responsive sources.
  • Navigation and forms work with keyboard, touch, and screen readers.
  • Zoom, text enlargement, dark mode, and reduced motion have been considered.
  • CSS uses reusable tokens and low-specificity component rules.
  • Critical flows have been tested in major browser engines and on physical devices.
  • Performance has been measured under realistic network conditions.

Chapter Summary

Responsive web design creates interfaces that adjust fluidly to available space, while adaptive design supplies deliberate variants for particular conditions. A robust implementation usually begins with a mobile-first semantic baseline and progressively enhances it with Flexbox, Grid, fluid units, media queries, and container queries. Breakpoints should be selected from content and interaction requirements rather than device names.

Responsive images improve both visual quality and performance when combined with intrinsic dimensions, srcset, sizes, and appropriate loading strategies. Progressive enhancement keeps essential content and tasks available before optional CSS or JavaScript features are applied. Reusable CSS architecture uses tokens, layout objects, components, utilities, and state styles to make responsive behaviour maintainable.

Accessibility, performance, and cross-browser testing are integral rather than optional. Professional developers test real journeys across viewports, browsers, input methods, preferences, network conditions, and varied content. The central principle is to design for people and tasks across contexts, not for a fixed collection of device labels.

Key Terms

Responsive design: A design approach in which layout and presentation adjust to the available environment.

Adaptive design: An approach using predefined layouts or behaviours for selected conditions.

Mobile-first: Designing the smallest practical experience as the baseline, then adding enhancements for larger environments.

Viewport: The browser’s visible area in which a web document is rendered.

Breakpoint: A condition at which layout or behaviour changes because content or interaction requires it.

Media query: A CSS condition that applies rules according to viewport or user-environment features.

Container query: A CSS condition that applies rules according to a component container’s dimensions.

Fluid sizing: Sizing that changes relative to available space or another scalable reference.

Flexbox: A one-dimensional CSS layout system for arranging and aligning items.

CSS Grid: A two-dimensional CSS layout system for rows and columns.

Intrinsic size: The natural dimensions of an asset such as an image.

Responsive image: An image implementation that supplies appropriate resources for different contexts.

Progressive enhancement: Building a reliable baseline and adding supported enhancements progressively.

Design token: A named reusable value for a design decision such as colour or spacing.

Specificity: The precedence weight used by CSS when competing selectors apply.

Cumulative layout shift: Unexpected movement of page content during loading or interaction.

Accessibility: Designing technology so people with diverse abilities can use it effectively.

Feature query: A CSS @supports condition that tests whether a browser supports a capability.

Logical property: A CSS property expressed relative to writing direction, such as margin-inline.

Cross-browser testing: Evaluating behaviour across browser engines, devices, and configurations.

Review Questions

Short Answer Questions

  1. What is the main difference between responsive and adaptive web design?
  2. Why is the viewport meta element important for mobile pages?
  3. When is a container query more appropriate than a media query?
  4. What information do srcset and sizes provide to a browser?
  5. State two accessibility considerations for responsive interfaces.

Long Answer Questions

  1. Explain how a mobile-first workflow can improve both design quality and maintainability.
  2. Compare Flexbox and CSS Grid, giving examples of suitable uses for each.
  3. Describe a professional strategy for choosing and validating breakpoints.
  4. Explain progressive enhancement and show how it can be applied to navigation.
  5. Discuss the role of reusable CSS architecture in a large responsive application.

Analytical / Application Questions

  1. A card component appears in both a wide main column and a narrow sidebar. Explain why a container query may be preferable to a viewport media query and outline an implementation.
  2. A website displays horizontal scrolling at 390 pixels wide. Identify at least four possible causes and propose a systematic debugging process.
  3. Design a testing plan for an enrolment form that must work on phones, desktops, keyboard-only systems, screen readers, slow networks, and dark-mode settings.

Multiple Choice Questions with Answers

Question 1

Which approach best describes mobile-first development?

A. Designing only for mobile devices

B. Starting with a practical small-screen baseline and adding enhancements

C. Using fixed pixel widths for phones

D. Serving a separate website to every device

Correct Answer: B. Starting with a practical small-screen baseline and adding enhancements

Question 2

What does a container query primarily evaluate?

A. The browser’s JavaScript version

B. The viewport’s height only

C. The size of a component’s containing context

D. The network’s download speed

Correct Answer: C. The size of a component’s containing context

Question 3

Which CSS unit distributes available space in a Grid track?

A. fr

B. px

C. deg

D. s

Correct Answer: A. fr

Question 4

What is the principal purpose of the sizes attribute on a responsive image?

A. To provide alternative text

B. To describe the expected rendered width in layout conditions

C. To force every browser to download every image

D. To define the image’s colour profile

Correct Answer: B. To describe the expected rendered width in layout conditions

Question 5

Which practice helps prevent layout movement while an image loads?

A. Removing the image dimensions

B. Using only a background colour

C. Providing intrinsic width and height information

D. Disabling all image loading

Correct Answer: C. Providing intrinsic width and height information

Question 6

Why should breakpoints generally be based on content rather than device names?

A. Device names determine font families

B. Content may stop fitting at different widths and devices vary

C. Device names are required by HTML

D. Content never changes size

Correct Answer: B. Content may stop fitting at different widths and devices vary

Question 7

What is progressive enhancement?

A. Replacing semantic HTML with JavaScript

B. Building a reliable baseline and adding supported capabilities

C. Using the largest possible image first

D. Avoiding all browser feature detection

Correct Answer: B. Building a reliable baseline and adding supported capabilities

Question 8

Which layout system is primarily one-dimensional?

A. CSS Grid

B. Flexbox

C. SVG

D. Media Queries

Correct Answer: B. Flexbox

Question 9

Which user preference should motion-sensitive interfaces respect?

A. prefers-reduced-motion

B. prefers-large-images

C. prefers-fixed-width

D. prefers-no-semantic-html

Correct Answer: A. prefers-reduced-motion

Question 10

Which is an appropriate cross-browser testing practice?

A. Testing only the developer’s default browser

B. Testing only after deployment

C. Combining automated checks with manual testing across engines and input methods

D. Assuming emulation perfectly reproduces every physical device

Correct Answer: C. Combining automated checks with manual testing across engines and input methods

Introduction to Web Technologies (HTML, CSS, JS)
Chapter 8 of 15