Learning path · Level 3 · Tutorial 4

Size Images and Video for Responsive Layouts

Make your own local images and video fit responsive containers, preserving proportions and choosing deliberately when media should be cropped.

  • Level: Beginner
  • Duration: 15 min
  • 6 steps
Start tutorial Back to the path
Size Images and Video for Responsive Layouts

What you will go through

  1. Keep Images Inside Their Containers Constrain a local image with proportional sizing so it remains inside a fluid page container without distortion. 2 min
  2. Add a Local Video with Native Controls Embed a learner-owned local video with native browser controls and reuse proportional sizing so it fits the responsive gallery. 2 min
  3. Choose a Responsive Media Frame Create an empty, fluid 16:9 media frame and verify that its height follows its width. 2 min
  4. Choose Between Contain and Cover Choose between contain and cover inside an intentional media frame while avoiding distortion and protecting meaningful image or video content. 3 min
  5. Check Video at Narrow Widths Check a narrow video layout for both proportional visual fit and usable native playback controls. 2 min
  6. Build and Review Your Responsive Gallery Finish Local Media Gallery with a naturally proportioned image, a framed local video, and a practical narrow-layout review. 3 min

What you will learn

  • Reference a local video file and expose its native playback controls.
  • Constrain an image or video to its container while preserving its intrinsic proportions.
  • Create an intentionally proportioned media box with aspect-ratio.
  • Choose contain or cover based on whether the complete media must remain visible.
  • Check narrow layouts for distortion, loss of meaningful content, and unusable playback controls.

Before you start

  • Add Images with Appropriate Alt Text
  • Link Local Pages with Relative Paths
  • Control Element Size and Spacing
  • Adapt Layouts with Fluid Sizing and Media Queries

Step 1 of 6

Keep Images Inside Their Containers

Constrain a local image with proportional sizing so it remains inside a fluid page container without distortion.

See the sizing problem

Intrinsic dimensions and overflow

Every image has intrinsic dimensions: its source width and height. Their relationship creates its intrinsic proportions. For example, an illustrative source that is 1200 × 800 pixels has a 3:2 shape.

A fluid .page can become narrower than a large image. Without a sizing limit, the image may extend past its container. Proportional scaling reduces width and height together, so visual shapes keep their intended appearance. Changing width and height independently can cause distortion.

A constrained image keeps its shape

Diagram comparing a wide image overflowing a narrow page container with the same image scaled down proportionally inside an equal-width container.

An illustrative 1200 × 800 pixel source can shrink inside a narrower .page while retaining its intrinsic proportions.

Build the starter page

Set up your local practice

Create a local folder for Local Media Gallery. Put an image in a media folder. This starter example uses media/photo.jpg; if your image has another real name or extension, change the src to match it. Replace the sample alternative text with an accurate description of your chosen image.

Save both complete files below in the same folder. The .page rule uses the fluid, bounded page-container pattern you already know.

index.html

Save as index.html.

html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Local Media Gallery</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <main class="page">
      <h1>Local Media Gallery</h1>
      <img class="media-natural" src="media/photo.jpg" alt="A red bicycle leaning against a brick wall">
    </main>
  </body>
</html>

styles.css

Save as styles.css. Then open index.html in a browser. The expected render below describes the page when both files are saved and the image path is valid.

css
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
}

.page {
  width: 100%;
  max-width: 48rem;
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
}

.media-natural {
  display: block;
  max-width: 100%;
  height: auto;
}
Expected output
Browser render:
A centered, bounded page area contains the heading “Local Media Gallery” and the chosen photo below it.

At a wide browser width, the page stops growing at 48rem. At a narrower width, the page and photo become narrower together. A large photo stays within the page content area and keeps its original shape; a smaller photo remains at its natural displayed width.

Your chosen image and its natural displayed width will differ. A smaller source image is not enlarged by this rule.

Apply proportional sizing

Complete the image rule

Complete the proportional-sizing rule. Then enter the completed declarations in your local styles.css file.

css
.media-natural {
  
  
  
}

Test at a narrow width

Check your local image

Ensure your local .media-natural rule is complete, save both files, and open index.html. First view the page wide, then gradually make the browser narrower.

In 2–4 sentences, name the three declarations you used, report whether the image shrank or overflowed, and decide whether the result was proportional scaling or distortion. Explain what happened to your image if it began narrower than .page.

Write at least 120 characters (0/120).

Step 2 of 6

Add a Local Video with Native Controls

Embed a learner-owned local video with native browser controls and reuse proportional sizing so it fits the responsive gallery.

Reference the file and expose controls

A local video player

A <video> element can play a learner-owned, browser-playable local file through its src attribute. The boolean controls attribute asks the browser to show its built-in playback interface—no JavaScript is needed.

For this sizing practice, choose a short clip without essential audio information. Native controls can look different across browsers; they do not need to match an illustration exactly.

Video inside the gallery container

Diagram of a photo and a video player stacked inside a narrow centered page container, with native controls along the bottom of the video.

The browser supplies native controls. Their exact appearance varies by browser.

Apply the existing sizing rule

Add the video markup

In index.html, add this video below the existing image in Local Media Gallery. The example uses media/clip.mp4.

Put your own browser-playable video in the media folder. If its real filename or extension differs, change the src value to match it. Do not rename an incompatible file just to match this example.

index.html

Add below the existing image in index.html.

html
<video class="media-natural" src="media/clip.mp4" controls>
  Your browser cannot play this video.
</video>
Expected output
Local Media Gallery

[Your chosen image, displayed no wider than the centered page container]

[Your video, displayed below the image at its intrinsic proportions]
[Native browser playback controls appear along the bottom of the video player]

The chosen image, video poster frame, intrinsic dimensions, and native control design vary by your files and browser. The important result is that both media elements remain inside `.page`, and the video keeps its intrinsic proportions as the page narrows.

Reuse proportional sizing

The existing .media-natural class provides the same proportional scaling for this video that it provided for the image: it limits the displayed width to the container and preserves intrinsic proportions with automatic height.

Do not add a separate forced height. If the player does not load or play, first check the relative path and whether the browser can play that file; CSS sizing cannot repair either problem.

Tip

Remember

Save index.html and refresh the browser. max-width: 100%; is a limit, not a command to enlarge a smaller video. A smaller source can remain narrower than .page.

Check the two essential attributes

Complete the player markup

Complete the sample video element for the gallery.

html
<video class="media-natural" src="" >
  Your browser cannot play this video.
</video>

Practice and verify

Test your local gallery

Add the completed markup to your local index.html. If your clip has a different real path, update only the src value to match it. Save, refresh, start playback, and then narrow the browser window.

Check that the player stays inside .page and changes size without distortion. A failed load or failed playback points to the file path or browser compatibility before it points to the proportional-sizing rule.

Verify your own clip

After testing your own gallery, report the relative path you used and what happened when you played the clip and narrowed the browser window. State whether native controls appeared and whether the video stayed within its container with proportional scaling.

Write at least 80 characters (0/80).

Step 3 of 6

Choose a Responsive Media Frame

Create an empty, fluid 16:9 media frame and verify that its height follows its width.

Separate the source from the frame

Source proportions and frame proportions

A source’s intrinsic dimensions describe the proportions built into an image or video. A media frame is different: it is a layout box whose shape you choose for the page.

For Local Media Gallery, create an empty 16:9 media frame before placing media inside it. This isolates the frame’s size from a source’s intrinsic proportions. The frame reserves a deliberate area in the layout, but it does not yet decide how differently shaped media will appear inside.

A chosen frame can differ from the source

Diagram comparing a 3:2 source rectangle with wide and narrow empty 16:9 media frames.

A source can have one intrinsic proportion while the page uses a different, intentional media frame.

Create a fluid outlined frame

Add an empty frame

Inside the existing <main class="page"> on your Local Media Gallery page, add this empty frame below the video. It has no image or video child yet, so its outline makes the frame-sizing behavior visible.

index.html

Add below the existing video in the .page container.

html
<section>
  <h2>16:9 frame test</h2>
  <div class="media-frame"></div>
</section>

styles.css

Add this rule to styles.css. The border and background only make the empty media frame visible while you test its sizing.

css
.media-frame {
  box-sizing: border-box;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 2px dashed #52657a;
  background: #eef3f7;
}
Expected output
Wide rendering, with 640px available inside .page:
16:9 frame test
[ empty dashed media frame: 640px wide × 360px high ]

Narrow rendering, with 320px available inside .page:
16:9 frame test
[ empty dashed media frame: 320px wide × 180px high ]

These illustrative dimensions occur when the available content width is 640px or 320px. Your actual frame size depends on the available width inside `.page`, but its 16:9 shape remains the same.

Tip

Check the outlined result

width: 100%; makes the media frame fluid within .page. Because there is no fixed height, aspect-ratio lets its height follow its width.

Do not add a definite height here. Setting both width and height to definite values can override the preferred ratio. The outlined result confirms frame sizing only; it is not yet a complete media-fitting solution.

Set the intended ratio

Complete the frame rule

Complete the aspect-ratio declaration for the outlined media frame.

css
.media-frame {
  box-sizing: border-box;
  width: 100%;
  aspect-ratio:  / ;
  border: 2px dashed #52657a;
  background: #eef3f7;
}

Check proportional resizing

Verify the resized frame

After saving your CSS, narrow the browser until the outlined .media-frame is 320px wide. Which displayed size preserves 16:9 proportions?

Step 4 of 6

Choose Between Contain and Cover

Choose between contain and cover inside an intentional media frame while avoiding distortion and protecting meaningful image or video content.

Fit the content, not the frame

One frame, three possible results

A .media-frame already provides a fluid 16:9 box. Give an image or video inside it the .media-fill class so the media element occupies that box:

  • width: 100% and height: 100% size the media element to the media frame.
  • object-fit decides how its visual content fits inside that element.

This differs from .media-natural. A media frame has an intentional shape, so a source with different intrinsic proportions needs unused space or intentional cropping to avoid distortion.

Compare fitting outcomes

The same illustrative 1200 × 800 pixels landscape image in three 16:9 media frames: fully visible with unused side space, cropped at the top and bottom to fill the frame, and stretched until a circular object becomes oval.

Contain and cover preserve intrinsic proportions. Contain can leave unused space; cover can crop edges; forced stretching causes distortion.

Add and test a fitting choice

Add the fitting rules

Add one fitting class with .media-fill; do not combine .media-natural and .media-fill on the same media element. object-fit changes how content is drawn inside the media element. It does not resize the surrounding .media-frame.

Add these rules below your existing .media-frame rule in styles.css. The default centered alignment is used in both fitting modes.

Fitting rules for the media child

Add these rules to styles.css.

css
.media-fill {
  display: block;
  width: 100%;
  height: 100%;
}

.fit-contain {
  object-fit: contain;
}

.fit-cover {
  object-fit: cover;
}
Expected output
Browser result: No visible change occurs yet if the media frame is still empty. The rules become visible after they are applied to media inside a .media-frame.

Saving these CSS rules alone produces no visible fitting change until an image or video uses `media-fill` together with one fitting class.

Put your local video in the frame

In index.html, replace the empty <div class="media-frame"></div> from the 16:9 frame test with this complete media frame. It uses your existing local video path; update the path only if your real file differs.

html
<div class="media-frame">
  <video class="media-fill fit-contain" src="media/clip.mp4" controls>
    Your browser cannot play this video.
  </video>
</div>
Expected output
Browser result: The video occupies the fluid 16:9 media frame and keeps proportional scaling. With fit-contain, the complete video remains visible; unused space can appear inside the frame when its intrinsic proportions differ. Native playback controls are available.

The exact unused space and the appearance of native controls vary with your video’s intrinsic dimensions and browser. A valid local video path is required.

Test both modes over time

Save, refresh, and play your local clip with .fit-contain. Then change only the fitting class to .fit-cover, refresh, and play the relevant sequence again.

With contain, all of the source remains visible, though unused space may appear. With cover, the media frame fills, but content near an edge can be lost. Keep contain when every detail or action matters. Keep cover only when the visible crop still preserves the intended meaning. Restore the class that fits your own clip.

A crop can change during playback

Watch why a crop that looks safe at the beginning can remove meaningful action later in a video sequence.

For a source wider than a 16:9 media frame, contain leaves unused space above and below, while cover crops the left and right edges. Judge video crops across the relevant sequence.

Practice the CSS choice

Complete the fitting rules

Complete the two fitting rules in styles.css.

css
.fit-contain {
  object-fit: ;
}

.fit-cover {
  object-fit: ;
}

Apply the fitting decision

Choose by content meaning

Match each content requirement to the appropriate fitting result.

Tap an item and then its matching pair.

Step 5 of 6

Check Video at Narrow Widths

Check a narrow video layout for both proportional visual fit and usable native playback controls.

Separate visual fit from playback usability

Picture fit is not control usability

A video can have a correct media frame and still be hard to operate. Treat these as separate checks:

  • Visual fit: proportional scaling prevents distortion, and your contain or cover choice keeps meaningful content visible.
  • Playback usability: the native play/pause button, seek control, and any other available controls need enough usable space.

object-fit changes how image or video content is drawn inside its box. It does not enlarge, move, or rearrange the browser’s native controls. Control designs and available features can differ between browsers.

Compare the two checks

A comparison of a spacious video player and a narrow video player; both pictures keep their proportions, but only the wider player has comfortably usable controls.

Proportional scaling can succeed while the native playback interface becomes cramped.

Warning

Avoid false fixes

Do not solve a cramped player by removing controls or hiding overflow. Those changes can conceal the problem while making playback less usable.

Run a focused narrow check

Inspect your narrow layout

Open your local Local Media Gallery and make the browser window narrow enough to resemble a small viewport. Perform this focused check on the video:

  1. Confirm that the picture still has proportional scaling—no stretched faces, circles, or objects.
  2. If you use cover, confirm that the meaningful action stays visible throughout the relevant sequence.
  3. Check that the page has no horizontal overflow.
  4. Use play/pause and seek where your browser exposes it.
  5. Put focus on the player, then use Tab to check access to the controls your browser makes available.

This is a focused media check, not a full device or zoom test.

Tip

Choose space, not overflow

If the picture fits but interaction is impractical, give the player more of the available layout width. Use an existing responsive layout technique: for example, let the player occupy a single narrow-layout column or span the available width. Keep the page fluid; do not force a wider page just to enlarge the player.

Decide how to correct a cramped player

Select the usable correction

At a narrow width, a video keeps its proportions but its native controls are too cramped to use comfortably. Which correction is most appropriate?

Apply the focused check

Report your narrow-layout check

Narrow your local gallery and operate its native player. In 3–5 sentences, report whether the media remains proportional, whether important content stays visible, whether the available controls are usable, and whether the page gains horizontal overflow. Then name the correction you would choose if the player is cramped.

Write at least 140 characters (0/140).

Step 6 of 6

Build and Review Your Responsive Gallery

Finish Local Media Gallery with a naturally proportioned image, a framed local video, and a practical narrow-layout review.

Plan the final gallery

One page, two sizing decisions

You will now combine the techniques from this tutorial into Local Media Gallery. The finished page has two deliberate modes:

  • A photo that uses proportional scaling and keeps its intrinsic proportions.
  • A video inside a 16:9 media frame. The frame has an intentional shape; the video uses a fitting mode to preserve its proportions.

Your goal is not to make every box look full. First decide whether a source needs its natural shape or a deliberate frame. Then prevent distortion, assess any intentional cropping, and check the player at a narrow width.

See the intended layout behavior

Diagram comparing wide and narrow gallery views with a proportionally scaled photo and a complete video contained inside a 16:9 media frame.

This is an illustrative expected render. Your chosen media and your browser's native controls will not look identical, but the proportional scaling and frame behavior should match.

Assemble the gallery

Create the final local page

In your local folder, keep index.html, styles.css, and the media folder. Put your own browser-playable image and video in that folder.

This complete example uses media/photo.jpg and media/clip.mp4. Change each path and extension if your real files differ. Replace the sample image alternative text with an accurate description of your chosen image.

Build the page

index.html — replace the file contents with this complete page.

html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Local Media Gallery</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <main class="page">
      <h1>Local Media Gallery</h1>

      <section aria-labelledby="photo-heading">
        <h2 id="photo-heading">Natural photo</h2>
        <img class="media-natural" src="media/photo.jpg" alt="A red bicycle leaning against a brick wall">
      </section>

      <section aria-labelledby="video-heading">
        <h2 id="video-heading">Framed video</h2>
        <div class="media-frame">
          <video class="media-fill fit-contain" src="media/clip.mp4" controls>
            Your browser cannot play this video.
          </video>
        </div>
      </section>
    </main>
  </body>
</html>
Expected output
Browser render: a Local Media Gallery heading, a naturally proportioned photo, then a 16:9 framed video with native playback controls.

The exact photo, video image, and native control design depend on your local files and browser. The illustrated expected layout is shown below.

Add the sizing rules

styles.css — replace the file contents with these sizing rules.

css
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
}

.page {
  width: 100%;
  max-width: 48rem;
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
}

section + section {
  margin-top: 2rem;
}

.media-natural {
  display: block;
  max-width: 100%;
  height: auto;
}

.media-frame {
  width: 100%;
  aspect-ratio: 16 / 9;
  background-color: #e8e8e8;
}

.media-fill {
  display: block;
  width: 100%;
  height: 100%;
}

.fit-contain {
  object-fit: contain;
}

.fit-cover {
  object-fit: cover;
}
Expected output
Wide view: the photo keeps its intrinsic proportions, and the video sits inside a 16:9 media frame. Narrow view: both areas become narrower without unintended media overflow or distortion.

Together with index.html, these rules produce the wide and narrow behavior illustrated below. A contained video may show unused space when its intrinsic proportions differ from 16:9.

Expected render at two widths

Expected wide and narrow rendering of a natural photo above a fully visible video inside a 16:9 frame.

After saving both files, compare your page with this behavior. The media itself, unused space, and browser-native controls vary with your files and browser.

Verify the framed video

Check the essential declarations

Complete the framed-video markup used in the final gallery.

html
<div class="media-frame">
  <video class="" src="media/clip.mp4" >
    Your browser cannot play this video.
  </video>
</div>

Review at wide and narrow widths

Summary

Responsive media decision sequence

  • Use .media-natural when the source should retain its intrinsic proportions as it scales.
  • Use .media-frame when the layout needs a deliberate 16:9 shape, then preserve media proportions with object-fit.
  • Choose contain when all content must remain visible; use cover only when intentional cropping keeps the meaning intact throughout a video sequence.
  • At narrow widths, inspect unintended overflow, distortion, meaningful visible content, and native playback-control usability.

Run the final responsive review

Run the completed local page at a wide width, then make the browser window narrow enough to test the gallery and play the relevant part of your clip.

In 3–6 sentences, report:

  1. Whether your framed video uses contain or cover, and why that choice preserves the content that matters.
  2. What happened to the photo and video proportions at narrow width.
  3. Whether there was unintended horizontal overflow.
  4. Whether the visible native controls remained practical to use.

If essential video action is cropped or the controls are impractical, revise the fitting choice or give the video more available layout width using techniques you already learned.

Write at least 120 characters (0/120).

Gallery complete

Congratulations! You completed: Size Images and Video for Responsive Layouts

You finished a local responsive gallery with proportional media, a deliberate media frame, and a narrow-layout playback check.

Download the App now to have access to + 5000 free courses, exercises, certificates and lots of content without paying anything!

  • 100% free online courses from start to finish

    Thousands of online courses in video, ebooks and audiobooks.

  • More than 60 thousand free exercises

    To test your knowledge during online courses

  • Valid free Digital Certificate with QR Code

    Generated directly from your cell phone's photo gallery and sent to your email

Cursa app on the ebook screen, the video course screen and the course exercises screen, plus the course completion certificate