Responsive Web Design: Viewports and Media Queries

Make web pages that work well on phones, tablets and desktops. Set the viewport so mobile browsers use the real screen width, size text and boxes with relative and viewport units, and change layouts with media queries written mobile-first. Use container queries to let components adapt to the space they are given, serve images that fit each screen with srcset, sizes and <picture>, and test pages across widths, zoom levels, touch input and reader preferences.

BeginnerLesson 6 of 8 in Web Development Basics Course

What you will learn

  1. Explain what responsive design is, make mobile browsers lay out a page at the device's real width, and size text and boxes with units that scale.
    • Explain responsive design and set the viewport
      Describe what responsive design does, how CSS pixels relate to device pixels, and why every responsive page needs the viewport meta tag.
    • Size text and boxes with relative units
      Use percentages, em, rem and ch so that boxes follow their container and text follows the reader's font-size setting.
    • Use viewport units and fluid sizing
      Size elements against the viewport with vw, vh and the newer small, large and dynamic units, and set limits with min(), max() and clamp().
  2. Write media queries, build styles mobile-first with breakpoints chosen by the content, and turn flexbox and grid layouts into layouts that adapt to the screen.
    • Write media queries
      Use media types, width conditions, logical operators and other media features, and place media queries correctly in the cascade.
    • Build mobile-first with content-based breakpoints
      Write base styles for small screens, add min-width queries as space grows, and place breakpoints where the content needs them.
    • Make flexbox and grid layouts responsive
      Change layouts at breakpoints, and use intrinsic grid and flexbox patterns that adapt without any media query.
  3. Declare query containers, style components with container queries and container units, and choose between media queries, container queries and intrinsic layouts.
    • Set up containers and write container queries
      Declare a query container with container-type, write @container rules, name containers and avoid the common setup mistakes.
    • Size with container units and choose the right tool
      Use container query units for sizes that follow a container, and decide when to use a media query, a container query or an intrinsic layout.
  4. Keep images and embedded media inside their containers, let the browser choose a suitable file with srcset and sizes, and change the crop or format with the picture element.
    • Make images and embedded media flexible
      Stop images and videos from overflowing, keep their proportions, reserve space before they load, and control cropping with object-fit.
    • Let the browser choose an image size with srcset and sizes
      Offer the same image in several files, describe how wide it is displayed, and predict which file the browser loads.
    • Change the crop or format with the picture element
      Use picture and source to show a different crop at different widths or to offer modern image formats with a fallback.
  5. Check pages across widths, zoom levels and real devices, find and fix overflow, and adapt to touch input and the reader's settings.
    • Test across widths and fix overflow
      Use the browser's responsive design tools and real devices, check reflow at 320 CSS pixels, and track down the causes of horizontal scrolling.
    • Design for touch, zoom and user preferences
      Size controls for fingers, avoid hover-only interactions, support zoom and orientation, and respond to reduced motion, color scheme and print.

Responsive Web Design study guide

5 chapters

1

Prepare a page to adapt to any screen

Good responsive behavior starts with the space the browser thinks it has and the reference each size follows. Set up the mobile viewport, distinguish CSS pixels from screen dots, then choose units for text, boxes and full-height sections. A container that can shrink and text that respects the reader’s settings give later layout changes a reliable base.

1.1 One page, many screen widths

Responsive design lets one HTML page adapt as its available space changes. A phone held upright, a tablet turned sideways and a resized desktop window should all show readable text, usable controls and images that fit. You design for a continuous range of widths, including the spaces between familiar device sizes.

Ethan Marcotte introduced the term in 2010 around three techniques: fluid layouts, flexible images and media queries. Flexbox, grid, container queries and responsive image markup now extend that toolkit. Each solves a different part of the job; a page needs more than a single setting.

Count the right pixels

A CSS pixel is a layout unit, not necessarily one dot on the screen. CSS defines 96px as equal to 1in; on a screen, that relationship does not promise a measured physical inch. A high-density screen uses several device pixels for each CSS pixel. The device pixel ratio describes that relationship.

A phone 390 CSS pixels wide with a ratio of 3 has 1170 device pixels across. A width query still sees 390px. The extra dots help draw sharp text and images; they do not give a layout three times as much space. This distinction matters when you later choose image files.

The viewport is the browser area used to display the page, excluding its own toolbars. Resizing a window or rotating a device changes the space available. On phones, showing and hiding toolbars can also change the visible height.

Tell mobile browsers the intended width

Mobile browsers can lay out an unconfigured page in a wide virtual viewport, commonly around 980 CSS pixels, then shrink it to the screen. On a 390px phone, the scale is about 0.4. Text declared at 16px then looks about 6.4px tall, and a narrow-width query may never match because the layout viewport is still wide.

Place this inside the document's <head>:

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

width=device-width asks for the device width in CSS pixels. initial-scale=1 starts at normal zoom. After adding it, reload the page in a phone-sized browser view and check both the text and the active width queries.

Keep zoom available. Do not add user-scalable=no or maximum-scale=1. Readers with low vision need to enlarge content; text must support enlargement to 200% without losing content or function. Some mobile browsers ignore zoom restrictions, but the page should support zoom itself.

Check the layout as well

The viewport tag does not change a fixed-width box. A .page with width: 960px still extends beyond a 390px viewport. Change rigid widths into fluid widths with limits, make media flexible and add layout changes where the content needs them.

To diagnose a tiny mobile page, first inspect the <head> for the viewport tag. To diagnose a page that is normal-sized but scrolls sideways, inspect the boxes that exceed the available width. These symptoms point to different causes.

Key Takeaways

  • Responsive pages adapt across a continuous range of widths.
  • Layout uses CSS pixels; device pixel ratio describes screen density.
  • Put the viewport meta tag in the document head.
  • Leave zoom enabled and make fixed-width content flexible.

1.2 Sizes that follow text and containers

Choose a unit by asking what the size should follow. A column usually follows its containing block. Button padding may follow the button's text. A paragraph's line length follows the font. Using the right reference makes changes work together.

UnitWhat it followsUseful application
%A percentage width follows the containing block's widthFluid boxes and columns
emParent font size for font-size; own font size for other propertiesButton padding and icons
remRoot element's font sizeText and shared spacing
chWidth of the font's "0" glyphA readable line-length limit
pxCSS reference pixelsA thin border
Percentages and the box model

A box with width: 50% is 300px wide in a 600px containing block and 180px wide in a 360px one. Percentage padding and margins, including top and bottom values, also use the containing block's width in normal horizontal writing. Thus padding-top: 10% in a 600px-wide parent is 60px.

Height is different. A percentage height needs a definite reference height. When an ordinary parent's height comes from its content, height: 50% generally behaves as auto. Let text boxes grow with their content instead of using percentage heights to control them.

Use a fluid container with a maximum:

.page {
  box-sizing: border-box;
  width: 100%;
  max-width: 70rem;
  margin-inline: auto;
  padding-inline: 1rem;
}

On a narrow screen, the box fills the available width, with padding inside it. On a wide screen, it stops at 70rem and the automatic inline margins center it. With a 16px root, its maximum outer width is 1120px. border-box includes padding and borders in the declared width; under content-box, 100% width plus padding would overflow. A fixed width: 70rem would also prevent shrinking.

For example, a 340px-wide border-box wrapper with 16px of padding on each side has 308px left for its content when there is no border. The declared width describes the outside of this box. If you switch the same declaration to content-box, the content alone is 340px and the outer width becomes 372px. Inspecting these separate widths in developer tools makes padding-related overflow easier to spot.

Let text settings carry through

Leave the root font size at the reader's default, or use a percentage such as 100%. At a 16px root, 1.5rem is 24px; at a 20px root, it is 30px. A fixed html { font-size: 16px; } prevents that default-font preference from carrying through to root-relative sizes.

For font-size, em uses the parent's font size. Nested lists each set to 1.2em therefore compound: 16px becomes 19.2px, then 23.04px. Use rem when all nesting levels should keep the same size.

In other properties, em uses the element's own font size. A button with padding: 0.5em 1em gains padding as its text grows. That keeps the spacing in proportion without a separate breakpoint.

For paragraphs, max-width: 65ch provides a useful reading limit. The unit measures the width of "0", so it does not count characters exactly in a proportional font. Roughly 45–75 characters per line is a common reading guideline; check the actual font and text in the browser.

Key Takeaways

  • Percentages make widths follow their containing block.
  • Use border-box to keep padding inside a declared width.
  • rem follows the root font size without nested compounding.
  • em can keep component spacing proportional to its own text.
  • ch offers a font-based line-length limit.

1.3 Fluid sizes with useful limits

Viewport units follow the browser's viewport even when an element sits inside a smaller box. 1vw is 1% of the viewport width; 1vh is 1% of its height. vmin uses the smaller dimension, and vmax uses the larger. In an 800px-wide viewport, 50vw is 400px. In a 300px-tall viewport, 70vh is 210px.

This reference matters. A sidebar can be only 260px wide in a 1400px window. A child with width: 50vw asks for 700px, so it will not fit that sidebar. Use a percentage when the child should follow its parent. For an ordinary full-width block, prefer width: 100%: 100vw can include a desktop scrollbar's width and create sideways scrolling.

Choose which mobile height you mean

Mobile browser toolbars can reduce the visible page area. The viewport height units distinguish three states:

UnitHeight it followsMain tradeoff
svhSmall viewport, with toolbars shownStable size that fits that state
lvhLarge viewport, with toolbars hiddenMay extend behind visible toolbars
dvhCurrent dynamic viewportCan resize during scrolling

Plain vh corresponds to the large viewport. Width variants such as svw, lvw and dvw also exist. For a tall opening section, min-height: 100svh sets a stable minimum while allowing longer content to grow. A fixed height leaves overflow to spill out when the content becomes taller. Dynamic units can follow toolbar changes, but updates may happen in steps and make the layout move.

Choose the height reference by the behavior you need. A stable opening section can use the small viewport even after the toolbars disappear; it does not have to stretch again just because more space becomes visible. A panel intended to follow that changing space can use dynamic units, accepting the possible movement. In either case, a minimum height lets additional content extend the section in normal flow.

Put bounds around a fluid value

min() selects the smaller value and can cap a width: width: min(100%, 40rem) fills the parent until it reaches 40rem. max() selects the larger value and provides a floor. clamp() gives a floor, a preferred value and a ceiling in that order.

.title {
  font-size: clamp(1rem, 0.5rem + 2vw, 2rem);
}

With a 16px root, the floor is 16px, the preferred size is 8px plus 2% of the viewport width, and the ceiling is 32px. Work through the bounds:

  1. At 375px, the preferred size is 15.5px, so the 16px minimum applies.
  2. At 1000px, the preferred size is 28px, within the limits.
  3. At 1400px, the preferred size is 36px, so the 32px maximum applies.

The same functions accept calculations such as min(100% - 2rem, 70rem). Keep compatible length values and spaces around the subtraction operator.

Text sized only in vw can become tiny on narrow screens and huge on wide ones. Desktop zoom also narrows the viewport in CSS pixels, which can cancel that text's enlargement. Include a font-relative part and useful bounds, then test text enlargement: a mixed rem/vw expression alone does not guarantee a full 200% increase.

Key Takeaways

  • Viewport units follow the window, even inside a narrow parent.
  • Use svh for the small viewport and dvh for the changing visible height.
  • min() caps a value; max() gives it a floor.
  • clamp() sets a minimum, preferred value and maximum.
  • Test fluid type with zoom as well as changing window widths.
2

Adapt layouts with media queries

A layout needs to work while the window is changing, including between its major arrangements. Write conditions that apply CSS at the right time, start from a useful narrow page and add columns when the content has room. Flexbox and grid can handle many changes automatically; explicit breakpoints handle the moments when the arrangement itself needs to change.

2.1 Conditions that switch CSS rules

A media query applies CSS when a condition about the output, viewport or reader's preferences is true. The browser checks again when relevant conditions change, so resizing a window can switch styles without reloading the page.

.heading { font-size: 1.25rem; }

@media (min-width: 40em) {
  .heading { font-size: 2rem; }
}

At the usual 16px browser default, 40em is 640px. The larger heading applies from that width upward. In media queries, em and rem use the browser's initial font size, not an author-set size on <html>. A reader with a 20px default gets the same switch at 800px.

Read conditions precisely

min-width includes its boundary: 600px matches min-width: 600px. max-width also includes its boundary. Range syntax makes the comparison explicit:

@media (width >= 600px) { /* wider layout */ }
@media (600px <= width < 900px) { /* middle band */ }

If you pair max-width: 600px with min-width: 600px, both match at 600px. Using 599px and 600px instead leaves a gap at fractional widths such as 599.5px. Increasing minimum-width queries avoid that gap; complementary ranges such as < 600px and >= 600px are another option.

Media types identify output: screen, print or the default all. @media print can remove navigation and use dark text on white paper. Conditions combine in useful ways:

FormMeaning
screen and (min-width: 40em)Both the output type and width must match
(max-width: 30em), printEither narrow output or printing matches
not printThe output is not print

only has no useful effect in current browsers. Width and height describe the viewport; orientation: portrait means its height is at least its width. Other features test input precision, hover, resolution, reduced motion and color scheme. A landscape viewport does not tell you whether the reader has a mouse.

For a practical condition, @media (hover: hover) and (pointer: fine) requires a primary input that can hover and target precisely. A narrow mouse-driven window can match it, while a wide touch-first device may not. Use the query for optional pointer effects; keep the underlying action available through the normal page controls.

Keep the cascade in view

A media query does not increase a selector's specificity. In a normal stylesheet with equally specific rules, the later matching declaration wins. If the base .heading rule appears after the wider-screen rule above, it wins at wide widths too. Put base rules first and the queries that change them afterward.

You can also attach a condition in HTML. <link rel="stylesheet" href="print.css" media="print"> applies its rules when printing, but a browser can still download the file at a lower priority. A <source> element's media attribute uses conditions to choose an image source.

Key Takeaways

  • Media queries respond to output, viewport features and user preferences.
  • min-width and max-width include their boundary values.
  • and requires all conditions; a comma offers alternatives.
  • Query rules still obey the normal cascade and specificity.
  • Relative query units use the browser’s initial font size.
  • Complementary ranges avoid fractional-width gaps.

2.2 Add layout as space becomes available

Mobile-first CSS starts with the narrow layout as ordinary rules outside any query. Minimum-width queries then add changes when space permits. This suits normal document flow: blocks already stack vertically, so the base page often needs little layout code.

Desktop-first CSS starts with wide-screen rules and uses maximum-width queries to undo them. Both approaches can work, but mobile-first usually makes the base and each later change easier to follow.

Build from one column
.cards {
  display: grid;
  gap: 1rem;
}

@media (min-width: 40em) {
  .cards { grid-template-columns: 1fr 1fr; }
}

@media (min-width: 64em) {
  .cards { grid-template-columns: repeat(3, 1fr); }
}

With a 16px default, the changes happen at 640px and 1024px. A 500px viewport shows one column. At 800px, the first query matches and gives two columns. At 1200px, both queries match; the later rule gives three columns.

The wider query does not turn the earlier query off. If you reverse their order, the 40em rule is last and reduces the wide layout to two columns. Keep increasing minimum-width queries in increasing order when they change the same property with the same specificity.

Find the breakpoint in the content

A breakpoint is where a query changes the layout. Start with the actual content in a narrow window, then widen it slowly. Look for navigation wrapping awkwardly, paragraphs becoming too long or cards stretching beyond a useful reading width. Add a change where it improves that content.

Do not choose a breakpoint just because a familiar device has that width. Screens vary, desktop windows can be any size, and a larger font can make the same navigation need more space. Relative-unit breakpoints can help the layout respond to the reader's default text size.

After each change, test both sides of the breakpoint and several widths between changes. A layout that looks right only at 375px, 768px and 1280px still has many opportunities to fail. Fluid widths, wrapping and automatic grid columns handle much of that in-between space.

Test the navigation and the card list together after adding a page sidebar. The window has become wider, but the main column may have become narrower because it now shares that space. A card layout that worked just below the page breakpoint can fail just above it. This is a useful place to let cards wrap or fit automatic columns while the page itself uses an explicit query.

Keep the content and downloads useful

Phone users need the page's content too. If a secondary explanation takes too much space, stack it lower or put it in a native <details> disclosure that readers can open. Do not remove necessary information just to make a narrow screenshot look tidy.

Starting narrow also helps you choose a light default page. Hiding a large image with CSS does not reliably prevent its download; use responsive image markup to offer suitable files. A readable single-column page with a smaller image is a strong starting point, then wider layouts add room and rearrange it.

Key Takeaways

  • Write narrow-screen base rules outside queries.
  • Add matching min-width changes in increasing order.
  • Place breakpoints where the content needs a layout change.
  • Test widths between breakpoints, including larger text.
  • Reorganize useful content for phones and send suitable image files.

2.3 Responsive rows and columns

A layout can adapt through an explicit query or through its own sizing rules. Use a query when the arrangement must change at a chosen point. Use an intrinsic layout when items simply need to fit into the available space.

Let grid count the columns
.tiles {
  display: grid;
  grid-template-columns:
    repeat(auto-fit, minmax(min(15rem, 100%), 1fr));
  gap: 1rem;
}

At a 16px root, each column aims to be at least 240px. Grid fits as many as possible, subtracts the gaps, then shares the remaining width with 1fr. In a 1000px container, four 240px columns plus three 16px gaps would need 1008px, so only three fit. Each gets about 322.7px. A 700px container gets two 342px columns; a 360px container gets one.

The inner min(15rem, 100%) matters when the container is under 240px wide. It lets the minimum become the available width. Without it, the 240px minimum would force overflow. auto-fit collapses empty tracks, letting a few items expand; auto-fill keeps empty tracks and can leave those items narrower.

Let flex items wrap
.links {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.links > * { flex: 1 1 15rem; }

Each item starts from a 15rem basis, can shrink or grow and wraps when the row cannot fit more items. Each line shares its own remaining space. Two items on the final line can therefore become wider than items above them. Grid is a better fit when column widths should line up across rows. Navigation can wrap too, or a query can switch its flex-direction to column.

Change the whole page when needed

Start the page in normal flow. Then add a sidebar arrangement:

@media (min-width: 48em) {
  .page {
    display: grid;
    grid-template-columns: 16rem minmax(0, 1fr);
    gap: 1rem;
  }
}

At the usual 16px default, this starts at 768px. The first column is the sidebar; the second takes the remaining space. The zero minimum lets the main track shrink; its contents still need to wrap or scroll appropriately. Named grid areas can express a more complex arrangement.

Keep the HTML in a sensible reading order. Flex order and grid placement change where elements appear, but keyboard focus and screen readers still follow source order. Test by tabbing through links after rearranging them.

A wide data table may need its two-dimensional structure. Give it a local scrolling wrapper with overflow-x: auto, tabindex="0", role="region" and an accessible name such as aria-label="Event schedule". Only the table region should scroll sideways; the surrounding page should still reflow.

Key Takeaways

  • Intrinsic layouts reflow without a breakpoint.
  • auto-fit and minmax() fit columns and share spare space.
  • Cap the column minimum at 100% to avoid narrow-container overflow.
  • Flex lines share space independently; grid aligns columns across rows.
  • Visual reordering does not change the HTML reading or tab order.
  • Keep wide tables in a reachable, named scrolling region.
3

Make components respond to their container

A component can appear in a wide main column or a narrow sidebar on the same page. Its own available width may matter more than the width of the window. Declare a measuring container, change the elements inside it and use container-relative sizes when they suit the component. Choosing the right reference keeps a reusable card useful wherever it is placed.

3.1 A card that follows its own space

A viewport-width query gives the same answer for every component on a page. That can be wrong for a reusable card: the window may be 1400px wide while the sidebar gives the card only 300px. A container size query measures an ancestor's available space, so the card can adapt to its actual slot.

Declare a wrapper, then style its contents
<div class="card-wrap">
  <article class="card">...</article>
</div>
.card-wrap { container-type: inline-size; }
.card { display: grid; gap: 1rem; }

@container (min-width: 25rem) {
  .card { grid-template-columns: 8rem minmax(0, 1fr); }
}

In horizontal writing, inline-size is width. It declares that the wrapper can answer inline-size queries. With a 16px root font, 25rem is 400px. A card in a 300px wrapper stays stacked; another in a 500px wrapper puts its image beside its text. Both can be on the same page at the same time.

A query measures an ancestor of the element being styled. If you put container-type only on .card and then try to change that same card, the query still looks above it. Use a wrapper as the measuring box and the inner card as the styled box.

Select the intended ancestor

An unnamed size query uses the nearest suitable ancestor size container. In a nested layout, that may be an inner wrapper. A name lets you choose a particular ancestor:

.panel { container: sidebar / inline-size; }

@container sidebar (width >= 30rem) {
  .panel-title { font-size: 1.5rem; }
}

The shorthand sets both the name and type. If there is no suitable ancestor, the query does not match; the base styles continue to apply. Conditions can use ranges and logical operators such as and, or and not.

Make sure the container gets its size

Containment prevents the queried dimension from depending on the very contents that the query may restyle. Put container-type on a box whose width comes from the parent layout, such as a block, grid area or growing flex item. A shrink-to-fit box can collapse when its contents no longer contribute to its inline size.

container-type: size supports both width and height queries, but also contains block sizing. Its height needs an independent sizing arrangement; otherwise it can collapse. For ordinary cards, inline-size lets the height continue to follow the content. The default normal does not establish a size-query container.

Size features include width, height, inline size, block size, aspect ratio and orientation, provided the chosen container type supports the dimensions involved. Current major browsers support size queries. Keep a usable stacked base layout so the card still works if a browser ignores the enhancement.

Key Takeaways

  • Put container-type on an ancestor wrapper.
  • inline-size queries suit cards whose height should follow content.
  • A container query styles descendants, not its own measuring box.
  • Name a container when nested ancestors could change the intended reference.
  • Give a query container a size through its surrounding layout.

3.2 Choose the size reference that fits the job

A responsive page can combine three approaches: automatic reflow, viewport conditions and component conditions. Decide what information the layout needs before choosing a query.

NeedUseful toolReason
Fit repeated cards into a rowIntrinsic grid or wrapping flexboxItems can reflow without a threshold rule
Change a page-wide sidebar arrangementMedia queryAvailable window space controls the page
Change a card in different page slotsContainer queryThe card needs its wrapper's size
Respect reduced motion or print outputMedia queryThese are user or output conditions
Size within a container

Container query units describe percentages of a suitable ancestor container. cqw and cqh use width and height. cqi and cqb use inline and block size. cqmin and cqmax use the smaller and larger of the two dimensions. In horizontal writing, inline size is width.

In a 400px-wide eligible container, 5cqi is 20px. In a 600px-wide one, it is 30px. This lets a heading follow its own component, even when the whole page remains the same width.

Keep limits around text:

.card-wrap { container-type: inline-size; }
.card h2 {
  font-size: clamp(1rem, 4cqi, 1.5rem);
}

With a 16px root, a 300px wrapper gives a preferred 12px, so the 16px minimum applies. At 600px, 4cqi is 24px, equal to the ceiling. Wider wrappers cannot push the heading above 24px. Test enlargement as well as wrapper resizing; fluid text must still remain usable when readers zoom.

Container units choose the nearest eligible ancestor for the direction measured. If none exists, they fall back to small viewport units. A forgotten container-type can therefore make text change with the window while appearing to work. Inspect the ancestors when a size seems to follow the wrong space. A container name on a query does not, by itself, retarget every container unit to that named ancestor.

Combine the tools

A page can switch to sidebar-and-main columns at 48em through a media query. Inside the main area, an automatic grid can fit as many cards as space allows. A card's own container query can move its image beside its text only when that particular slot is wide enough.

Do not replace a reduced-motion or hover query with a container query. A box's dimensions do not reveal a reader's preferences or input device. Likewise, copying a card's rules into separate viewport queries for every page slot ties that reusable component to each surrounding layout. Let its local space decide when its arrangement changes.

Key Takeaways

  • Choose intrinsic reflow when no arrangement switch is needed.
  • Media queries handle page-wide conditions and user preferences.
  • Container queries handle a component’s available space.
  • Container units use a suitable ancestor for each measured direction.
  • Without an eligible container, container units fall back to small viewport units.
4

Deliver images and media that fit every screen

Images need both a flexible display box and a sensible file to download. Preserve their shape, reserve loading space and decide when cropping is acceptable. Then describe alternative resolutions, crops and formats so the browser can choose from them. Matching image markup to the CSS layout keeps a page clear on dense screens without unnecessary downloads.

4.1 Flexible media without distortion

An image can be wider than the space the layout gives it. Start with a rule that lets it shrink while preserving its shape:

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

max-width limits the displayed width to the container without forcing a small image to fill it. width: 100% would stretch a small logo to the full width. Automatic height preserves the aspect ratio as the width changes.

Reserve the space before download

Keep dimension attributes in the HTML:

<img src="coast.jpg" width="1200" height="800"
     alt="A rocky coast with a walking path">

Those dimensions give a 3:2 aspect ratio, so the browser can reserve space before the file arrives. When CSS displays the image 360px wide, the matching height is 240px. Removing the attributes because the image is responsive loses that early sizing information. CSS can still make the displayed image smaller than the attribute dimensions.

For an embedded video player, give the box its own ratio:

iframe {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
}

This keeps a 16:9 player shape while its width changes. Keep heights content-driven for text; a fixed aspect ratio is useful for media boxes with a known shape.

Choose whether cropping is allowed

A card thumbnail often has to fill a uniform box. Give it a width and an aspect ratio, then choose how the source fits:

.thumbnail {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  object-position: top;
}

cover fills the box and crops parts that extend outside it. contain shows the whole image and can leave empty bands. The default fill stretches the image to the box, which can distort it. object-position chooses the alignment used when fitting; top alignment keeps the top portion visible in this example.

GoalSetting
Show an entire diagramobject-fit: contain
Fill a thumbnail box, accepting a cropobject-fit: cover
Preserve the natural shape while shrinkingheight: auto with a width limit

For a concrete comparison, place a 400 × 200 photo in a 320 × 240 thumbnail. Cover scales it to 480 × 240, then crops 160px of the scaled width in total. Contain scales it to 320 × 160, leaving 80px of unused height in total. Both preserve the source proportions; the choice is whether the box must be filled or the whole picture must stay visible.

Content images belong in <img> with useful alternative text. CSS backgrounds suit decoration; background-size: cover can crop them to fill their box, and media queries can switch the background file.

For images below the opening screen, loading="lazy" can defer fetching until the reader approaches them. Leave the main opening image eager so it can appear promptly. Shrinking a 4000-pixel photo with CSS still downloads that photo; responsive image markup is needed to choose a smaller file.

Key Takeaways

  • Limit media width and preserve its aspect ratio.
  • Keep width and height attributes to reserve loading space.
  • cover crops; contain keeps the whole image visible.
  • Use img for content and CSS backgrounds for decoration.
  • Lazy-load lower images, not the main opening image.
  • CSS display size does not reduce the downloaded file.

4.2 Offer the right image resolution

One photo can have several files with the same content and proportions but different pixel dimensions. Resolution switching lets the browser choose a file using the display size, screen density and its loading decisions. It saves unnecessary data while keeping enough detail.

Fixed display size: density descriptors
<img src="logo-200.png"
     srcset="logo-200.png 1x, logo-400.png 2x"
     width="200" height="80" alt="Northwind Books">

The logo is displayed 200 CSS pixels wide. The 200-pixel file suits a ratio-1 screen; the 400-pixel file supplies two source pixels per displayed CSS pixel for ratio 2. This form uses x descriptors and needs no sizes attribute.

Changing display size: width descriptors
<img src="park-800.jpg"
     srcset="park-480.jpg 480w,
             park-800.jpg 800w,
             park-1600.jpg 1600w"
     sizes="(min-width: 64em) 50vw, 100vw"
     width="1600" height="1000"
     alt="Trees around a park pond">

Each w value must state the file's real pixel width. It does not set a CSS width. The CSS still controls the box, and sizes describes that intended display width to the browser.

The first matching condition in sizes wins. With a 16px browser default, at 1024px or wider the slot is 50vw; below that it is 100vw. The final unconditional length is the fallback. Lengths can use viewport units, pixel or font units, and calculations such as calc(100vw - 2rem), but not percentages. When sizes is absent with width descriptors, the browser normally assumes 100vw.

Work out the needed detail

Multiply the described display width by the device pixel ratio. For a smallest-sufficient selection, compare that target with the supplied file widths:

  1. A 1200px viewport at ratio 1 uses the 50vw slot: 600 CSS pixels. The 800w file is the first large enough.
  2. A 390px viewport at ratio 3 uses 390 CSS pixels. It needs 1170 source pixels, so 1600w meets the target.
  3. The same narrow viewport at ratio 2 needs 780 source pixels, which 800w meets.

Real browsers can reuse a cached larger file or favor a smaller one under constrained loading conditions. The markup supplies candidates and sizing information, not an unconditional command to fetch one exact file.

Browsers begin fetching images before full CSS layout is available. That is why the sizing hint belongs in HTML. If the CSS gives an image half the viewport but sizes says 100vw, the browser may fetch too large a file. Update sizes when the layout changes. Keep src as a fallback and use a single alt text. Different crops need <picture>, because ordinary resolution candidates should remain interchangeable views of the same image.

Key Takeaways

  • Use x descriptors for a fixed display size and different densities.
  • Use w descriptors for file widths and sizes for the intended slot.
  • The first matching sizes condition wins; percentages are not slot lengths.
  • Estimate source width from display width and device pixel ratio.
  • Keep sizes consistent with CSS; the browser retains selection freedom.

4.3 Choose a crop or image format

Reducing a photo's pixel dimensions keeps the same composition. Sometimes a phone needs a different composition: a wide stage photo can make the speaker too small to recognize. Art direction changes the crop or image so its important subject remains clear.

The <picture> element supplies ordered source choices, followed by a required <img>:

<picture>
  <source media="(min-width: 50em)"
          srcset="stage-wide.jpg" width="1600" height="900">
  <img src="stage-close.jpg" width="800" height="1000"
       alt="A speaker presenting on stage">
</picture>

With a 16px default, the wide crop is selected from 800px upward; below that, the image uses the closer crop. The <img> remains the displayed element. It supplies fallback behavior and the alternative text, and it is the element your image CSS styles. A <picture> without <img> displays no image.

Put the strongest conditions first

The browser chooses the first supported source whose conditions match. Suppose you offer crops at 30em and 60em. List the 60em minimum first, then 30em, then the fallback. If 30em is first, it also matches a very wide screen and prevents the browser reaching 60em. Non-overlapping ranges can also make the intended choice clear.

A source's media condition is a selection instruction. By comparison, ordinary srcset candidates and sizes give the browser information to choose an appropriate resolution. Use art direction when the composition needs to change; use resolution switching when the image content stays the same.

Offer formats in preference order
<picture>
  <source type="image/avif" srcset="hall.avif">
  <source type="image/webp" srcset="hall.webp">
  <img src="hall.jpg" width="1200" height="800"
       alt="Rows of seats in a lecture hall">
</picture>

A browser that supports the first format can use it; otherwise it tries the next supported choice and ultimately the JPEG fallback. AVIF and WebP often provide smaller files at comparable visual quality. The type attribute lets the browser skip unsupported formats without downloading them to find out.

Each source can also offer several width candidates with its own srcset and sizes. That combines a chosen crop or format with resolution switching. When art-directed crops have different proportions, dimension attributes on the sources let the browser reserve the appropriate space for each crop.

Write the alternative text once, on <img>, and make sure it describes every crop. If a close crop removes a second person, an alt description naming two people no longer fits. For a purely decorative image, use alt="" so it does not add unnecessary spoken content.

Key Takeaways

  • picture needs an img for display, fallback and alt text.
  • Use art direction when the composition changes.
  • The first supported matching source wins, so source order matters.
  • A source type can offer a preferred format with fallback.
  • Every crop must agree with the one alt description.
5

Test and refine responsive pages

A page can look correct in one screenshot and still fail with a longer title, larger text or a different input device. Test its widths and zoom levels, trace boxes that overflow and inspect the images it downloads. Touch targets, motion preferences, color themes and print output complete the checks that make the same content usable in different conditions.

5.1 Find the width where the page fails

A responsive page needs testing across a range, not just at a few named devices. Open the browser's responsive view, set a narrow viewport near 320px and drag toward a wide window. Watch headings, navigation, cards and image edges as you move. Test immediately below, at and above each breakpoint.

Developer tools can emulate viewport dimensions, device pixel ratio and touch input. Chrome and Edge provide a device toolbar; Firefox and Safari provide responsive design modes. Emulation is useful, but real devices can differ in toolbar behavior, touch, fonts and performance. Check a real phone and tablet in both orientations, plus a resized desktop window.

Check reflow and enlarged text

WCAG's AA reflow criterion asks that ordinary content work at a width of 320 CSS pixels without requiring scrolling in two directions. A 1280px-wide viewport zoomed to 400% provides that width. Set a 320px responsive viewport for a direct narrow-width check, and also test actual desktop zoom to see how the page enlarges.

Content that needs a two-dimensional layout, such as a data table, map or diagram, can retain that arrangement. Keep any necessary sideways scrolling inside its own region. Surrounding headings, instructions and paragraphs should still fit the page width.

Use realistic content: a long title, an unbroken URL, many links and longer translated text. Raise the browser's default font size too. A box that looked roomy with a short sample may overflow when text grows.

Trace horizontal overflow

A sideways scrollbar or an empty strip at the right often means one box exceeds the viewport. Inspect suspicious elements and compare their box edges with the page boundary. Temporarily outline them:

* { outline: 1px solid red; }

An outline shows the boxes without adding to their measured width. Look for fixed widths, 100vw blocks, images without a width limit, wide minimum grid tracks, negative margins, long text and code blocks.

Match the fix to the cause:

CauseUseful repair
Rigid content widthUse a flexible width with max-width or min()
Oversized imageLimit width and use automatic height
Long unbroken URLUse overflow-wrap: anywhere
A code block or necessary wide tableAdd local overflow-x: auto
A grid track with an excessive minimumAllow a smaller track minimum

Do not hide the whole page's horizontal overflow as a shortcut. body { overflow-x: hidden; } can make the scrollbar disappear while leaving content beyond the reader's reach.

For a local scrolling table, test the interaction as well as the width. Use the keyboard to reach the named wrapper and check that its horizontal scroll reveals every column. Then continue tabbing to the next control. The table can require sideways movement without forcing the paragraph above it or the rest of the page into the same wide scrolling area.

Finally inspect image requests in the Network panel. Set both a phone-sized viewport and a realistic high device pixel ratio, reload under the intended test conditions and check the selected candidate and file size. A visually small image can still be a large download if its sizes hint is wrong.

Key Takeaways

  • Sweep through widths and test breakpoint boundaries.
  • Check reflow at 320 CSS pixels and actual zoom behavior.
  • Use real devices to check behaviors emulation approximates.
  • Inspect overflowing boxes and repair the specific cause.
  • Check realistic content and the files images actually download.

5.2 Keep pages usable with touch and reader settings

Width is only one part of responsive behavior. A wide tablet can use touch; a narrow desktop window can use a mouse. Pages also need to work with zoom, larger text, different orientations and the reader's preferred motion and color settings.

Make controls easy to reach

WCAG 2.2 AA target-size guidance calls for at least 24 × 24 CSS pixels or sufficient separation under its spacing rule, with exceptions such as links within sentences. The AAA size criterion uses 44 × 44 CSS pixels, also with exceptions. Larger padded navigation links are often more comfortable than a dense row of tiny labels.

At a 16px root, a link with 16px text, line-height: 1.5 and padding: 0.75rem 1rem has a 24px line box plus 12px above and below: 48px tall. Make the link itself the padded clickable box.

Essential information must not depend on hover. Keep actions visible or reachable by tap and keyboard focus. Add optional hover effects only when the main input supports them:

@media (hover: hover) and (pointer: fine) {
  .card:hover { outline: 2px solid currentColor; }
}

pointer: coarse describes an imprecise primary input such as a finger. any-pointer and any-hover check whether any connected input has a capability; they do not establish that every reader is using it.

Let enlarged text and rotation work

Keep zoom enabled and verify text enlargement to 200% without lost content or function. Use font-relative sizes and growing text boxes. min-height lets a box grow when its content grows; fixed height can let enlarged text spill into the next section.

Support portrait and landscape unless one orientation is essential to the activity. Landscape phones can have very little vertical space. A height query such as @media (max-height: 30em) can let a sticky header scroll away so it does not cover the content.

Respect preferences and output

Use prefers-reduced-motion to avoid large motion for readers who request less. One approach is to put optional animations inside @media (prefers-reduced-motion: no-preference). You can also remove an existing animation inside a reduce query.

For a dark theme, pair readable text and background colors inside @media (prefers-color-scheme: dark). Check contrast in both themes. color-scheme: light dark lets browser-drawn controls and scrollbars follow an appropriate theme; it does not choose all of your page colors for you.

A reduced-motion rule can target only the animated component:

@media (prefers-reduced-motion: reduce) {
  .banner { animation: none; }
}

Keep the message itself available when movement is removed. For a dark theme, change foreground and background together; changing only one can make text blend into its surroundings. Preview form controls too, since browser-drawn controls can differ from your authored surfaces.

Use @media print to hide navigation and buttons and produce dark text on white. Printed links can show their destinations with a[href]::after { content: " (" attr(href) ")"; }. Test a print preview to check that content, backgrounds and page breaks remain readable.

Key Takeaways

  • Size the clickable target, including its padding.
  • Use input features to check hover and pointer precision.
  • Keep essential content available to tap and keyboard users.
  • Support zoom, growing text boxes and both orientations.
  • Respect reduced motion and test both color themes.
  • Provide readable print styles and useful link destinations.

Responsive Web Design examples

5 Walkthroughs

1

Build a flexible event page

13 steps
Set the viewport and choose sizes for an event page that must fit narrow windows and enlarged text.
  1. 1
    IntroductionBuild a flexible event page

    An event page has tiny text on a phone and a content box that extends beyond the screen. Start by separating the browser’s layout width, set by the viewport, from the size of the boxes inside it. Then make the wrapper follow available space and choose text and height units with useful limits, such as clamp, so nothing breaks on narrow windows or enlarged text. You will check each change against a specific cause instead of changing several unrelated sizes at once.

  2. 2
    ExplanationRead the viewport in CSS pixels

    A high-density display does not provide extra CSS layout width. If a phone has 1080 device pixels across at a device pixel ratio of 3, the corresponding width is 360 CSS pixels. A media query and a viewport-width unit use that CSS width. The physical dots provide finer detail for drawing text and images; they do not turn a narrow page into a desktop layout.

  3. 3
    ExampleFix the wide virtual layout

    The event page has no viewport instruction, so the mobile browser can lay it out in a wide virtual viewport and shrink the result. Put this in the document head:

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

    Reload the narrow preview. The page now uses the device width in CSS pixels and starts at normal scale. Leave zoom enabled for readers who need larger text.

  4. 4
    ChallengeExplain the remaining sideways scroll

    After the viewport change, the text looks normal, but the event wrapper is still width: 900px on a 360px viewport. Why does it still scroll sideways?

    ---reveal---

    The tag changed the browser’s starting layout viewport, not the wrapper’s CSS width. A 900px box still exceeds 360px. Make the wrapper fluid with an upper limit so it can shrink to the space the page actually has.

  5. 5
    ExampleGive the wrapper room to shrink

    Replace the wrapper’s fixed width with a full-width box and a cap:

    .event {
      box-sizing: border-box;
      width: 100%;
      max-width: 68rem;
      padding-inline: 1rem;
      margin-inline: auto;
    }
    

    The wrapper follows a narrow parent but stops growing on a wide screen. Border-box includes the padding inside its declared width, while automatic inline margins center the capped box.

  6. 6
    ExplanationChoose a reference for text and spacing

    Leave the root font at the reader’s default. A 1.25rem label becomes 20px with a 16px root and 25px with a 20px root. For a button’s padding, em can be more useful: padding: 0.5em 1em follows the button’s own text size. In the font-size property itself, however, em follows the parent. That difference explains why nested em font rules can compound.

  7. 7
    TipLimit paragraph width with the font

    The event description should not stretch into one very long line on a wide screen. Add max-width: 62ch to its reading area. A ch measures the width of the font’s digit "0", so the result is a useful font-based limit, not a promise of exactly 62 characters per line. Check the real font and text; narrow windows will still make the paragraph wrap before it reaches that cap.

  8. 8
    ChallengeKeep a button’s proportions

    A button’s own font increases from 16px to 24px. Its horizontal padding is 0.75em. What happens to each side’s padding, and why?

    ---reveal---

    It increases from 12px to 18px because em in padding follows the button’s own font size. This lets the surrounding space grow with the label. A padding value in rem would instead follow the root font and would not necessarily change with this local font increase.

  9. 9
    ExplanationFit a tall opening section

    The event’s opening section should fill a phone screen without trapping longer text in a fixed-height box. Use min-height: 100svh. The small viewport represents the state with browser toolbars visible; min-height allows the section to grow beyond that starting size. Plain vh follows the large viewport, while dvh follows toolbar changes and can make the section resize during scrolling.

  10. 10
    ExampleStart a bounded heading size

    For the event title, try:

    .event h1 {
      font-size: clamp(1.5rem, 1rem + 2vw, 2.5rem);
    }
    

    With a 16px root, the limits are 24px and 40px. In a 600px viewport, the preferred value is 16px plus 12px, giving 28px. Because 28px is inside the limits, the browser uses it. This produces a smooth change without a separate query for each size.

  11. 11
    ExampleApply the limits at both ends

    Keep the same heading expression and 16px root. At 300px wide, the preferred value is 16px plus 6px, or 22px, so the 24px floor applies. At 1600px wide, it would be 48px, so the 40px ceiling applies. The middle expression suggests a size; clamp decides whether that suggestion falls inside the allowed range. It does not scale all three arguments together.

  12. 12
    ChallengeChoose units and check enlargement

    The event page’s wrapper fits, but its title uses only 6vw and barely enlarges during desktop zoom. What should you change and then test?

    ---reveal---

    Use a font-relative contribution and sensible limits instead of pure viewport sizing, then test actual text enlargement. Zoom narrows the viewport in CSS pixels, reducing a vw-only font size. Even a mixed rem/vw expression needs a zoom check to confirm that readers can enlarge the text enough.

  13. 13
    RecapCheck the flexible base

    Check three references before adding layout changes: the mobile viewport uses device width, the wrapper follows its parent, and text sizes respect a font reference. Bound fluid values where needed and let text boxes grow. Try a narrow window, a wide window and enlarged text before moving on to additional columns.

2

Turn a card list into an adaptive layout

14 steps
Build from a narrow stack, add useful breakpoints and compare explicit and automatic columns.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionTurn a card list into an adaptive layout
  2. 2
    ExplanationMake a width condition readable
  3. 3
    ExampleCombine conditions with the right meaning
  4. 4
    ChallengeFind a boundary gap
  5. 5
    ExampleWrite the narrow layout first
  6. 6
    ExampleAdd two columns when they fit
  7. 7
    ChallengeKeep the wider change in control
  8. 8
    ExplanationLet grid fit columns automatically
  9. 9
    ExampleCount tracks and gaps together
  10. 10
    Key InsightChoose how empty space behaves
  11. 11
    ChallengeKeep the narrow grid usable
  12. 12
    TipPreserve the reading order
  13. 13
    ChallengeCombine page and component behavior
  14. 14
    RecapCheck each transition
3

Reuse one card in different page slots

10 steps
Set up a local query wrapper and combine container units with page-level conditions.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionReuse one card in different page slots
  2. 2
    ExampleCreate the measuring ancestor
  3. 3
    ExampleChange the card inside the wrapper
  4. 4
    ChallengeRepair a self-query
  5. 5
    TipName the intended measuring box
  6. 6
    ExplanationMake a local heading fluid
  7. 7
    ChallengeFind the surprising reference
  8. 8
    ConnectionKeep page and preference decisions separate
  9. 9
    ChallengeChoose conditions for a reusable card
  10. 10
    RecapInspect the reference box
4

Deliver a clear image without a large download

15 steps
Make an image flexible, predict a resolution candidate and add a crop or format choice where needed.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionDeliver a clear image without a large download
  2. 2
    ExampleKeep the photo inside its column
  3. 3
    ExplanationChoose what may be cropped
  4. 4
    ChallengeSeparate display size from file size
  5. 5
    ExampleOffer real file widths
  6. 6
    ExampleRead the slot before choosing a file
  7. 7
    TipKeep sizes and CSS together
  8. 8
    ChallengeChoose the needed resolution
  9. 9
    ConnectionUse density descriptors for the fixed logo
  10. 10
    ExplanationDecide whether the crop must change
  11. 11
    ExamplePlace the wide crop before the fallback
  12. 12
    ChallengeRepair crop ordering
  13. 13
    ExplanationOffer formats without changing the subject
  14. 14
    ChallengeFinish the workshop image plan
  15. 15
    RecapCheck the box and the request
5

Test a page beyond its tidy preview

12 steps
Trace overflow and test reflow, input capabilities, text size and reader preferences.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionTest a page beyond its tidy preview
  2. 2
    ExampleSweep through the layout widths
  3. 3
    ExplanationCheck a zoomed reading width
  4. 4
    ExampleFind the box that crosses the edge
  5. 5
    ChallengeKeep a necessary wide region reachable
  6. 6
    ExampleMake the link itself large enough
  7. 7
    Key InsightDo not infer input from width
  8. 8
    ChallengeRepair a panel for enlarged text
  9. 9
    ExplanationTest motion, themes and print
  10. 10
    TipInspect downloads under the intended conditions
  11. 11
    ChallengePlan a final usability check
  12. 12
    RecapRepeat the condition that found the fault

Responsive Web Design practice questions

40 Practice Questions in 4 sets

Set 1: Breakpoints, containers and usable controls10 Practice Questions

Question 1 of 10

A viewport can be 679.5px wide during zoom. Which boundary plan assigns every width to exactly one of two width-based layouts?
  1. Use (max-width: 679px) and (min-width: 680px).
  2. Use (width < 680px) and (width >= 680px).
  3. Use (max-width: 680px) and (min-width: 680px).
  4. Use (width <= 680px) and (width > 681px).
Show answerHide answer
Answer: B
The complementary comparisons cover widths below 680px and widths at or above it, with no overlap. The 679px/680px plan leaves 679.5px uncovered. Inclusive maximum and minimum conditions at the same number would both match at that boundary.

Question 2 of 10

A navigation row wraps awkwardly at some widths. What should determine where its layout changes?
  1. The most common phone width in a device preset list.
  2. The width at which all page images finish downloading.
  3. The physical pixel count printed in the screen specifications.
  4. The width at which the actual links need a different arrangement.
Show answerHide answer
Answer: D
A content-based breakpoint comes from testing the actual labels and controls as their available space changes. A device preset can help test a page, but it cannot represent every window width, font preference or longer label the navigation must handle.

Question 3 of 10

A grid uses repeat(auto-fit, minmax(14rem, 1fr)) with a 1rem gap. The root is 16px, there are many items, and the content width is 920px. How many columns fit?
  1. Two columns
  2. Three columns
  3. Four columns
  4. Five columns
Show answerHide answer
Answer: B
Each minimum is 224px and each gap is 16px. Four columns would need 944px, so only three fit in 920px. Dividing 920 by 224 without adding the gaps suggests four, but the gaps are part of the width the grid must accommodate.

Question 4 of 10

A card grid can be placed in a 190px sidebar or a 1000px main area. It uses repeat(auto-fit, minmax(16rem, 1fr)). With a 16px root, the sidebar overflows and two main-area cards should stretch across their row. What track rule meets both needs?
  1. repeat(auto-fit, minmax(min(16rem, 100%), 1fr))
  2. repeat(auto-fill, minmax(16rem, 1fr))
  3. repeat(auto-fit, minmax(16rem, 16rem))
  4. repeat(auto-fill, minmax(min(16rem, 100%), 1fr))
Show answerHide answer
Answer: A
The inner min() lets a single track shrink below 256px to fit the sidebar. Keeping auto-fit collapses empty main-area tracks so two cards can expand. Switching to auto-fill with a flexible minimum fixes the sidebar but retains empty tracks in the wide area.

Question 5 of 10

Which wrapper declaration enables width container queries while allowing height to follow the content in horizontal writing?
  1. container-type: normal
  2. container-type: size
  3. container-type: inline-size
  4. container-name: inline-size
Show answerHide answer
Answer: C
The inline-size type makes the width available for size queries while keeping content-based height. The size type also contains the block dimension, so it is not the right choice when the wrapper’s height should continue to come from its contents.

Question 6 of 10

A card has container-type: inline-size. Inside @container (width >= 24rem), .card changes to two columns, but it never changes. There is no eligible ancestor container, and the card is in a shrinking flex slot. What repair makes the query work reliably?
  1. Keep the type on the card and add a container name to the query.
  2. Put the type on the card and change the query to max-width.
  3. Put the type on a growing wrapper and query the inner card.
  4. Put the type on an inline wrapper whose width comes only from its contents.
Show answerHide answer
Answer: C
The styled card needs an eligible ancestor, so its own container type cannot supply the query. A wrapper whose flex layout gives it width supplies that ancestor and avoids shrink-to-fit collapse. Naming the card does not remove the ancestor requirement.

Question 7 of 10

A heading uses 5cqi inside an eligible 460px-wide container in horizontal writing. The viewport is 1380px wide. What is its computed font size before any other limits?
  1. 23px
  2. 69px
  3. 46px
  4. 13.8px
Show answerHide answer
Answer: A
One cqi is 1% of the eligible container’s inline size, so 5% of 460px gives 23px. Using 69px takes 5% of the viewport instead and misses the component-local reference that makes container units useful.

Question 8 of 10

A 1500 × 1000 image has matching HTML dimension attributes and CSS max-width: 100%; height: auto. Its container is 450px wide. What displayed dimensions preserve its proportions?
  1. 450 × 1000 CSS pixels
  2. 450 × 300 CSS pixels
  3. 1500 × 1000 CSS pixels
  4. 450 × 450 CSS pixels
Show answerHide answer
Answer: B
The width shrinks from 1500px to 450px, a scale of 0.3, so the 1000px height becomes 300px. Keeping the attribute height of 1000px as the displayed height would distort the image; height: auto maintains its aspect ratio.

Question 9 of 10

At 320px, a page contains a readable paragraph with a long URL and a genuinely two-dimensional results table. The URL and table both widen the whole page. What repair preserves access to both?
  1. Hide page-wide horizontal overflow and keep both boxes at their current widths.
  2. Give both the paragraph and table fixed widths of 320px and hide their overflow.
  3. Put the whole page in one horizontal scrolling region and preserve its wide layout.
  4. Allow URL breaks and put the table in a named, keyboard-reachable scrolling wrapper.
Show answerHide answer
Answer: D
The URL can break within the paragraph, while the table can retain its necessary structure in a local scrolling region. That lets surrounding content reflow at 320px. Hiding the body’s overflow removes access to the excess content without fixing either cause.

Question 10 of 10

A large tablet is touch-first and reports hover: none and pointer: coarse. How should a card’s essential details be provided?
  1. Show them only on hover because the tablet viewport is wide.
  2. Show them only when the tablet enters landscape orientation.
  3. Keep them visible or reachable by tap and keyboard focus.
  4. Keep them hidden until an any-pointer: fine query matches.
Show answerHide answer
Answer: C
Essential details must remain available without hover, including through touch and keyboard use. A large viewport does not create a hover-capable primary input. Even a connected precise pointer would not justify making essential content unavailable to someone using touch.
Set 2: Viewport, sizing and layout foundations10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

A screen has 824 device pixels across and a device pixel ratio of 2. What width does CSS use for its layout viewport when the viewport tag requests device width?
  1. 824 CSS pixels
  2. 1648 CSS pixels
  3. 412 CSS pixels
  4. 206 CSS pixels
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

A mobile page appears as a tiny desktop layout. Its head has no viewport meta tag, and its main box has width: 880px. What pair of changes addresses both causes?
  1. Add the device-width viewport tag; give the main box a fluid width.
  2. Add the device-width viewport tag; keep the main box at 880px.
  3. Keep the current viewport; give the main box a fluid width.
  4. Set maximum-scale to 1; give the main box a fluid width.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

A panel has width: 60% in a containing block that is 450px wide. The panel has no padding or border. What is its width?
  1. 180px
  2. 450px
  3. 750px
  4. 270px
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

The root font is 18px. A button inherits a 20px font from its parent, sets font-size: 1.25em, and has padding-inline: 0.8em. What is its horizontal padding on each side?
  1. 16px
  2. 20px
  3. 18px
  4. 14.4px
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

A hero section must be at least as tall as the small mobile viewport and grow if its text needs more height. What declaration fits?
  1. height: 100svh
  2. min-height: 100svh
  3. min-height: 100lvh
  4. height: 100dvh
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

The root font is 18px and the viewport is 900px wide. What font size results from clamp(1rem, 1rem + 1vw, 1.4rem)?
  1. 25.2px
  2. 27px
  3. 18px
  4. 32.4px
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

At a viewport width of exactly 760px, does (min-width: 760px) match?
  1. No, because the width must be greater than 760px.
  2. Only if a max-width condition is also present.
  3. Only if the stylesheet sets the root size in pixels.
  4. Yes, because its minimum boundary is included.
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

A browser has a 20px initial font size. The stylesheet sets html { font-size: 12px; }. At what viewport width does (min-width: 36em) begin matching?
  1. 432px
  2. 576px
  3. 720px
  4. 864px
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A base grid has one column. A 62em query sets three columns, followed later by a 42em query setting two. Both use .items with equal specificity in the same layer. What appears at 70em?
  1. Three columns, because the larger threshold has greater specificity.
  2. One column, because overlapping minimum-width queries cancel out.
  3. Three columns, because a browser sorts queries by threshold value.
  4. Two columns, because both queries match and the later rule wins.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

Four flexible cards wrap into a full row of three and a final row of one. Each has flex: 1 1 14rem. Why can the final card be wider?
  1. The last card inherits a larger root font size.
  2. Flex wrapping preserves empty columns on the last line.
  3. Each flex line distributes its own remaining width.
  4. The last card ignores the container’s declared width.
Show answerHide answer

Save this lesson to see this answer.

Set 3: Image files, crops and display boxes10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

A diagram must fit a fixed rectangular thumbnail without losing any part of its contents. Which object-fit value suits it?
  1. contain
  2. cover
  3. fill
  4. none
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

A page’s images look small on phones but still download huge files. They already have max-width: 100%; height: auto. What additional change addresses the download size?
  1. Reduce the CSS max-width from 100% to 80%.
  2. Remove the HTML width and height attributes.
  3. Offer smaller files through responsive image markup.
  4. Set object-fit to contain on every image.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

An image displays at 340 CSS pixels on a ratio-2 screen. Candidates are 400w, 700w and 1400w. Assume the browser selects the smallest candidate meeting the density target, with no cache or network adjustment. What file width is selected?
  1. 400 pixels
  2. 680 pixels
  3. 1400 pixels
  4. 700 pixels
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

The browser default is 16px. An image has sizes="(min-width: 54em) 40vw, 100vw" and 600w, 1000w and 1800w candidates. At a 1000px viewport and ratio 2, assume the smallest candidate meeting the density target is chosen. Which file width results?
  1. 600 pixels
  2. 1000 pixels
  3. 1800 pixels
  4. 800 pixels
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

CSS gives a photo a 48vw slot, but its width-descriptor markup says sizes="100vw". What is the likely loading problem?
  1. The browser must display the photo at 100vw despite CSS.
  2. The browser must reject every width-descriptor candidate.
  3. The browser may choose an unnecessarily large candidate.
  4. The browser may crop the photo to half its composition.
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

The default is 16px. An image uses sizes="(min-width: 38em) 100vw, (min-width: 66em) 45vw, 100vw". At 1152px and ratio 2, candidates are 640w, 1280w and 2560w. Under the smallest-sufficient selection rule, what happens?
  1. The first condition wins and selects the 2560w candidate.
  2. The larger matching threshold wins and selects the 1280w candidate.
  3. The first condition wins and selects the 1280w candidate.
  4. The final fallback wins and selects the 2560w candidate.
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

Inside a picture element, where should the alternative text be written?
  1. On every source element.
  2. On the picture element itself.
  3. On the first matching source only.
  4. On the required img element.
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

The browser default is 16px. A picture first offers a 34em minimum-width source with 600w/1200w candidates and sizes="50vw", then a 68em source with 1600w/2400w candidates and sizes="100vw", then img. Both formats are supported. At 1152px and ratio 2, what source and file result under smallest-sufficient selection?
  1. The second source and its 2400w file.
  2. The first source and its 1200w file.
  3. The first source and its 600w file.
  4. The second source and its 1600w file.
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A picture offers AVIF, then WebP, then a JPEG img. There are no media conditions. A browser supports WebP and JPEG but not AVIF. What does it select?
  1. The JPEG img src.
  2. The WebP source.
  3. The AVIF source.
  4. Both WebP and JPEG.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

After widening a test window, a large responsive image is cached. Narrowing the window still shows that file in use. What is the sound conclusion?
  1. The browser must have ignored every sizes condition.
  2. A width-descriptor image cannot adapt after window resizing.
  3. The image’s CSS width must still equal the wider viewport.
  4. A cached larger candidate may be reused; retest with cache controlled.
Show answerHide answer

Save this lesson to see this answer.

Set 4: Diagnose responsive pages across conditions10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

A content-box wrapper is width: 100% inside a 370px parent and has 20px left and right padding, with no border. What change makes the wrapper fit the parent while keeping that padding?
  1. Keep content-box sizing because its total width is already 370px.
  2. Use border-box sizing so the wrapper’s total width becomes 410px.
  3. Keep content-box sizing and add automatic inline margins to remove overflow.
  4. Use border-box sizing so the wrapper’s total width is 370px.
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

A sidebar is 240px wide inside a 1440px viewport. Its text panel uses width: 30vw; height: 8rem. When text is enlarged, it overlaps the next panel. Which repair addresses both visible faults?
  1. Keep the viewport-relative width and replace height with min-height.
  2. Use a parent-relative width and keep the fixed 8rem height.
  3. Use a parent-relative width and let the panel’s height grow with content.
  4. Use a smaller viewport-relative width and hide all panel overflow.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

These rules are in one layer with equal specificity. What font size applies at a 900px viewport?

@media (min-width: 700px) { .title { font-size: 2rem; } }
.title { font-size: 1.2rem; }
  1. 1.2rem
  2. 2rem
  3. 3.2rem
  4. The browser’s initial font size.
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

The browser default and root are 16px. At a 928px viewport, a page wrapper is 540px wide. A 52em query gives it an 18rem sidebar, a 1rem gap and a minmax(0, 1fr) main column. The main grid uses repeat(auto-fit, minmax(16rem, 1fr)), and a card URL measures 620px unwrapped, with overflow-wrap: normal. Which two edits let the main content fit without changing the sidebar?
  1. Move the sidebar breakpoint wider and keep the rigid track and URL rules.
  2. Allow a shrinkable track minimum and wrapping inside the URL text.
  3. Hide body overflow and keep the rigid track and URL rules.
  4. Allow a shrinkable track minimum but keep the URL text unbreakable.
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

The root is 16px. A 520px wrapper named panel contains a 280px inline-size container, which contains a card. Both ancestors are eligible. An unnamed 26rem query styles the card. What happens, and how can it deliberately use the outer width?
  1. It matches already; unnamed queries always use the widest ancestor.
  2. It stays false; name the inner container panel and keep its 280px width.
  3. It matches already; 26rem is computed from the card’s 280px width.
  4. It stays false; name the query panel so it measures the 520px ancestor.
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

A reusable product tile appears in both page columns. It should switch image placement according to its own slot, while large animation should respect reduced motion. Which design assigns each decision to the right condition?
  1. Use a viewport width query for placement and a container query for motion.
  2. Use one viewport width query for both placement and reduced motion.
  3. Use a container size query for placement and a motion media query for animation.
  4. Use one container width query for both placement and reduced motion.
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A photo fills a wrapper whose containing block spans a 1440px viewport. The wrapper has width: 100%; max-width: 48rem, with no padding, and the root is 16px. The image hint is sizes="50vw"; ratio is 2 and candidates are 800w, 1500w and 2000w. Under smallest-sufficient selection, what correction and file choice match the real slot?
  1. Keep the 720px slot hint; the real slot needs only the 1500w file.
  2. Use a 768px slot hint at this width; the target needs the 2000w file.
  3. Use a 768px slot hint at this width; the target needs the 1500w file.
  4. Use a 1440px slot hint at this width; the target needs the 2000w file.
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

A wide crop shows two cyclists, but the phone crop shows only one. The img has alt text "Two cyclists crossing a bridge." What should be changed?
  1. Make the crops and the shared alt description agree about the visible subject.
  2. Give the phone source its own alt and leave the img description unchanged.
  3. Move the existing alt text from img onto the picture element.
  4. Remove the required img and let each source display its own alt text.
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A 1440px layout viewport is zoomed to 300%. What is its approximate resulting width in CSS pixels?
  1. 4320 CSS pixels
  2. 1140 CSS pixels
  3. 480 CSS pixels
  4. 720 CSS pixels
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

On a short landscape touch screen, a sticky header covers much of the page and essential menu details appear only on hover. A reader has also requested reduced motion. Which plan addresses all three conditions?
  1. Use one narrow-width condition to shrink the header, add hover and stop motion.
  2. Use height, input and motion conditions for their respective adaptations.
  3. Use a container query to shrink the header and infer hover and motion preferences.
  4. Use height and hover conditions, then enable animation whenever the viewport is wide.
Show answerHide answer

Save this lesson to see this answer.

Stuck on your own question? Homework Help

Key Terms

74 Key Terms, each with its definition

A

Alternative text
Alternative text is the text equivalent on an img element that conveys the image's purpose or content when the image cannot be perceived.
One alt description must remain accurate for every crop that the surrounding picture can select.
Art direction
Art direction selects a different image composition or crop for a display condition so the important subject stays clear at the chosen size.
A phone receives a close crop of a performer while a wide window shows the performer and the stage.
Aspect ratio
An aspect ratio expresses the relationship between width and height, letting a media box keep the same proportions as its dimensions change.
A 3:2 photograph displayed at 300px wide needs 200px of height to keep its original shape.
auto-fill
The auto-fill grid repeat keyword keeps the tracks that fit even when some are empty, preserving unused column positions in the row.
Two cards can stay narrower because auto-fill leaves room reserved for additional empty tracks.
auto-fit
The auto-fit grid repeat keyword creates as many tracks as fit and collapses unused tracks so occupied tracks can share their space.
Two cards can stretch across a wide row when auto-fit collapses the row's empty tracks.

B

Border-box sizing
Border-box sizing includes padding and borders within an element's declared width and height, leaving less room for its content.
A full-width page wrapper uses box-sizing: border-box so its side padding does not cause overflow.
Breakpoint
A breakpoint is a threshold where a conditional rule changes a layout or style because the content needs a different arrangement.
A navigation row gets a breakpoint where its full labels stop fitting comfortably on one line.

C

Cascade
The cascade is the process that resolves competing CSS declarations; conditional rules still participate when their conditions match.
With equally specific matching rules in the same layer, the later font-size declaration wins.
ch
A ch is a length equal to the advance width of the digit "0" in the current font, often used to set a paragraph's line-length limit.
An article capped at 65ch stays readable in a wide window without claiming an exact character count.
clamp()
The clamp() function limits a preferred size between a minimum and a maximum, letting the size change smoothly within those bounds.
A heading combines rem and vw in clamp() so it can grow with the window within useful limits.
Color-scheme preference
A color-scheme preference indicates the reader's preferred light or dark appearance, which a page can use to select readable theme colors.
A dark-theme query pairs a dark background with readable light text and checks the resulting contrast.
contain
The contain value of object-fit keeps the entire image visible inside a sized box while preserving its proportions, leaving empty space if needed.
A diagram uses contain so all its labels stay visible even when its shape differs from the thumbnail.
Container name
A container name identifies an ancestor container so a query can target it instead of the nearest eligible unnamed reference.
@container sidebar (width >= 30rem) looks for an eligible ancestor named sidebar.
Container query unit
A container query unit is a length based on one percent of an eligible ancestor container's size in a particular dimension.
A heading with 5cqi grows when its own card wrapper widens, even if the viewport stays unchanged.
Container size query
A container size query applies styles to descendants when a suitable ancestor container meets a condition about its dimensions.
Two cards on the same page can use different arrangements because their wrappers have different widths.
Content-based breakpoint
A content-based breakpoint is chosen from the behavior of actual text and controls, instead of a preset width associated with a device.
Testing a long page title reveals that the sidebar should move below it sooner than expected.
cover
The cover value of object-fit fills a sized image box while preserving source proportions, cropping the parts that extend outside the box.
A course photo fills a 4:3 thumbnail with cover, so some content near its edges can disappear.
cqb
A cqb is one percent of the block size of an eligible ancestor container, equivalent to height in horizontal writing.
A block-size unit needs a container eligible in that direction, not merely an inline-size container.
cqi
A cqi is one percent of the inline size of the nearest eligible ancestor container, equivalent to its width in horizontal writing.
Inside a 500px-wide eligible container, 4cqi gives a preferred font size of 20px.
cqmin and cqmax
The cqmin and cqmax units use one percent of the smaller and larger eligible container dimensions, respectively, for proportional sizing.
A cqmin length follows the smaller reference dimension when the two dimensions differ.
CSS pixel
A CSS pixel is a reference length used for layout; on a high-density screen it can correspond to several physical device pixels.
A ratio-3 phone with a 390px CSS viewport has 1170 device pixels across its screen.

D

Density descriptor
A density descriptor states the pixel-density scale of a candidate using an x suffix, typically for an image with a fixed CSS display size.
A logo displayed 180px wide can offer a 360-pixel candidate with a 2x density descriptor.
Desktop-first CSS
Desktop-first CSS begins with wide-screen base styles and uses maximum-width queries to change or undo them as the viewport narrows.
A desktop-first row switches to a vertical stack inside a max-width query for smaller windows.
Device pixel ratio
Device pixel ratio is the number of device pixels per CSS pixel, relating screen density to the page's layout reference unit.
A photo shown 250 CSS pixels wide can use about 500 source pixels on a ratio-2 display.
dvh
A dvh is one percent of the dynamic viewport height, which follows changes in the current visible area as browser toolbars move.
A panel using 100dvh can resize while a phone's toolbar appears or disappears during scrolling.

E

em
An em is a font-relative length based on the parent font for font-size and the element's own font for other properties.
A button with 1em horizontal padding gains more space around its label when its own font grows.

F

Flex wrapping
Flex wrapping lets a flex container place items on additional lines when their sizes and gaps no longer fit on the current line.
Navigation links with flex-wrap: wrap can form a second row instead of extending past the page.

H

Horizontal overflow
Horizontal overflow occurs when content extends beyond the width available to its box, potentially creating sideways scrolling or hidden content.
A fixed 700px panel overflows a 360px page even when the viewport meta tag is present.
Hover capability
Hover capability describes whether the primary pointing input can conveniently stay over an element without activating it.
Essential details remain visible on touch devices, while optional hover styling runs inside a capability query.

I

Inline size
Inline size is an element's dimension along the direction that text runs within a line, corresponding to width in horizontal writing.
A horizontal card's inline-size query responds to width while its height can still follow its text.
Intrinsic image size
An image's intrinsic size is the size information supplied by the image resource, used when CSS and HTML do not specify another display size.
A large image can overflow a narrow article unless CSS limits its width to the available space.
Intrinsic layout
An intrinsic layout adapts through its own sizing and wrapping rules, allowing content to fit available space without explicit breakpoints.
A wrapping card row gains another line as the container narrows without any media query.

L

Layout shift
A layout shift is a visible movement of page content after its initial position changes, such as when an image loads without reserved space.
Image width and height attributes let the browser reserve a ratio and reduce text jumping during loading.
Lazy loading
Lazy loading delays fetching an image until it approaches the visible area, reducing early downloads for content lower on a page.
A gallery image below the opening screen can use loading="lazy", while the main opening photo loads promptly.
Logical operator
A logical operator combines or negates conditions so a query can require several facts or accept alternative matching conditions.
screen and (min-width: 40em) requires both screen output and a sufficiently wide viewport.
lvh
An lvh is one percent of the large viewport height, representing the layout space available when retractable browser toolbars are hidden.
A section set to 100lvh can extend behind mobile toolbars while those toolbars are still visible.

M

max()
The max() function selects the largest supplied value, so it can place a lower limit on a length while another value changes.
A gap set to max(1rem, 2vw) cannot become smaller than 1rem as the viewport narrows.
Media query
A media query is a condition that controls when CSS applies, based on viewport features, output type, input capabilities or preferences.
A min-width query adds a second content column when enough window space becomes available.
Media type
A media type identifies the broad kind of output, such as screen or print, to which a stylesheet or conditional CSS rule applies.
An @media print block removes navigation controls that a reader cannot use on paper.
min-width condition
A min-width condition matches when the viewport is at least as wide as its stated threshold, including the threshold value itself.
The condition (min-width: 620px) is true at 620px and remains true at wider viewport widths.
min()
The min() function selects the smallest supplied value, so it can place an upper limit on a size that otherwise grows with available space.
A box with width: min(100%, 36rem) fills narrow containers but stops growing at 36rem.
minmax()
The minmax() grid function gives a track a minimum and maximum size, letting the track grow or shrink within the declared range.
A track using minmax(15rem, 1fr) cannot shrink below 15rem even if its container is narrower.
Mobile-first CSS
Mobile-first CSS uses ordinary base rules for narrow layouts and adds wider arrangements through minimum-width conditions as space grows.
A card list starts as one column and gains extra columns through increasing min-width queries.

N

Network panel
The Network panel is a developer tool that lists resource requests and their transfer details, helping check which image files a page downloads.
A phone-sized test can reveal a large image download caused by a sizes hint that overstates the slot.

O

Object fitting
Object fitting controls how an image's contents fit inside a sized image box, allowing proportional cropping, empty bands or stretching.
object-fit: contain preserves every part of a diagram inside a uniform thumbnail box.
object-position
The object-position property sets the alignment of image contents inside their box, controlling which area stays visible when fitting crops them.
A thumbnail uses object-position: top to keep the upper part of its source visible when cropped.
Orientation feature
The orientation feature compares the viewport's width and height; portrait includes a square viewport and landscape means width is greater.
A landscape query can match a wide desktop window as well as a phone held sideways.

P

Percentage width
A percentage width gives an element a share of its containing block's width, allowing that element to follow changes in available space.
A box set to 50% is 240px wide when its containing block provides 480px of width.
picture
The picture element groups ordered source choices with one required img, supporting art direction and alternative formats with a fallback.
A picture can offer AVIF and WebP sources before the JPEG file provided by its final img.
Pointer precision
Pointer precision describes how accurately the primary pointing input can target an area, reported as fine, coarse or none in media queries.
A touch-first tablet can report pointer: coarse even when its viewport is wide.
Print stylesheet
A print stylesheet applies CSS for printed output, keeping useful content readable while adapting navigation, colors and link presentation.
A print rule hides menu buttons and can add each link's destination after its label.

Q

Query container
A query container is an ancestor declared as eligible for the size features a container rule needs to measure for its descendants.
A wrapper with container-type: inline-size can supply width information to a card inside it.

R

Range syntax
Range syntax expresses media or container size conditions with comparison operators, making included and excluded boundaries explicit.
The condition (600px <= width < 900px) includes 600px but excludes 900px.
Reduced-motion preference
A reduced-motion preference is a system setting indicating that the reader wants less animation, exposed through a CSS media feature.
Large optional movement can run only inside prefers-reduced-motion: no-preference.
Reflow
Reflow is the rearrangement of content to fit available space; WCAG AA checks ordinary vertical content at a width of 320 CSS pixels.
A paragraph should wrap into narrower lines at 400% zoom without requiring sideways page scrolling.
Relative unit
A relative unit gets its computed size from another measurement, such as a container dimension, a font size or the viewport.
Using rem for text makes its size follow the root font instead of a fixed pixel value.
rem
A rem is a length equal to the root element's font size, allowing sizes to share one reference without compounding through nesting.
With a 20px root font, a heading set to 1.5rem is 30px before any zoom enlargement.
Resolution switching
Resolution switching offers versions of the same image at different pixel sizes so a browser can choose useful detail for the display.
A ratio-2 screen can receive a larger file without changing the photo's crop or its CSS box.
Responsive emulation
Responsive emulation is a browser tool that approximates device conditions such as viewport size, screen density and touch input for testing.
Dragging the emulated viewport reveals widths where a card title extends beyond its container.
Responsive web design
Responsive web design adapts one page's layout, text and images to the available space across different windows and devices.
A course page stacks its cards on a narrow phone and places them in columns when the window has room.

S

Size containment
Size containment prevents contents from determining a container's size in the contained dimensions, avoiding circular sizing dependencies.
A container-type: size box needs independent height sizing because its text no longer supplies that height.
sizes
The sizes attribute describes an image's intended display-slot width under ordered media conditions when srcset uses width descriptors.
sizes="(min-width: 60em) 50vw, 100vw" describes a half-window slot on wider viewports.
Small-viewport fallback
The small-viewport fallback supplies container-unit measurements when no ancestor is eligible for the dimension that the unit needs.
A missing container-type can leave cqi sizing tied to the window instead of the card wrapper.
source element
A source element inside picture offers image candidates and optional media or format conditions that determine whether it can be selected.
A source using (min-width: 55em) can supply the wide crop before a narrow img fallback.
Source order
Source order is the sequence of elements in HTML, which supplies reading and keyboard focus order even when CSS changes visual placement.
A link moved visually to the top can still be reached last by Tab if it remains last in the HTML.
srcset
The srcset attribute supplies image candidates with width or density descriptors, giving the browser alternatives to a single source file.
A photo offers 480w, 800w and 1600w candidates that all show the same composition.
svh
An svh is one percent of the small viewport height, representing the mobile layout space with browser toolbars visible.
An opening section uses min-height: 100svh for a stable starting height while allowing content growth.

T

Text enlargement
Text enlargement increases readable text size while preserving content and controls; WCAG requires support up to 200% without loss.
A panel with growing height can keep its full explanation visible when a reader doubles the text size.
Touch target
A touch target is the clickable or tappable area of a control, including its active padding, which needs enough size or separation for use.
Padding the link itself creates a larger target than adding nonclickable space around its text.

V

Viewport
The viewport is the browser area in which the page is displayed, excluding the browser's own toolbars and surrounding controls.
Rotating a phone changes the viewport dimensions and can trigger different layout rules.
Viewport meta tag
The viewport meta tag gives mobile browsers layout-width and initial-scale instructions so they can display a page at a useful size.
The tag belongs inside the document's <head> and requests width=device-width, initial-scale=1.
Viewport unit
A viewport unit is a length based on a percentage of a viewport dimension, independent of the element's containing block.
A sidebar child set to 50vw still uses half the window width, even when that exceeds the sidebar.
vw
A vw is one percent of the viewport width, making a length track the window's width as that width changes in CSS pixels.
At a viewport width of 900px, 10vw produces a computed length of 90px.

W

Width descriptor
A width descriptor states an image candidate's actual source-pixel width with a w suffix; it does not set the displayed width in CSS.
The descriptor 900w is correct only when the supplied candidate file is actually 900 pixels wide.

About this lesson

By StudeliReport a mistake

Make your own Study Materials

Turn your notes, slides and documents into Study Guides, Practice Questions, Flashcards and 6 more tools.

Get Started