skip to Main Content

I’m having difficulty spanning box-two to take up column two and three. I’ve checked for typos. My page looks great before screen width is 1024px. Below is my html page and stylesheet

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="container">
        <div class="box">
            <img src="css/images/blackberries.jpg" alt="blackberries">
        </div>
        <div class="box box-two">
            <img src="css/images/brunch.jpg" alt="brunch">
        </div>
        <div class="box">
            <img src="css/images/utensils.jpg" alt=" cooking utensils">
        </div>
        <div class="box">
            <img src="css/images/blackberries.jpg" alt="blackberries">
        </div>
        <div class="box">
            <img src="css/images/brunch.jpg" alt="brunch">
        </div>
        <div class="box">
            <img src="css/images/utensils.jpg" alt=" cooking utensils">
        </div>
        <div class="box">
            <img src="css/images/blackberries.jpg" alt="blackberries">
        </div>
    </div>
</body>
</html>

My stylesheet is below. I think the issue lies in my stylesheet but I’m having a hard time pinpointing were.

.container {
    display: grid;
    grid-template-rows: repeat(auto, 10rem);
    grid-template-columns: repeat(1, 10rem);
    gap: 10px;
}

img {
    width: 10rem;
    height: 10rem;
}

@media screen and (min-width:768px) {
    
    .container{
        display: grid;
        grid-template-columns: repeat(2, 10rem);
    }
}

@media screen and (min-width:1024px) {
    
    .container{
        display: grid;
        grid-template-columns: repeat(3, 10rem);
        grid-template-rows: repeat(4, 10rem);
    }
    
    .box-two {
        grid-column: span 2 ;
    }
}

I’ve tried grid-column: 2 / 4 and other variants of the grid-column command but it simply adds additional columns without letting box-two take up multiple columns. My code isn’t bringing up any errors and I’ve tried seeing if I was missing anything elsewhere.

2

Answers


  1. I think your CSS works as-is, but your image doesn’t fill the entire cell when it spans multiple columns so it doesn’t appear to be working.

    If you run the snippet below (and open it full-screen to get the layout in question) you can see that box-two does span two columns, but your image is still width: 10rem so it doesn’t grow.

    screenshot of layout

    .container {
        display: grid;
        grid-template-rows: repeat(auto, 10rem);
        grid-template-columns: repeat(1, 10rem);
        gap: 10px;
    }
    
    img {
        width: 10rem;
        height: 10rem;
        visibility: hidden; /* hiding the images for clarity about the grid */
    }
    
    .box {
      border: 1px solid red; /* so we can see the grid cells */
    }
    
    @media screen and (min-width:768px) {
        
        .container{
            display: grid;
            grid-template-columns: repeat(2, 10rem);
        }
    }
    
    @media screen and (min-width:1024px) {
        
        .container{
            display: grid;
            grid-template-columns: repeat(3, 10rem);
            grid-template-rows: repeat(4, 10rem);
        }
        
        .box-two {
            grid-column: span 2;
            background: tomato; /* to accentuate this box */
        }
    }
     <div class="container">
            <div class="box">
                <img src="css/images/blackberries.jpg" alt="blackberries">
            </div>
            <div class="box box-two">
                <img src="css/images/brunch.jpg" alt="brunch">
            </div>
            <div class="box">
                <img src="css/images/utensils.jpg" alt=" cooking utensils">
            </div>
            <div class="box">
                <img src="css/images/blackberries.jpg" alt="blackberries">
            </div>
            <div class="box">
                <img src="css/images/brunch.jpg" alt="brunch">
            </div>
            <div class="box">
                <img src="css/images/utensils.jpg" alt=" cooking utensils">
            </div>
            <div class="box">
                <img src="css/images/blackberries.jpg" alt="blackberries">
            </div>
        </div>

    This probably isn’t the solution, but just to illustrate: adding this rule will allow the image to scale up and fill the full width:

    .box-two img {
      width: 100%;
      height: auto;
    }
    

    with the width: 100% rule

    Login or Signup to reply.
  2. Try using precise CSS Grid properties (like grid-column-start and grid-column-end) for more control over placement.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search