How come my media query is being ignored by the css grid-template-areas feature?

Having trouble finding a silly error in my code. I've checked multiple times but can't seem to locate it.

The media query grid-template-areas in my code look like this:

". title title ."
". img-1-title img-2-title ."
". img-1 img-2 ."
". img-3-title img-4-title ."
". img-3 img-4 ."

However, the template displayed in my browser is different:

"img-1 title title img-2"
"img-3 img-1-title img-2-title img-4"
". img-3-title img-4-title ."

Here's the HTML snippet:

<section>
  <div class="grid">
    <div class="title">Here is a couple of side projects</div>
    <div class="img-1-title">A chat app build with socket.io and node.js</div>
    <div class="arrow-1"><i class="fas fa-arrow-down"></i></div>
    <div class="img-1"></div>
    <div class="img-2-title">A responsive mock template of a company build with html css and flexboxgrid</div>
    <div class="arrow-2"><i class="fas fa-arrow-down"></i></div>
    <div class="img-2"></div>
    <div class="img-3-title">Wikipedia search bar connected to the wikipedia API</div>
    <div class="arrow-3"><i class="fas fa-arrow-down"></i></div>
    <div class="img-3"></div>
    <div class="img-4-title">Vanilla js calculator</div>
    <div class="arrow-4"><i class="fas fa-arrow-down"></i></div>
    <div class="img-4"></div>
  </div>
</section>

I had to redeclare my grid-area due to a yellow error in the console when not done. It works fine, but I prefer to avoid errors if possible.

CSS styling:

.grid {
        display: grid;
        grid-template-columns: 1fr;
        grid-template-areas: 
        "title"
        "img-1-title"
        "arrow-1"
        "img-1"
        "img-2-title"
        "arrow-2"
        "img-2"
        "img-3-title"
        "arrow-3"
        "img-3"
        "img-4-title"
        "arrow-4"
        "img-4";
        background: rgb(27, 27, 27);
    }

/* CSS continues... */

Answer №1

It appears that everything is functioning correctly from my perspective. The media query adjusts at 1000px.

https://i.sstatic.net/NBuzP.jpg

A suggestion for improvement would be to incorporate a grid-gap to prevent images from clustering together, particularly on smaller devices.

.grid {
  grid-gap: 20px;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas: "title" "img-1-title" "arrow-1" "img-1" "img-2-title" "arrow-2" "img-2" "img-3-title" "arrow-3" "img-3" "img-4-title" "arrow-4" "img-4";
  background: rgb(27, 27, 27);
}

.title {
  grid-area: title;
  padding: 20px 0;
  font-family: Verdana, Tahoma, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 900;
  color: white;
}

.img-1 {
  grid-area: img-1;
  background: url("http://placekitten.com/1500/650") center/cover;
  height: 300px;
}

... (CSS code continues) ...

.arrow-1,
.arrow-2,
.arrow-3,
.arrow-4 {
  display: flex;
  padding: 15px;
  height: 25px;
  justify-content: center;
  font-size: 20px;
}

... (Media Query CSS code continues) ...
<section>
  <div class="grid">
    <div class="title">Here is a couple of side projects</div>
    <div class="img-1-title">A chat app build with socket.io and node.js</div>
    ... (HTML code continues) ...
  </div>
</section>

https://i.sstatic.net/hMmSN.png

If there was any confusion in understanding your question, I apologize for that 😄


The issue may lie within your browser as you have encountered problems before.

I redeclare my grid-area to avoid a yellow error in the console even though it functions properly. I just don't like seeing errors.

Firefox seems to work smoothly, so perhaps consider using it. Additionally, Firefox features a helpful grid-view for analyzing CSS grids.

In case you need specifics, I use

 Firefox Quantum 61.0.1 (64-Bit) 

My testing shows that the files function well on Chrome as well, specifically tested on version

 Version 68.0.3440.75 (official build) (64-Bit) 

To address the issue on Chrome, check your browser version and compare it against the list of supported browsers.

If you're unsure how to determine your browser version, refer to this guide for most modern web browsers. [LINK]

Answer №2

Wow, I finally figured out the issue! It turns out that having <a> tags before my div elements was causing grid malfunctions. I can't believe I overlooked including them in my HTML thinking they weren't the culprit - facepalm moment.

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

Is it possible for a navbar in CSS to collapse based on its width?

At the moment, I am utilizing Bootstrap v3.3.6 and jQuery v1.9.1. My current project involves a horizontal navbar that collapses once it reaches a specific screen resolution. I am pleased with how this feature operates and wish to maintain the same breakpo ...

The Chrome equivalent of -moz-user-focus

We have a custom model dialog control that we use for all popups on our web pages. When this dialog is initialized, we use jQuery expose to gray out the rest of the page. To prevent selection on the grayed-out area, we apply the following styles to the mas ...

If the initial hidden field contains data, then assign the checkbox value to a different hidden input field

I am dealing with a group of 4 checkboxes. Once any two checkboxes have been checked, their values are duplicated into two hidden input fields. The value of the first checked checkbox is copied to the first input with id="checkedBox1" Now, I am trying t ...

Filtering posts in Angular with the use of pipes

I've been attempting to use pipes in Angular to filter posts from a table, but I keep running into this error on the console: core.js:12689 Can't bind to 'ngForFilter' since it isn't a known property of 'a'. I believe th ...

Javascript program that generates drag and drop elements using HTML5 features:

Whenever I attempt to drag and drop elements that are normally created by HTML, everything works fine. However, when I try to drag and drop elements generated by JavaScript, I encounter the following error: Uncaught TypeError: Cannot read property 'p ...

Can Python and Selenium help me extract text from HTML table cells?

On a website, I have a table that looks similar to this: <table class="table-class"> <thead> <tr> <th>Col 1</th> <th>Col 2</th> <th>Col 3</th> </tr> </thead> ...

Guide on presenting an image retrieved from a database with ajax

I am currently developing a straightforward mobile application that utilizes a database to store various types of content, including images. I have implemented ajax requests to retrieve this information and display it within the app. However, I am encounte ...

What is the correct way to superimpose an image with a transparent background?

I am currently working on a mini game that involves overlaying images on camera views. The goal is to have the gun displayed without the background rectangle, as shown in this image: https://i.stack.imgur.com/yU4A2.jpg The code snippet below is relevant ...

Float from the bottom and then rise up

I'm attempting to use CSS to make code from further down in the HTML appear above code from a section above it. Here is how my HTML structure looks: <div class="showbelow"> show below </div> <div class="showabove"> show above < ...

Can someone point out the mistakes in my CSS Layout?

I've been struggling to make progress on this task over the past couple of days, encountering roadblocks with no clear solution in sight. The maze of nested divs I'm navigating through might be playing tricks on my mind, so having a fresh pair of ...

Attempting to pass HTML content from a Laravel controller to display in data tables

Issue Description I am trying to send HTML tags to a table using editColumn but it is not working. AddColumn works fine for adding HTML code to a column, but editColumn does not display the HTML elements as expected. My question is how can I successfully ...

Having trouble getting the product PHP script to upload properly

I have been struggling with a Php script that I created to upload products to my website for over 5 days. Unfortunately, it's not functioning properly and no errors are being displayed. Despite my best efforts, I am unable to identify the error in the ...

Efficiently shrink column width in Material-UI's <TableRow/> using ReactJS and Material-UI

At the moment, I am utilizing ReactJS along with Material-UI. One issue I am encountering is that when using Material-UI's <Table>, the columns' width are automatically set based on content which results in all columns having equal width. H ...

Implement a transition effect for when elements change size using the `.resizable().css` method

I attempted to incorporate a deceleration effect when resizing an element back to its original size using the resizable method. The slowdown effect should only trigger during the click event (when the "Click-me" button is pressed), not while manipulating ...

.Net authentication using Windows credentials

As I work on developing a website utilizing ASP.net and C#, one of my requirements is to utilize windows authentication for user login. Currently, I am experimenting with storing the authenticated user's name in a session in order to access it later ...

Is it possible for jQuery UI Dialog to function similar to an iFrame?

Is there a way to set up my dialog box with a form so that when the form is submitted, only the dialog box changes instead of the browser location? ...

How to Use HTML5 and PHP to Retrieve and Change the Inputted Month Value to a String, then Subtract One Month from the Input Value

Recently, I had a unique experience with the Stackoverflow community. Despite hours of searching on Google and trying different solutions, I was unable to find a proper code for my problem. The input and desired output seem simple at first glance, but codi ...

Is it possible to generate an image from HTML using PHP?

Can anyone suggest a way to generate an image from pure HTML using PHP, possibly utilizing the GD library or other similar tools? I am looking to display icons from external sites and considering if creating an image would help improve load times. If not ...

Animate the sliding up of divs using Jquery SlideToggle

Is there a way to make this perfect example slide up instead of pushing the 'navs' down? View Example Here Currently, it reveals from top to bottom (pushing the menu down), but I want it to slide up and push the navs up when clicked. The fiddl ...

Ways to align the navigation icon to the left side

I need help positioning my help icon at the bottom of the screen, regardless of the user's monitor size. I've tried using margin and margin-top, but it doesn't adjust properly when changing monitor sizes. Any suggestions or assistance would ...