Learn CSS Flexbox by Building a Photo Gallery - Step 21

Tell us what’s happening:
Describe your issue in detail here.
I have learned about the advantages of box-sizing: border-box, but I sometimes do not know why when i use this box-sizing, the web appears like this. For example, when I use box-sing: border-box for this website, it is like this

but when i comment this, it shows:


i do not resize the web, can you explain that difference to me? Thank you.
Your code so far

/* file: index.html */
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Photo Gallery</title>
  <link rel="stylesheet" href="./styles.css">
</head>
<body>
  <div class="header">
    <h1>CSS FLEXBOX PHOTO GALLERY</h1>
  </div>
  <div id="gallery">
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/1.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/2.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/3.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/4.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/5.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/6.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/7.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/8.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/9.jpg"/>
    <img src="https://cdn.freecodecamp.org/curriculum/css-photo-gallery/10.jpg"/>
  </div>
</body>
</html>
/* file: styles.css */
* {
/* box-sizing:border-box; */
}

body {
margin: 0;
font-family: Arial;
background: #EBE7E7;
}

.header {
text-align: center;
/* padding: 32px; */
background: #E0DDDD;
}

#gallery {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
padding: 0 4px;
}

#gallery img {
width: 25%;
height: 300px;
object-fit: cover;
margin-top: 8px;
padding: 5px 70px;
border-radius: 10px;
}

@media (max-width: 800px) {
#gallery img {
  width: 50%;
}
}


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Learn CSS Flexbox by Building a Photo Gallery - Step 21

Link to the challenge:

Because you have the image width set at 50% plus you have added a 4px padding on either side, which makes it just over 50%. So by default only one image can fit in a row because it takes up just over 50% of the horizontal space. But when using box-sizing: border-box the width of the image is calculated to include the 4px on either side of the image, so the actual width of the image is exactly 50% and thus two of them can fit on a row.

1 Like