Creating a sticky navigation bar in a CSS Grid layout

Using a grid for the first time and attempting to make the nav-bar sticky, I initially used:

position: sticky; top: 0;

without any success. Subsequently, trying:

position: fixed;

resulted in undesired consequences.

Desiring the nav-bar to remain in place while scrolling, I attempted using jQuery but encountered the same outcome.

Answer №1

Alright, I have modified the navbar as per your requirements:

Step 1: Introduced a wrapper for the sticky part of your navbar (excluding the profile image) using this line (refer to comment within HTML: line 18):

<div class="nav-sticky">

Step 2: Closed the wrapper by adding this line at the end of the nav-bar (check HTML comment reference: line 33): </div>

Step 3: Included a CSS rule for the wrapper to make it sticky:

.nav-sticky { position: sticky; top: 0; }
(CSS-line: 355 - 360).

Hence, now the Navbar will scroll and stick to the top of the screen once it reaches there. To also make the profile image stick to the top, simply relocate the opening tag of the wrapper above the image tag to enclose it within the wrapper.

* {
  margin: 0;
  padding: 0;
  font-family: 'Andika New Basic', sans-serif;
  box-sizing: border-box;
}

body {
  background-color: #dedede;
}

.grid-container {
  display: grid;
  grid-template-areas: 'menu header' 'menu portfolio' 'menu about' 'menu contact' 'menu footer';
  width: 80%;
  margin: 0 auto;
  grid-template-columns: 300px 1fr;
  grid-template-rows: 160px auto auto auto 100px;
  grid-gap: 20px;
}


/* Header and Filter */

.header {
  grid-area: header;
  border-bottom: 5px solid #cecece;
  padding: 10px 0;
}

.header h1 {
  font-size: 40px;
  text-align: left;
  font-weight: bold;
}


/* Filter */

.header ul {
  font-size: 15px;
  text-align: left;
  margin-top: 30px;
}

.header ul li {
  display: inline-block;
  list-style-type: none;
  padding: 5px 10px;
  user-select: none;
}

.header i {
  margin-right: 5px;
}

.header span {
  margin-right: 10px;
}


/* Hover effect for Filter */

.header ul li:hover:not(:first-child) {
  cursor: pointer;
  color: #000;
  background-color: #cecece;
}

.active {
  color: #fff;
  background-color: #000;
}

.header ul:first-of-type('li') {
  color: red;
}


/* End of Header and Filter */


/* Side menu with profileheader */

.nav-bar {
  grid-area: menu;
  background-color: #fff;
}


/* Profile picture */

#profile-picture {
  height: 100%;
  width: 100px;
  height: 100px;
  margin: 20px 20px 30px 20px;
}

#profile-picture img {
  object-fit: cover;
  border-radius: 4px;
}

.nav-bar h2 {
  font-size: 25px;
  margin-left: 20px;
}


/* Side navigation */

.nav-bar ul {
  list-style-type: none;
  font-size: 20px;
  margin-top: 15px;
  user-select: none;
}

.nav-bar ul a {
  text-decoration: none;
  color: #000;
  display: block;
  padding: 10px 20px;
}

.nav-bar ul a i {
  margin-right: 10px;
}

.nav-bar ul a:hover {
  background-color: #cecece;
}

#social-media {
  margin: 10px 0 20px 20px;
  font-size: 20px;
}

#social-media a {
  text-decoration: none;
  color: #000;
}

#social-media a:hover {
  opacity: 0.5;
}

.nav-bar
/* End of navigation */

.main {
  grid-area: portfolio;
}

.main {
  display: grid;
  grid-template-areas: 'project project project' 'project project project' 'project-footer project-footer project-footer';
  width: 100%;
  margin: 0 auto;
  grid-gap: 20px;
  grid-template-rows: 400px 400px;
  border-bottom: 3px solid #cecece;
}

.image-container {
  height: 250px;
  background-image: url('file:///C:/Users/Dell/Desktop/Portfolio/img/avatar.jpg');
  background-size: cover;
  margin-bottom: 10px;
}

.project-footer {
  margin: 0 auto;
  grid-area: 'project-footer';
  grid-column: 1 / 4;
  margin-bottom: 30px;
}

.image-button {
  display: inline-block;
  width: 40px;
  text-align: center;
  padding: 10px;
  font-size: 12px;
}

.image-button:hover {
  background-color: #000;
  color: #fff;
  cursor: pointer;
}

.project-footer .active:hover {
  color: #000;
  background-color: #cecece;
}

.about {
  grid-area: about;
}

.about {
  display: grid;
  grid-template-areas: 'about about' 'about about' 'about-me about-me';
  width: 100%;
  margin: 0 auto;
  grid-gap: 20px;
}

.about-image {
  background-image: url('file:///C:/Users/Dell/Desktop/Portfolio/img/avatar.jpg');
  background-size: cover;
  margin-bottom: 10px;
  height: 400px;
}

.about-me {
  grid-area: 'about-me';
  grid-column: 1 / 3;
  margin-bottom: 20px;
  padding: 10px;
}

.skills {
  margin: auto;
  text-align: center;
}

.skills ul {
  list-style-type: none;
  padding: 20px;
}

.skills ul h4 {
  font-size: 20px;
}

.skills ul li {
  margin-bottom: 10px;
}

.about-me p {
  margin-bottom: 50px;
}

.about-me a {
  text-decoration: none;
  color: #000;
  padding: 15px;
  background-color: #cecece;
}

.about-me a:hover {
  background-color: #bebebe;
}

.contact {
  grid-area: contact;
  background-color: #bebebe;
  padding: 15px;
  width: 100%;
  display: grid;
  grid-template-areas: 'contact-header contact-header contact-header' 'contact-button contact-button contact-button' 'contact-hr contact-hr contact-hr' 'contact-main contact-main contact-main';
  width: 100%;
  margin: 0 auto;
  grid-template-columns: 33%;
  grid-gap: 20px 0px;
}

.contact h3 {
  grid-area: contact-header;
  grid-column: 1 / 4;
  margin-bottom: 10px;
}

.contact a {
  background-color: #333;
  color: #fefefe;
  text-decoration: none;
  font-size: 15px;
  text-align: center;
  padding: 15px;
}

.contact:nth-child(5) a:nth-child(3) {
  background-color: rgb(34, 151, 87);
}

.contact a i {
  display: block;
  font-size: 30px;
  margin-bottom: 5px;
}

.contact hr {
  grid-area: contact-hr;
  grid-column: 1/4;
  color: #fefefe;
  background-color: #fefefe;
  height: 1px;
  border: none;
  margin-top: 20px;
}

.contact form {
  grid-area: contact-main;
  grid-column: 1/4;
}

.contact input,
.contact textarea {
  width: 100%;
  margin-bottom: 10px;
  border: none;
  outline: none;
  resize: none;
  padding: 10px;
}

.contact button {
  border: none;
  background-color: #000;
  color: #fff;
  padding: 10px 20px;
  font-size: 14px;
}

.contact button:hover {
  cursor: pointer;
  background-color: #dedede;
  color: #000;
}

.contact button i {
  margin-right: 10px;
}

.footer {
  grid-area: footer;
  text-align: center;
  background-color: #333;
  color: #cecece;
  padding: 20px;
}

.footer a {
  color: #cecece;
}

/* Adding css for the sticky wrapper */

.nav-sticky {
  position: sticky;
  top: 0;
}


/* Extra small devices (phones, 600px and down) */

@media only screen and (max-width: 600px) {
  .example {
    background: red;
  }
}


/* Small devices (portrait tablets and large phones, 600px and up) */

@media only screen and (min-width: 600px) {
  .example {
    background: green;
  }
}


/* Medium devices (landscape tablets, 768px and up) */

@media only screen and (min-width: 768px) {
  .example {
    background: blue;
  }
}


/* Large devices (laptops/desktops, 992px and up) */

@media only screen and (min-width: 992px) {
  .grid-container {
    margin: 0;
    width: 99%;
  }
}


/* Extra large devices (large laptops and desktops, 1200px and up) */

@media only screen and (min-width: 1200px) {
  .example {
    background: pink;
  }
}

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

Designing a responsive navigation bar with slide functionality for both web and mobile interfaces utilizing Bootstrap

I have integrated Bootstrap 4.5.0 with the necessary CSS and script from the CDN on my webpage. I am attempting to create a responsive navigation bar header similar to the one on the Bootstrap website, which functions effectively on both desktop web browse ...

What is the best way to center my image both vertically and horizontally?

Utilizing react.js to develop a template website has been my current project. The issue arose when I began constructing the first component of the site. The dilemma: The picture was not centered on the page, neither vertically nor horizontally. Despite ...

The Google API is experiencing issues when using input that has been added on

Situation: In my attempt to integrate a Google address search feature into an online shopping website, I encountered a challenge. I don't have direct access to the website's HTML code, but I can insert code snippets in the header, footer, and in ...

Utilizing jQuery for dynamic horizontal positioning of background images in CSS

Is there a way to set only the horizontal background property? I've tried using background-position-x and it works in Chrome, Safari, and IE, but not in Firefox or Opera. Additionally, I would like to dynamically set the left value of the position. ...

Is there a way to relocate the play again button below the box?

How can I ensure that my "Play Again" button moves up to align perfectly under the black border box? The black border box is styled with a class named .foot .button { background: rgb(247, 150, 192); background: radial-gradient(circle, rgba(247, 1 ...

The JavascriptExecutor is unable to access the 'removeAttribute' property of a null object

While utilizing Javascript executor to remove the readonly attribute, I encountered an error message: Cannot read property 'removeAttribute' of null. I came across various discussions where users suggested that removing AdBlock from Chrome solve ...

The issue arises when I try to retrieve information from MySQL using Ajax, as it appears

I am currently working on developing an ecommerce website. In order to display products, I am fetching data from a database. My goal is to allow users to add products to their cart without having to refresh the page. I have attempted to use AJAX for this ...

Can you explain the distinctions among <Div>, <StyledDiv>, and <Box sx={...}> within the MUI framework?

When exploring the MUI documentation, I came across a table of benchmark cases that can be found here. However, the differences between the various cases are not clear to me. Can someone please explain these variances with real examples for the following: ...

Full-width sub menu within the menu

I'm trying to make a sub menu appear below my main menu that is the same width as the main menu, which has a fixed width. I want the sub menu to adjust its width based on the number of links it contains. Is this even possible? Here's the code I h ...

The issue regarding the fixed table layout bug in Firefox

Upon testing Firefox 5, it has come to my notice that an additional pixel of space is being added between the right column and the table border due to this particular piece of code: <style type="text/css"> table { border: 1px s ...

Having issues with the Bootstrap tabs code provided in the documentation not functioning correctly

Exploring the world of Bootstrap 5 tabs led me to copy and paste code from the official documentation (https://getbootstrap.com/docs/4.1/components/navs/#javascript-behavior), but unfortunately, it's not functioning as expected: clicking on a tab does ...

Ways to manage an element that has been loaded using the load query function

After implementing the query load function to update posts on the homepage, I was able to display the most up-to-date posts. However, a new issue arose: Whenever I updated all posts without refreshing the entire page, I needed a way to control the element ...

Incorporate zoom feature into the jQuery polaroid gallery

Currently, I am utilizing a jQuery and CSS3 photo gallery found on this website. My goal is to allow the images to enlarge when clicked, however, the method provided by the author isn't very clear to me, as I'm not an expert in jQuery. I attempt ...

Troubleshooting the fluid container problem in Bootstrap 3

I am currently working on a website using Bootstrap 3 framework. I have a specific section where I need to have two fluid containers placed side by side, each with different background colors. One of these containers should also include a background image ...

"Internet Explorer text input detecting a keyboard event triggered by the user typing in a

It appears that the onkeyup event is not triggered in IE8/IE9 (uncertain about 10) when the enter button is pressed in an input box, if a button element is present on the page. <html> <head> <script> function onku(id, e) { var keyC = ...

In Javascript, the DOM contains multiple <li> elements, each of which is associated with a form. These forms need to be submitted using AJAX for efficient

I have a list element (ul) that includes multiple list items (li), and each list item contains a form with an input type of file. How can I submit each form on the change event of the file selection? Below is the HTML code snippet: <ul> ...

use python selenium to retrieve text enclosed by two span elements

<li class="page-item active"> <span class="page-link"> "12" <span class="hjhs">(current)</span> </span> </li> After reviewing the code above, my objective is to extr ...

Title: "Customizing Labels on Stack Bars and Lines in D3.js Visualization"

Currently working on a stacked bar chart with a line chart on dual axis using D3.js and facing difficulty aligning labels correctly. Check out the code I have experimented with so far: https://plnkr.co/edit/doobXBC5hgzvGwDLvArF?p=preview I am looking to ...

How to Embed Images from a Different Server Using PHPmailer

Currently, I am using phpmailer and AddEmbeddedImage to send emails. The web application I am working on sources data from two different servers: and . The website is hosted on and all scripts are executed from there as well. However, when I use phpmaile ...

Partially Assessed Ajax Response

I am struggling with my ajax response as it seems to evaluate only some of the html, but not all of it. For instance, I have this code that replaces a div with the response from the request: eval(document.getElementById("test").innerHTML-xmlhttp.response ...