Image size confusion

Joined
Jul 4, 2023
Messages
368
Reaction score
41
Don't use fixed sizes for images (<img>) if you want to have more responsive images.

Try like this:

CSS:
img {
  display: block;
  height: 100%;
  width: 100%;
  object-fit: cover;
}


another way is put the image as a background for element <div>

Try like this:

CSS:
div {
  width: 100%;
  aspect-ratio: 1;
  background-image: url('https://picsum.photos/1200/800?random=1');
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}

Full demo: on-line.
 
Last edited:
Joined
Jul 4, 2023
Messages
368
Reaction score
41
Don't use fixed sizes for images (<img>) if you want to have more responsive images.

Try like this:

CSS:
img {
  display: block;
  height: 100%;
  width: 100%;
  object-fit: cover;
}

another way is put the image as a background for element <div>

Try like this:

CSS:
div {
  width: 100%;
  aspect-ratio: 1;
  background-image: url('url to your image');
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}

Full demo:

HTML:
<!DOCTYPE html>
<html>
  <head>
    <style>
      
      div {
        width: 100%;
        aspect-ratio: 1;
        background-image: url('url to your image');
        background-repeat: no-repeat;
        background-position: center center;
        background-size: cover;
      }


      .gallery {
        display: flex;
        flex-wrap: wrap;
        border: none;
        height: auto;
        margin-top: 2rem;
      }
      .gallery img {
        width: 20%;
        flex: 1 0 20vh;
        object-fit: cover;

        display: block;
        max-width: 100%;
        margin: .5rem;
        border-radius: .25rem;
      }
    </style>
  </head>
  <body>

    <div></div>

    <section class="gallery">
      <img src="url to your image" />
      <img src="url to your image" />
      <img src="url to your image" />
      <img src="url to your image" />   
    </section>

  </body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Hamburger crisis 3
Hidden drop down 1
Validation crisis 12
Placing a google map into my code 5
Small screen layout trouble 3
Help with my navigation, please 0
.active 8
Flex-shrink 3

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top