Home > Back-end >  Displaying a full screen quadrant of 4 images using Flexbox
Displaying a full screen quadrant of 4 images using Flexbox

Time:09-21

I am looking to display 4 quadrants on the page with no vertical or horizontal scrollbar using flexbox. I achieved this. Each quadrant will contain an image. This is where the problem comes in. The aspect ratio of the images will be the same but the sizes could vary.

I would like each image to always completely fill its div regardless of the size of the display (something like what object-fit: contain does).

The images should not overflow outside of their divs and should not create a scrollbar.

Guidelines:

  • The 4 quadrants combined should fill the entire browser window with no horizontal or vertical scrollbars
  • Each image must fully fill its quadrant without any overflow or padding / spacing

Here is what I have so far:

body {
    padding: 0; margin: 0;
    height: 100vh;
}

.item {
    width: 100%;
    height: 100%;
  }
  
  .container {
    display: flex;
    flex-wrap: wrap;
  }
  
  .container > div {
    flex: 50%;
    box-shadow: 0 0 0 1px black;
  }


  img {
    width: 100%;
    object-fit: contain;
  }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="styles.css">
    <!-- <script src="scripts.js"></script> -->
</head>
<body>
    <div >
        <div ><img src="https://dummyimage.com/400x200/000/fff"></div>
        <div ><img src="https://dummyimage.com/800x400/000/fff"></div>
        <div ><img src="https://dummyimage.com/1200x800/000/fff"></div>
        <div ><img src="https://dummyimage.com/2400x1600/000/fff"></div>
      </div>
</body>
</html>

CodePudding user response:

remove the inner div .item set width and height of .container to 100vw & 100vh. image will have 50% on both height and width of the parent.each image size will be equal to 50vw,50vh.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  outline: 1px solid red;
}

.container {
  display: flex;
  width: 100vw;
  height: 100vh;
  flex-wrap: wrap;
}

img {
  width: 50%;
  height:50%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <link rel="stylesheet" href="styles.css">
  <!-- <script src="scripts.js"></script> -->
</head>

<body>
  <div >
    <img src="https://dummyimage.com/400x200/000/fff">
    <img src="https://dummyimage.com/800x400/000/fff">
    <img src="https://dummyimage.com/1200x800/000/fff">
    <img src="https://dummyimage.com/2400x1600/000/fff">
  </div>
</body>

</html>

CodePudding user response:

.container > div {
    display: flex;
    flex: 50%;
    box-shadow: 0 0 0 1px black;
 }     
 img {
    flex-grow : 1l
    width: 100%;
    min-width: 100%;
    max-width: 100%;
    overflow: hidden;
    object-fit: contain;
  }

i think this should work

CodePudding user response:

body {
  padding: 0;
  margin: 0;
}

.container {
  display: flex;
  flex-wrap: wrap;
  width: 100vw;
  height: 100vh;
}

.container>div {
  flex: 50%;
  box-shadow: 0 0 0 1px black;
}

img {
  width: 100%;
  object-fit: cover;
  height: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <link rel="stylesheet" href="styles.css">
  <!-- <script src="scripts.js"></script> -->
</head>

<body>
  <div >
    <div ><img src="https://dummyimage.com/400x200/000/fff"></div>
    <div ><img src="https://dummyimage.com/800x400/000/fff"></div>
    <div ><img src="https://dummyimage.com/1200x800/000/fff"></div>
    <div ><img src="https://dummyimage.com/2400x1600/000/fff"></div>
  </div>
</body>

</html>

I'm not sure what you see; is this what you want?

If it's not, could you share an image of what you seek it to look like

  • Related