Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image resize with CSS

I have a question about the image resizing in CSS.

I have a 1920x1080 image.

This image should be automatically resized to fit on any screen, however I am unable to do this.

Example of what I want here

At the top, a video start to play, that's how I want you to be my page, but instead of a video, the picture.

My HTML:

<body>
<div id="slides">
<ul>
    <li><img src="_img/img1.jpg"></li>
    <li><img src="_img/img2.jpg"></li>
    <li><img src="_img/img3.jpg"></li>
    <li><img src="_img/img4.jpg"></li>
</ul>
</div>

All the images inside the div called slides should fit in any screen.

like image 672
Matheus Cirillo Avatar asked Jul 03 '15 23:07

Matheus Cirillo


2 Answers

First of all you should insert meta viewport on your head, like this

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

then here is a snippet of how you could make this work.

body {
  margin: 0;
  padding: 0
}
ul,
li {
  list-style: none;
  margin: 0;
  padding: 0
}
#slides img {
  max-width: 100%;
  display: block /*fix inline image small gap*/
}

/*larger devices than the size of image */
@media (min-width: 1920px) {
  #slides img {
    width: 100%
  }
}
<div id="slides">
  <ul>
    <li>
      <img src="http://lorempixel.com/1920/1080">
    </li>
    <li>
      <img src="http://lorempixel.com/1920/1080">
    </li>
    <li>
      <img src="http://lorempixel.com/1920/1080">
    </li>
    <li>
      <img src="http://lorempixel.com/1920/1080">
    </li>
  </ul>
</div>
like image 66
dippas Avatar answered Oct 20 '22 01:10

dippas


You should add to your CSS:

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

This make the images fit the width of the container and keeping the aspect ratio.

like image 26
Jabel Márquez Avatar answered Oct 19 '22 23:10

Jabel Márquez