Media queries in CSS Grid causing elements to be pushed outside of the grid

Currently, I am delving into CSS grid, encountering a peculiar issue along the way. I'm in the process of constructing a basic grid consisting of five areas - Header, Main, Footer, left sidebar, and right sidebar. Everything seems to be functioning smoothly until I resize the grid, at which point the right sidebar inexplicably shifts beneath the grid on the right side. Here is the code snippet:

<div class="container">
   <div class="red_box left-bar box"><h2>On left</h2></div>
   <div class="green_box header box">Header</div>
   <div class="orange_box right-bar box"><h2>On right</h2></div>
   <div class="coral_box main box">Main</div>
   <div class="blue_box bottom box"><h2>Footer</h2></div> 
</div>

If you want to view the CodePen demo showcasing this issue, click here. To clarify, after applying the media query, my objective is for only the Header, Middle, and Footer sections to be visible.

Here is the corresponding CSS code:

.container{
           display:grid;
           grid-template-columns:auto;
           grid-template-rows:auto;
           grid-template-areas: 
                              "leftbar header header header rightbar"
                              "leftbar middle middle middle rightbar"
                              "leftbar middle middle middle rightbar"
                              "leftbar middle middle middle rightbar"
                              "leftbar middle middle middle rightbar"
                              "leftbar middle middle middle rightbar"
                              "footer footer footer footer footer";
           height:400px;
       }

      .box{
          border:1px solid black;
          width:100%;
          height:100%;
          text-align:center;
      }

     .right-bar{
          grid-area:rightbar;
     }

     .left-bar{
          grid-area:leftbar;
     }

    .header{
          grid-area:header;
    }

    .main{
          grid-area:middle;
    }

    .bottom{
          grid-area:footer;
    }

    .red_box{
          background-color:red;
    }

    .orange_box{
          background-color:orange;
    }

   .blue_box{
         background-color:blue;
    }

   .green_box{
         background-color:green;
   }

   .coral_box{
         border:1px solid black;
         background-color:coral;
         width:100%;
         height:100%;
         text-align:center;
   }

  @media (max-width:1000px){
      .container{
          grid-template-areas:
                             "header header header"
                             "middle middle middle"
                             "middle middle middle"
                             "middle middle middle"
                             "middle middle middle"
                             "middle middle middle"
                             "footer footer footer";
       }
  }

Answer №1

When you exclude leftbar and rightbar from your grid-template-areas, they are not removed from the DOM but simply taken out of the grid layout. In fact, your left-bar is positioned behind your right-bar when the media query is active, which is why it is not visible.

To completely hide them, you should add display: none to their styles:

.left-bar, .rigth-bar{
  display: none;
}

Below is a snippet from your code pen demonstrating this:

.container {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: auto;
  grid-template-areas: "leftbar header header header rightbar" "leftbar midle midle midle rightbar" "leftbar midle midle midle rightbar" "leftbar midle midle midle rightbar" "leftbar midle midle midle rightbar" "leftbar midle midle midle rightbar" "footer footer footer footer footer";
  height: 400px;
}

.box {
  border: 1px solid black;
  width: 100%;
  height: 100%;
  text-align: center;
}

.rigth-bar {
  grid-area: rightbar;
}

.left-bar {
  grid-area: leftbar;
}

.header {
  grid-area: header;
}

.main {
  grid-area: midle;
}

.bottom {
  grid-area: footer;
}

.red_box {
  background-color: red;
}

.orange_box {
  background-color: orange;
}

.blue_box {
  background-color: blue;
}

.green_box {
  background-color: green;
}

.coral_box {
  border: 1px solid black;
  background-color: coral;
  width: 100%;
  height: 100%;
  text-align: center;
}

@media (max-width:1000px) {
  .container {
    grid-template-areas: "header header header" "midle midle midle" "midle midle midle" "midle midle midle" "midle midle midle" "midle midle midle" "footer footer footer";
  }
  .left-bar,
  .rigth-bar {
    display: none;
  }
}
<div class="container">
  <div class="red_box left-bar box">
    <h2>On left</h2>
  </div>
  <div class="green_box header box">Heade</div>
  <div class="orange_box rigth-bar box">
    <h2>On right</h2>
  </div>
  <div class="coral_box main box">Main</div>
  <div class="blue_box bottom box">
    <h2>Footer</h2>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Design an HTML button/label with text and a starting width of zero

I'm attempting to design HTML buttons and labels with text that initially have zero width. Once inserted, they should smoothly transition to a visible state. I've tried adjusting the initial width and min-width style properties without success. ...

Showing tags with varying sizes

https://i.sstatic.net/oo3MP.png Is there a better way to organize and display my content? I have an array of elements that I want to display like the example above. I attempted using lists, but it didn't work out as expected. Here's what I&apo ...

Explore various color schemes for diverse paths

I am currently working with the following structure: App Component: <div className="App"> <Header></Header> <Container></Container> <Footer></Footer> </div> Container Component: <Router> <div cla ...

jQuery Masonry now renders "row blocks" as opposed to trying to fit elements into place

Have you ever heard of the jQuery masonry plugin? It's a great tool for placing images in "blocks" instead of trying to squeeze them into empty spaces. Take a look at this explanation: But how can we minimize those pesky "voids"? HTML: <!-- This ...

Troubleshooting issue with CSS `gap` property malfunctioning in conjunction with `display: block;` setting

I've run into an issue with the gap property in CSS not working as expected in my layout. After investigating, I found that some of my containers have display: block; set, which seems to be causing the gap property to be ineffective. My goal is to cre ...

Utilizing Bootstrap's column grid system (col-sm) to set the width of form controls

Is it possible to utilize col-sm or col-xs within the input tag to specify its width? <div> <input type="button" class="col-sm-2" value="Clear"> <input type="button" class="col-sm-2" value="Confirm"> </div> ...

A different approach to achieve the same design using CSS

Looking to create a layout where each row displays identical images with unique content in HTML/CSS. I have successfully used tables in HTML and CSS to accomplish this task, but I am curious if there are better practices for creating such a design. The go ...

Floating CSS drop menu with added bottom margin

I have created a simple dropdown CSS menu positioned in the footer with an upwards direction. You can view it here: http://jsfiddle.net/RmTpc/11/ My issue is that I want the opened menu to have some bottom margin from the main list item ("Items"). However ...

``The issue concerning the preloading layer on the website``

My experience with the website www.wirecreation.net is sometimes marred by an issue during the initial load. It seems that there is a margin of a few pixels on the gray div containing the logo and progress bar at times, but other times it appears with zero ...

Step-by-step guide to positioning an iframe with 'transform:scale' on the left side

Recently, I encountered an issue with an iframe in my project. Initially, without any 'transform:scale' css applied, it displayed on the top-left side of the div as expected. However, after adding -webkit-transform:scale(0.6,1); margin-left:0.0e ...

Applying a live status to a particular component (Navbar)

Currently, I've been diving into creating a navbar and wrestling with some jQuery code that was passed my way. I'll be sharing my code below, along with a Codepen link (https://codepen.io/JustNayWillo/pen/aXpKbb) for a better understanding of th ...

Difficulty in breaking text within flexbox styling

I'm facing an issue where the text inside a flex container does not break correctly when the horizontal space is too small to display it all. But shouldn't it still not break even in the "normal" state? What mistake have I made here? (functio ...

Embeddable javascript code can be enhanced by incorporating references into the header

I have developed a basic calculator application using HTML, JavaScript, JQuery, and CSS. My intention is to allow others to embed this calculator app on their own web pages by utilizing a file named generate.js. Within this file, there are several documen ...

Assist me in temporarily altering the color of menu items in a sequential manner upon the page's loading

Currently, I'm working on a website that features a subtle dark grey menu at the top of every page. The menu is built using HTML and CSS with a list structure. To highlight the corresponding menu item based on the current page, I am utilizing the ID a ...

Is there an automatic bottom padding feature?

Currently, I am facing a challenge in fitting the loader into the container without it being overridden by the browser. Using padding-bottom is not an ideal solution as it results in the loader appearing un-resized and unprofessional. Any suggestions or co ...

Is there a way to automate testing for my web application bundled with webpack against FUOC?

After creating a React + Redux web app bundled with webpack, I encountered a bundling error that caused my website to display flash of unstyled content (FUOC) behavior. Certain components did not inject their CSS into the server response, resulting in pa ...

What is the best way to end a CSS image slideshow on its final image?

My slideshow consists of 3 images, each with different texts. I'm trying to figure out how to stop the autoplay feature when the last image is displayed, so it doesn't fade out and disappear. Below is the code snippet: HTML section: <div cla ...

CSS - uniform sizing for border images

INQUIRY: I am looking for a way to maintain a consistent border image size when resizing a div. I have considered using pseudo elements, but I'm wondering if there is a simpler solution? HTML: <div id="resizable" class="ui-widget-content"> & ...

Ways to avoid scrolling on a fixed element

Here is the HTML setup I have... body .top .content The issue I am facing is that when scrolling reaches the end of the ul in the .top element, the background starts to scroll. This can be quite disorienting and makes the site slow on tablets. Even ...

Displaying a block of three cells requires the removal of any spacing between them

Click here to view the code <table cellspacing="1" width="50%" bgcolor="#cccccc"> <tr class="csstextheader"> <td> </td> <td>Class </td> <td>Numbers </td> </tr& ...