Fixed navigation menu at the top of the screen

Can anyone help me with my navbar issue? I want it to stay on top of the page, but there's a huge gap between the top of the page and the nav bar. I've tried running a JS file, but it doesn't seem to fix the problem. Any ideas on how to make it work properly? HTML can be so tricky sometimes. I appreciate any help in advance.

/*Custom Font for The Simpsons*/
@font-face{
font-family: 'TheSimpsons';
src:url('font/SimpsonsFont.otf');
}

/*Styling for "HOME" page*/
body{
margin: 0;
background-color: grey;
}

/*Styling for 'title'*/
.titulo{
top: 0;
position: absolute;
background-color: yellow;
display: inline-block;
position: relative;
margin: 0;
width: 100%;
background-image: url(img/clouds.jpg);
text-align: center;
background-size: 40%;
}
.titulo h1{
font-family: TheSimpsons;
color:yellow;
text-shadow: 2px 2px black;
}

/*Styling for the NavBar*/
.navbar{
position: fixed;
width: 100%;
background-color: yellow;
border-bottom: solid 2px black;
}
.navbar ul{
  list-style-type: none;
  margin: 0;
  padding: 0.5%;
  position: center;
  font-family: TheSimpsons;
  font-size: 18px;
  text-align: center;
}

.navbar li{
display: inline;
padding:2px 20px 2px 20px;
}

.navbar a{
color: black;
text-decoration: none;
}
<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
<title>The Simpsons</title>
</head>

<body>
<div class="titulo">
<h1>The Simpsons</h1>
</div>

<div class="navbar">
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Locations</a></li>
<li><a href="#">Characters</a></li>
<li><a href="#">Series</a></li>
<li><a href="#">Movies</a></li>
</ul>
</div>
</body>

</html>

Answer №1

Here's a Quick Solution:

To fix the issue with your navbar, simply update your CSS code like so:

.navbar{
    position: sticky;
    top:0;
    width: 100%;
    background-color: yellow;
    border-bottom: solid 2px black;
}

The problem was that you had set position:fixed, which caused the navbar to stay in place and create a gap at the top when scrolling.

I hope this solution clears things up for you.

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

Ensuring Valid Submission: Utilizing Jquery for a Straightforward Form Submission with 'Alajax'

After searching for a straightforward JQuery plugin to streamline the process of submitting forms via Ajax, I stumbled upon Alajax. I found it quite useful as it seamlessly integrates into standard HTML forms and handles all the necessary tasks. However, I ...

Connect my php search outcomes to a different webpage

Hello, I am new to the world of php and I have written a search result function that allows users to click on items. However, I am facing an issue with making a specific search item clickable to open in another page with full details. Below is the code I h ...

Notifying a Child Component in React When a Props-Using Function Resolves a REST Call

When I submit an item or problem, I trigger a function in the parent component that has been passed down to the child component as props. After sending my information, I want to clear the input fields. The issue is that I am clearing the input fields immed ...

How can you integrate jquery ajax in WordPress?

Recently, I started learning about jquery and have been following a tutorial on creating instant search using jquery. The tutorial can be found here. Now, I am trying to implement this on my WordPress site, but it seems like things work differently when d ...

Problem encountered while generating a torus shape using webGL

I am currently developing a program that aims to create 3D parametric shapes using webgl. The current code I have works successfully for rendering a sphere, but when I try switching the equations for a torus, only the upper half of the torus is being displ ...

Guide to inputting text into the Dev element

I am encountering a challenge with automating gist creation on GitHub. The issue arises when trying to input text into the gist content-box (see attached image). Despite attempting to use Javascript executer, I have not been successful. JavascriptExecutor ...

Refresh the CSS inheritance and hover effects

Facing an issue with my web project where I am using CSS. At times, I need to reset the inheritance from the parent class for certain elements. Although I have defined some classes to style my elements, I want to completely reset all styles except for hove ...

The function auth.createUserWithEmailAndPassword is not recognized in the context of React

base.jsx: import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const firebaseConfig = { config }; export const app = initializeApp(firebaseConfig); export const auth = getAuth(); Register.jsx ...

Creating collapsible items in Bootstrap

When using the standard bootstrap collapse tool, I encountered an issue: <div class="accordion" id="accordionExample"> <div class="card"> <div class="card-header" id="headingOne"> ...

Having trouble with SCSS styles not being applied after refactoring to SCSS modules?

Currently, I am in the process of restructuring an application to ensure that component styles are separated from global styles using CSS modules. However, I have come across an issue where the styles are not being applied correctly. The original code sni ...

What could be causing the misalignment between the desired output and the response from the AJAX request

Below is a basic JavaScript file I am working with: $.ajax({ url: "is_complete.php", type: "post", success: function (data) { if(data == 1) { } alert("ok") } }) The message "ok" will only be di ...

Objective-C and the World of WebSockets

Possible Duplicates: Comparison of WebSockets, TCP/IP, and JavaScript/AJAX for iPhone chat Integrating WebSockets into a Cocoa application Hello all, our team is embarking on creating a bespoke iPhone chat app and deliberating the use of WebSocket ...

Is there a way to eliminate the default arrow on a list input and replace it with a custom arrow solely with html and css?

Is there anyone who can help me figure out how to remove the default arrow in the list input field and replace it with a new arrow icon? <body> <form > <label for="data-list">Choose a browser from this list &l ...

Is there a way to execute two files concurrently in JavaScript using node.js?

I'm a beginner in the world of Javascript and Node.js, and I've encountered some issues while trying to test code I recently wrote. Specifically, I am attempting to test the code within a file named "compareCrowe.js" using another file named "tes ...

What is the best way to ensure the header spans the entire width of a webpage using the display: flex; property and flex-direction: column styling within the body element

Click here for the CSS code snippet Hello everyone, this is my very first query on Stack Overflow. I know it might be a simple question but it's my first time posting here so kindly bear with me. Below is the CSS code snippet provided: *{ ...

Non-functioning Tooltips on Google Charts

Currently, I am working on integrating Google Chart with ASP.NET and linking it to a SQL Server database. I have encountered an issue while attempting to customize the tool tip. Below is the Header Code: <script src="js/jquery/jquery-1.10.2.js" type=" ...

Transforming a JavaScript variable into a PHP variable

Is there a way to convert a JavaScript variable obtained from videoel.getCurrentTime function into a PHP variable, so that it can be included in an SQL Insert Query like INSERT INTO tblData VALUES ('$phpVarFromJavascript')? ...

Prevent users from adding or removing any letters

Exploring the ACE Editor integration with AngularJS (utilizing UI-Ace). I have a question. Is it possible to limit the user's actions to: Only allow entering a predefined character (for example, ;) Prevent deletion of any characters except for the p ...

When I update URLs in Django, my CSS breaks down

Recently, I encountered an issue on my website and found a solution by creating two separate HTML pages. However, when I modified the urls.py to differentiate the URLs for these pages, the CSS stopped working. Below is my code snippet and a detailed explan ...

What is the best way to retrieve data from divs that are nested within another element using Javascript?

I am currently working on implementing a modal that will showcase specific information based on the event-card it originates from. Here is an HTML snippet showcasing an example event-card: <div class="event-card upcoming hackathon"> <d ...