HTML / HTML Images and Multimedia

Responsive Images and Videos

This tutorial will guide you on how to make images and videos responsive, ensuring they look good on all devices. You'll learn techniques to make your multimedia content adapt to …

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Introduces the inclusion of images, audio, and video in HTML.

1. Introduction

Brief explanation of the tutorial's goal

The goal of this tutorial is to help you understand how to make images and videos responsive on your websites, ensuring they look good and function well on all devices (desktops, tablets, and mobile phones).

What the user will learn

By the end of the tutorial, you will learn how to:
- Use CSS techniques to make images and videos responsive.
- Use HTML's <picture> and <source> elements for responsive images.
- Set video width and height properties.

Prerequisites (if any)

You need to have a basic understanding of HTML and CSS. Familiarity with web design principles would be a plus but is not required.

2. Step-by-Step Guide

Responsive design is a web design approach that makes your web content adapt to the different screen and window sizes of a variety of devices.

Images

To make an image responsive, we will use CSS. The key is to set the max-width of the image to 100%. This way the image will scale down if it has to, but never scale up to be larger than its original size.

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

Videos

Videos can be made responsive in a similar way to images. By default, videos don't resize like images when the browser window is resized. We will use a container and then give it a relative position, with the video inside it an absolute position.

.video-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%; 
}

.video-container video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

The padding-bottom property for the .video-container is derived from the aspect ratio of the videos (16:9), which is about 56.25%.

3. Code Examples

Example 1: Responsive Image

Here is an example of a responsive image:

<!DOCTYPE html>
<html>
<head>
<style>
img {
  max-width: 100%;
  height: auto;
}
</style>
</head>
<body>

<h2>Responsive Image</h2>
<img src="img_chania.jpg" alt="Flowers in Chania">

</body>
</html>

Example 2: Responsive Video

Here is an example of a responsive video:

<!DOCTYPE html>
<html>
<head>
<style>
.video-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%;
}

.video-container video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>
</head>
<body>

<div class="video-container">
  <video controls>
    <source src="movie.mp4" type="video/mp4">
    Your browser does not support the video tag.
  </video> 
</div>

</body>
</html>

4. Summary

In this tutorial, you have learned how to make images and videos responsive using CSS. You've seen how to use the max-width property for images and position, width, and height properties for videos to ensure they adapt to different screen sizes. For further learning, you could explore other aspects of responsive design, such as responsive typography or responsive grids.

5. Practice Exercises

  1. Create a webpage with an image and video, both of which should be responsive.
  2. Modify the aspect ratio of the video in the responsive video example to 4:3, adjust the padding-bottom value accordingly.

Solutions

  1. You can use the code examples provided in the tutorial as a starting point. Ensure to replace the src attribute with your own image and video URLs.
  2. For a 4:3 aspect ratio, the padding-bottom should be 75% (3/4 = 0.75 or 75%).

Remember, practice is key to mastering any concept. Try creating your own images and videos and make them responsive. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help