I’ve been working on a simple product page to practice and better understand key concepts.
However, I’m encountering an issue with the .carousel grid inside a media query. The first element renders with a width and height of 0x0, and I can’t figure out why. Additionally, the gap property isn’t being applied, even though it’s defined within the media query.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Carrousel</title>
<style>
* { /* reset the styles */
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
padding: 0;
margin: 0;
box-sizing: border-box;
}
section {
margin-top: 48px;
}
main {
margin-top: 48px;
}
img {
max-width: 100%;
border-radius: 10px;
}
.container {
max-width: 75%;
margin: 0 auto; /* align content */
display: grid;
grid-template-columns: 1fr 1fr; /* create 2 columns of the same size */
gap: 24px;
}
@media (max-width: 780px) {
.container {
grid-template-columns: 1fr;
}
.carousel {
grid-auto-flow: column;
grid-auto-columns: 100%;
overflow-x:scroll;
}
.grid-item {
padding: 0;
margin: 0;
}
.grid-item:first-child {
outline: orange;
}
}
.carousel {
display: grid;
grid-template-columns: 1fr; /* for two columns: repeat(2, minmax(100, 1fr)) */
grid-auto-rows: 1fr;
gap: 8px;
scroll-snap-type: inline mandatory;
}
.grid-item {
height: auto;
scroll-snap-align: center;
}
.product-info {
position:sticky;
top: 3rem;
background-color: lightseagreen;
max-height: 200px;
h1 {
padding-top: 0;
line-height: 1.5rem;
}
}
</style>
</head>
<body>
<main>
<div class="container">
<div class="carousel">
<img class="grid-item" id="1" src="https://placehold.co/800x800" alt="default">
<img class="grid-item" id="2" src="https://placehold.co/800x800" alt="default">
<img class="grid-item" id="3" src="https://placehold.co/800x800" alt="default">
<img class="grid-item" id="4" src="https://placehold.co/800x800" alt="default">
<img class="grid-item" id="5" src="https://placehold.co/800x800" alt="default">
<img class="grid-item" id="6" src="https://placehold.co/800x800" alt="default">
</div>
<div class="product-info">
<h1>Product Title</h1>
<p>Product Information</p>
<p>$99.99</p>
<button>Add to cart</button>
</div>
</div>
</main>
</body>
</html>
I’ve tried adjusting the width, min-width, max-width, height, and other size-related properties, but the issue persists. It doesn’t seem to be related to the scroll behavior, since even after removing that property, only 5 out of the 6 elements are visible. I also tested by adding more elements and commenting out the first one, but the issue remains—the new first child always has a size of 0x0.
2
Answers
I fixed the two problems this way:
To fix the problem with the div of 0x0 size, I changed the next property of my carousel class.
To fix the problem with the gap, I only put the media query at the end of my styles.
Link to the CodeSandbox
Try defining a height in the grid-item:
For the gap issue try setting the carousel class as: