
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Responsive Image with CSS
To create a responsive image, first set an image using the <img> element. Use the width and max-width properties to set the same image to a responsive image.
Set an image
To set an image on a web page, use the <img> element. The link of the image is included using the src attribute of the <img> element −
<img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt ="Rail Track">
Above, we have also used the alt attribute for the alternate text.
Set the responsiveness
The width is set to 100% to convert an image to responsive. Also, you need to set the max-width property −
img { width: 100%; max-width: 1000px; }
Example
The following is the code to create a responsive image with CSS −
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> img { width: 100%; max-width: 1000px; } </style> </head> <body> <h1>Responsive Images Example</h1> <h2>Resize the window to see responsive image scale up and down</h2> <img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt ="Rail Track"> </body> </html>
Example
Let us see how to make an image responsive using the width and height properties −
<!DOCTYPE html> <html> <head> <style> img { width: 100%; height: auto; } </style> </head> <body> <h1>Responsive Images Example</h1> <h2>Resize the window to see responsive image scale up and down</h2> <img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt ="Rail Track"> </body> </html>
Example
Let us see how to make an image responsive using the max-width and height properties −
<!DOCTYPE html> <html> <head> <style> img { max-width: 100%; height: auto; } </style> </head> <body> <h1>Responsive Images Example</h1> <h2>Resize the window to see responsive image scale up and down</h2> <img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt ="Rail Track"> </body> </html>
Advertisements