Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit an image according to screen size

Tags:

html

css

Let's say we have image with 2000px width & 500px height in css property. For a 1080p monitor how can I configure this image properly. I want this image to be set on any screen size for responsive design.

like image 390
Saji Avatar asked May 31 '15 08:05

Saji


People also ask

How do I make an image fit my screen in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How do I make my image fit responsive?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.

How do I resize an image and keep the aspect ratio?

How to resize the image to a specific aspect ratio? In the Crop Image section, select one of the available ratios from the Aspect Ratio menu or add a custom one. Then click the Resize image to aspect ratio option to resize the image instead of cropping it.


1 Answers

This is what you want:

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

If you want your image to be scaled differently (or add/override certain styles for more responsivenss) in different devices you need to use CSS media queries. Eg.

img {
    display: inline-block;
    width: 25%; // Show 4 images in a row normally
    height: auto;
}

@media (max-width: 600px) {
  img {
    width: 100%; // Override width to show only one image in a row on smaller screens
  }
}

Mozilla Documentation on CSS media queries

W3Schools tutorial on CSS media queries

like image 156
Dilip Raj Baral Avatar answered Oct 02 '22 11:10

Dilip Raj Baral