How can you fix a navbar that won't stay in place?

I'm facing an issue with this example:

When I scroll, the navbar moves along due to the fixed property, which affects the overall look. How can I make it disappear when scrolling, while keeping the logo intact?

Can someone suggest if this layout is suitable for a beginner and point out any other considerations I should keep in mind? I want to have random widgets on the right side and content on the left side.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test Design
</head>
<style>
/* Body style */
body {
margin: 0;
padding: 0;
font-size: 12px;

}

/* Logo */
.header {
position: relative;
margin: 0;
padding: 0;
display: block;
height: 50px;
background: #fff !important;
padding: 8px 16px;
}
.logo-1 {
font-family: Helvetica, "serif";
text-decoration: none;
font-size: 37px;
letter-spacing: 3px;
font-weight: 900;
color: #555555;
display: block;
}

/* Navigation */
.navbar {
position: fixed;
width: 100%;
background: #333333;
list-style-type: none;
text-decoration: none;
margin: 0;
padding: 0;
text-align: center;

}

li {
float: left;
}

 li a {
display: block;
padding: 14px 16px;
color: #eee;
text-decoration: none;
text-align: center;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, "sans-serif";
}

 li a:hover:not(.active) {
background: #111111;
}

 .active {
background-color: #008cba;
}

/* Widget */
.widget {
position: relative;
bottom: -42px;
float: right;
overflow: hidden;
width: 190px;
border-left: 1px solid #9fa2a9;
display: block;
padding: 2px 5px;
}

</style>
<body>
<div class="header"><a class="logo-1" href="#">Test Design</a></div>
<!-- Navigation -->
<ul class="navbar">
<li><a class="active" href="#">Home</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li style="float: right"><a class="active" href="#">Register</a></li>
<li style="float: right"><a href="#">Login</a></li>
</ul>
<!-- Widget -->
<div class="widget">

<b><u>Random Lorem Ipsum Text</u></b>
<form action="#" method="post">
    <div>
         <label for="name">Text Input:</label>
         <input type="text" name="name" id="name" value="" tabindex="1" />
    </div>

    <div>
         <h4>Radio Button Choice</h4>

         <label for="radio-choice-1">Choice 1</label>
         <input type="radio" name="radio-choice-1" id="radio-choice-1" tabindex="2" value="choice-1" />

     <label for="radio-choice-2">Choice 2</label>
         <input type="radio" name="radio-choice-2" id="radio-choice-2" tabindex="3" value="choice-2" />
    </div>

  <div>
    <label for="select-choice">Select Dropdown Choice:</label>
    <select name="select-choice" id="select-choice">
      <option value="Choice 1">Choice 1</option>
      <option value="Choice 2">Choice 2</option>
      <option value="Choice 3">Choice 3</option>
    </select>
  </div>

  <div>
    <label for="textarea">Textarea:</label>
    <textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
  </div>

  <div>
      <label for="checkbox">Checkbox:</label>
    <input type="checkbox" name="checkbox" id="checkbox" />
    </div>

  <div>
      <input type="submit" value="Submit" />
    </div>
</form>
</div>
</body>
</html>

Answer №1

.top-menu {
    position: fixed;
    background-color: #333333;
    list-style-type: none;
    text-decoration: none;
    margin: 0;
    padding: 0;
    text-align: center;
}

Adjusting the placement of the top menu -

Answer №2

Check out this CSS snippet

The issue here lies in floating the li elements to the left, which causes the parent ul to collapse in height. To maintain its height, a clearfix is needed.

CSS Styling

body {
        margin: 0;
        padding: 0;
        font-size: 12px;

    }

    /* Logo */
    .header {
        position: relative;
        margin: 0;
        padding: 0;
        display: block;
        height: 50px;
        background: #fff !important;
        padding: 8px 16px;      
    }
    .logo-1 {
    font-family: Helvetica, "serif";
    text-decoration: none;
    font-size: 37px;
    letter-spacing: 3px;
    font-weight: 900;
    color: #555555;
    display: block;
    }

    /* Navigation */
    .navbar {
        width: 100%;
        background: #333333;
        list-style-type: none;
        text-decoration: none;
        margin: 0;
        padding: 0;
        text-align: center;

    }

  .clearfix::after{
    content:"";
    display:table;
    width:100%;
    clear:both;
  }

    li {
        float: left;
    }

     li a {
        display: block;
        padding: 14px 16px;
        color: #eee;
        text-decoration: none;
        text-align: center;
    background-color: #212121;
        font-family: Gotham, "Helvetica Neue", Helvetica, Arial, "sans-serif";
    }

     li a:hover:not(.active) {
        background: #111111;
    }

     .active {
        background-color: #008cba;
    }

    /* Widget */
    .widget {
        position:relative;
        bottom: -42px;
        float: right;
        overflow: hidden;
        width: 190px;
        border-left: 1px solid #9fa2a9;
        display: block;
        padding: 2px 5px;
    }

Link for more details Hope this explanation helps you solve your problem!

Answer №3

/* Custom CSS for Navigation Bar */

.navbar {
        position: fixed;
        width: 100%;
        background-color: #333333;
        list-style-type: none;
        text-decoration: none;
        margin: 0;
        padding: 0;
        text-align: center;
    }

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

What is the maximum amount of information that can be stored in a data attribute within the DOM?

Occasionally, I find myself wanting to include a substantial amount of data on the webpage in order to minimize additional AJAX calls for dynamic content. However, I am concerned about potential performance implications. Is there a penalty I should be aw ...

In what way is the JavaScript file able to locate the dependencies following the execution of npm install?

Upon receiving a javascript file named "neededjavascriptfile.js" along with a package.json file, I am faced with the task of running it in an html index file while utilizing certain functions from the javascript file. After executing "npm install --omit=d ...

Flexbox: Arrange header and footer in a sidebar layout using flexbox styling

In my content layout, I have structured the sections as header, content, and footer. While I want to maintain this order for small devices, I am looking to shift the header and footer to the sidebar on desktop. I have already implemented this using floats ...

Is there a feature similar to Google/YouTube Insight called "Interactive PNGs"?

Recently, I have been exploring the YouTube Insight feature and I am intrigued by how their chart PNGs are generated. When you view the statistics for a video, the information is presented in interactive PNG images. These images seem to be entirely compos ...

Align the prices of products in a uniform position within each table cell

Apologies for the lengthy explanation. I am working with osCommerce and have a page that displays all our product specials in a 3 column table layout. My challenge is to align the price of each product so that they are aligned perfectly across the screen. ...

Angular-ui does not support the ng-disable directive

<h3 class="pulse-green-text"><span class="icon ico_pulse_download"></span>VPN Certificate</h3> <div class="vpn-cert" ng-controller="vpnController vpnCert"> <form method="post" name="userform" class="form-horizontal" id="u ...

Is there a way to incorporate the ::after or ::before pseudo-elements into an image tag?

While working on my project, I attempted to incorporate the ::after tag (I had used ::before as well) for an image within an img tag. However, I am facing difficulties as it doesn't seem to be working. Any advice or guidance would be greatly appreciat ...

Tips on how to properly format a DateTime String

I need help with formatting a DateTime string retrieved from an API where it is in the format of YYYY-MM-DDTHH:MM:SS +08:00 and I want to change it to DD-MM-YY HH:MM getDataFromApi(res) { this.timestamp = this.timestamp.items[0].timestamp; console ...

No need for jQuery with this scrolling image gallery

I recently created a horizontal scrolling image gallery with thumbnails underneath. The goal is to click on a thumbnail and have the corresponding image scroll into view. Here's the HTML setup: <div class="images_container"> <img id="imag ...

Maximizing values entered into form fields

Looking for a way to extract the highest number from a set of input fields in an HTML form using JavaScript? Take this example: <input id="temp_<strong>0</strong>__Amount" name="temp[<strong>0</strong>].Amount" type="text" valu ...

Python code encountered an issue while trying to find the element with the locator //input[@name="session[username_or_email]". The element could not

I have recently started learning about selenium and web scraping in general, and today I attempted to follow a tutorial on selenium that included the following command: from selenium import webdriver driver = webdriver.Firefox() driver.get("https://t ...

Incorporate npm libraries into vanilla JavaScript for HTML pages

Attempting to incorporate an npm package into plain JS/HTML served using Python and Flask. <script type="module"> import { configureChains, createClient } from "./node_modules/@wagmi/core"; import { bsc } from "./nod ...

What is the source of the width for this expansive dropdown menu?

I can't seem to crack this puzzle. When you navigate to the following link: click here, and select 'Accordian' from the topbar menu, where exactly does the dropdown menu width originate from? Upon inspecting it, I only see a computed value w ...

Faux CSS, every other moment appears to be unsuccessful

My HTML code looks like this: <div class="container"> <div class="foo">Foo!</div> <!-- if this is red... --> <div class="bar">Bar!</div> </div> <div class="container"> <div class="foo">Foo! ...

Ways to revert all modifications to styles implemented using JavaScript

Hey there, just checking in to see how you're doing. I was wondering if there's a way to reset all the styles that have been changed using JavaScript? I'm referring to the styles shown in this photo: Thanks in advance. ...

String casting for large JavaScript integers may require rounding to function properly

When trying to pass a large integer to a function from an onclick event in HTML, I always encounter issues with rounding. Despite using bigInt libraries, I still struggle to pass the full number accurately and would prefer a simple string casting method. ...

jQuery does not support iterating over JavaScript objects

Here is the code snippet I am working with: $(obj).each(function(i, prop) { tr.append('<td>'+ i +'</td>' + '<td>'+ prop +'</td>'); }); Intriguingly, the data in $(obj) appears as: Obje ...

Unable to eliminate the border gaps in the table

I'm having trouble getting the picture and grey area in the example attached to stay together and be the same height. The border-spacing seems to be causing an issue. Any suggestions on how to fix this? Apologies for the messy style sheet. Here' ...

What can I do to stop my list items from dropping below the menu even when there isn't enough space?

I'm facing an issue with my menu and despite several attempts, I can't seem to resolve it. In Firefox, everything looks fine, but in other browsers, the last anchor tag inside my menu keeps collapsing underneath. I've tried various solutions ...

Retrieve the name of the product from the corresponding parent element

My goal is to trigger an alert box with the product name when the "Buy now" button is clicked. I have added the necessary code in jquery to maintain the onclick event of the button. <h2 class="product-name"><a href="product1.php" title="Sample Pr ...