What could be causing my grid to malfunction once the media query is triggered?

I am in the process of creating a straightforward navigation bar. My goal is to have the <nav> content, which includes the navigation links, positioned inside the header along with the logo (a random image from unsplash.it). To achieve this layout, I want it to activate when the screen width exceeds 780px. Therefore, I enclosed the code within a media query. However, the implementation seems to be ineffective as the browser fails to recognize the display: grid command provided.

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
  font-family: Century Gothic, Tahoma, sans-serif;
  font-size: 16px;
}

a {
  color: inherit;
  text-decoration: none;
}

body {
  background: black;
  margin-bottom: 1000px;
}


/* -----DROPDOWN MOBILE MENU----- */

header {
  background: rgba(217, 30, 24, 1);
  position: fixed;
  width: 100vw;
  z-index: 999;
  border: solid white 0px;
  display: flex;
  justify-content: center;
}

header .img {
  width: 10em;
  height: 3em;
  margin: 1em;
  border: solid black 1px;
  transform: scale(1, 1);
  transition: transform ease-in-out 250ms;
  border-radius: 5px;
}

header .img:hover {
  transform: scale(1.025, 1.025);
}

header nav {
  background: rgba(217, 30, 24, .85);
  width: 100vw;
  border: solid white 0px;
  position: absolute;
  top: 100%;
  left: 0;
  display: flex;
  justify-content: center;
}

header nav ul {
  width: 20%;
  border: solid white 0px;
  display: flex;
  flex-flow: column;
  align-items: center;
}

header nav ul li {
  list-style: none;
  color: white;
  padding: .5rem;
}

header nav ul li:hover {
  color: rgba(245, 245, 245, .7);
  cursor: pointer;
}


/* -----QUERY FOR LARGE SCREENS----- */

@media (min-width:780px) {
  header {
    border: solid white 1px;
    display: grid;
    grid-template-columns: 1fr auto 1fr 1fr;
    align-items: center;
  }
  header .img {
    border: solid blac 3px;
    border-radius: 5px;
    width: 10rem;
    height: 3rem;
    transform: scale(1, 1);
    transition: transform ease-in-out 250ms;
    grid-column: 2 / 3;
  }
  header .img:hover {
    transform: scale(1.025, 1.025);
  }
  header nav {
    background: rgba(217, 30, 24, 1);
    border: solid white 1px;
    width: 50vw;
    grid-column: 4 / 5;
  }
  header nav ul {
    display: flex;
    flex-flow: row;
    list-style: none;
    border: solid white 0px;
    width: 100%;
    justify-content: space-around;
    cursor: pointer;
  }
  header nav ul li::before {
    content: '';
    display: block;
    height: 2px;
    width: 0%;
    background: ivory;
    position: absolute;
    bottom: 0px;
    transition: ease-in-out .25s;
    -webkit-transition-duration: .25s;
    -o-transition-duration: .25s;
    -moz-transition-duration: .25s;
  }
  header nav ul li {
    color: white;
    display: flex;
    border: solid black 0px;
    justify-content: center;
    align-items: center;
    transition-duration: .5s;
    -webkit-transition-duration: .5s;
    -o-transition-duration: .5s;
    -moz-transition-duration: .5s;
    position: relative;
    padding: 0;
    padding-bottom: .5rem;
  }
  header nav ul li:hover {
    color: rgba(245, 245, 245, .7);
  }
  header nav ul li:hover::before {
    width: 100%;
  }
}


<!-- END LARGE-SCREEN QUERIES --> 
<header>
  <a href="#"><img class="img" src="//unsplash.it/300/100"></a>
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Support us</a></li>
      <li><a href="#">Contacts</a></li>
    </ul>
  </nav>
</header>

Answer №1

Your <nav> was styled with

position: absolute

Removing this will allow your <nav> to fit into the grid properly.

To address some other issues mentioned in your comments, consider implementing the following:

Create a margins-container to enclose your site and prevent it from touching the edges. Define a class with a specific width, margin: 0 auto, and a max-width setting. Note that if placed within a grid element, it will occupy one fraction of the space.

Here is an example of a margin-container I often use on my sites:

.margins-container {
    width: 90%;
    max-width: 1244px;
    margin: 0 auto 0 auto;
    @media (max-width: $tablet) {
         margin: 0 auto 20px auto;
     width: 95%;
     }
}

In the meantime, consider adding padding to the header. Within the media query, add the following CSS:

header {
  border: solid white 1px;
  display: flex;
  justify-content: space-between;
  padding: 0 30px;
}

This adjustment will resolve elements touching the edges and enhance compatibility with browsers such as IE. For a comprehensive guide on Flexbox, refer to: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Additionally, consider removing the border within this media query:

header nav {
  background: rgba(217, 30, 24, 1);
  width: 50vw;
}

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

The automatic selection process for IE document modes

It may seem simple, but I'm a little perplexed about how IE decides which browser mode to use. In IE9, there are options such as IE9 Compatibility Mode, IE9, IE8, and IE7. These modes are selected automatically depending on the webpage. Can someone e ...

What is the best way to toggle dropdown menu items using jQuery?

Upon clicking on an li, the dropdown menu associated with it slides down. If another adjacent li is clicked, its drop down menu will slide down while the previous one slides back up. However, if I click on the same li to open and close it, the drop down m ...

send data to a hyperlink

I need to pass a parameter to the href based on the first name in the list. The names are links to another page that I retrieve via an _id passed in the URL. The issue I am facing is that the id is not being passed to the URL, resulting in an error. How ...

Styling tip: Distance the tooltip from its parent element when scrolling through a list

I have a table inside li tag, and I want to display the description on :hover inside td. However, when scrolling through the list, the description content starts to appear with a distance from its parent. Despite trying various combinations of position se ...

Ways to trigger the display of an image upon hovering over a button without using inheritance

<div class="a"> <img class="img_c" id="img_id" src="~~"> <div class="b"> <div class="c"> <ul class="d" id="e"> <li ~~~ > </ul> </div> </div> </div> Is there a way to display the ...

Differences in React projects utilizing materialize-css compared to those using react-toolbox or material-ui

Is there a difference in technical benefits or code reliability when directly using material-css in JSX versus utilizing JSX specific libraries like material-ui or react-toolbox? Conversely, could using JSX libraries like material-ui or react-toolbox provi ...

The dropdown menu failed to display properly as intended

I'm encountering an issue with the CSS for my menu. Here is a screenshot of the dropdown menu: Whenever I hover over the submenu, it drops down along with the parent menu container. Please take a look and let me know what might be causing this probl ...

Laravel error message for unresolved symbolic link

Previously, I had developed a form for uploading profile pictures using the code snippet below, $image = $request->file('avatar'); $pass = Hash::make($request->password); if($image!=null){ $avatar_na ...

Tips for accessing a DOM element's ::before content using JavaScript

Is there a way to retrieve the content of a DOM element's ::before pseudo-element that was applied using CSS3? I've attempted several methods without success, and I'm feeling very confused! // https://rollbar.com/docs/ const links = docum ...

Understanding the Code for Responsive Web Typography with line-height in ems

I am currently trying to delve into the CSS calculation process behind the styling in the code snippet provided. This code snippet is from a responsive WordPress theme, and I am struggling to decipher how the em values for the line-height of the <h> ...

Do not use Express.js to serve the index.html file

Encountered an issue when attempting to run my Express.js solution. Instead of opening my desired index.html file located in the client directory, it kept opening the index.jade file from the views folder with the message "Welcome to Express" on the browse ...

What is the proper way to utilize the script type "text/x-template" in a multilanguage context?

I'm currently working with ASP NET and VueJS. In my ASP NET, I have a view component that looks like this: <script type="text/x-template" id="form-template"> @if (string.IsNullOrEmpty(Model.LastName)) { <in ...

Utilizing Font Awesome for social icons in a Vue component display

Currently, I'm in the process of building my own website and making the switch from traditional HTML, CSS, and JS to using VueJs. I've hit a roadblock when trying to transfer some code from my original HTML file to a Vue JS component, specificall ...

Styling a component next to another using CSS

Struggling with a simple question here. I'm experimenting with HTML and CSS, trying to create a page with a central content box flanked by two arrow images on each side. While the concept isn't too difficult, I'm running into issues with usi ...

What is the best strategy for managing pagination when dealing with a large number of pages?

Up until now, I have understood that pagination only links different pages together. This works fine when dealing with a limited number of pages or posts to display. However, what if I have 30 or more pages to paginate? Wouldn't it be impractical to c ...

It can be challenging to manage numerous if-else conditions in JQuery

I am having trouble with my JQuery code that checks multiple conditions. No matter what I enter, it always goes to the else condition. $(function() { $('#installment').on("keydown keyup", check); function check() { var inst = Number($( ...

Accessing an Android device from localhost using a hostname with ever-changing IP addresses

SCENARIO: I am currently running a local webpage on a WAMP server from my computer, which is connected to a wireless network. Additionally, I have an Android tablet also connected to the same network. OBJECTIVE: My goal is to access the local webpage on ...

How can I create a component that dynamically adjusts to different screen sizes

I have developed three child components for the main content area (excluding the header) However, one of the components is not responsive. Below is the code snippet: <div className="row" style={{marginTop:'30px',}}> <div class ...

blending the transition from dark image to black background

Utilizing the CSS below: #divPic { height: 32pc; background-image: url('https://wallpaperscraft.com/image/black_black_white_bone_time_game_noise_74543_4000x2248.jpg'); background-repeat: no-repeat; background-position: left center; b ...

I am looking to transfer the value of one textbox to another textbox within a dynamic creation of textboxes using JavaScript

var room = 1; function add_fields() { room=$('#row_count').val()-1; room++; var objTo = document.getElementById('education_fields'); var divtest = document.createElement("div"); divtest.setAttribute("class", "form- ...