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

How can two unique links toggle the visibility of divs with two specific IDs?

I am developing an interactive questionnaire that functions like a 'create your own adventure' story. The questions are shown or hidden depending on the user's responses. Below is the HTML code: <!-- TIER 3 ============================== ...

I am looking for assistance constructing a task management application using vue.js

I am facing an issue with developing a to-do list using Vue.js. The goal is to add tasks to the list by entering them and clicking a button. Once added, the new task should show up under "All Tasks" and "Not finished". Buttons for marking tasks as " ...

Tips for transferring simple CSS formatting to an independent stylesheet

As part of a university project, I am working on a basic website. Initially, I used in-line styling for the website design. However, the subject coordinator has now changed the requirements and we are only allowed to use an external CSS stylesheet. Most of ...

Bootstrap columns are still disrupted by large images even when they have reached their maximum width and height

I have been using bootstrap templates that allow users to create displays with images and text. I previously added max-width:100% and height:auto to the img CSS, thinking it would resolve issues with large images breaking the container. However, when inse ...

Tips for sending the output of a Python function to the webpage associated with the clicked action button in Python's Bottle framework

Here is a method I have: @post('/home', method='post') def myFunction(): if(len(request.forms.get('Matrix_dimension').strip()) != 0): length = int(request.forms.get('Matrix_dimension').strip()) tableRow = ...

Locate specific text within the content and apply an underline to it

Is there a way to locate specific text on my website and apply an underline to it? Suppose I have some text displayed and I wish to identify all instances of the word hello and underline them. Here is my attempt at the code: $( "body:contains('hell ...

When the open button is clicked, the Div will toggle between open and closed states

Recently, some of my questions have not been well-received, which makes me feel a bit disheartened. It's important to remember to be kind when providing feedback. I've noticed that some people downvote without offering constructive criticism, whi ...

Exploring the Benefits of jQuery History Plugin, Optimizing with Google Sitemap and

Despite searching online, I have yet to find a precise solution to my query. On my index.php page, there are two specific DIV's: #post-title and #post-content, whose content is dynamically loaded through a jQuery function triggered by clicking a link ...

Spartacus 3.3 - Unleashing the Full Potential of the Default Sparta Storefront Theme

Looking for advice on how to customize the default Spartacus Storefront theme (Sparta) by only overriding specific CSS vars. Any suggestions? I tried following the instructions provided at: https://github.com/SAP/spartacus/blob/develop/projects/storefront ...

Django auto-fill using the primary key on the page

Is there a way to automatically select the id or (pk) of an employee's page when adding documents to their profile through a form? Any suggestions for a solution would be greatly appreciated! view.py def createDocument(request): forms = documentF ...

Leveraging strings as URLs to embed PDFs in Wordpress using PDF Embedder

I'm encountering an issue related to a Wordpress plugin called PDF Embedder, as well as concatenating/using a string with document.write. My goal is to make this code work: <script src="http://nooze.org/wp-content/uploads/scripts/dateGetter.js"> ...

Phonegap - Retaining text data in a checklist app beyond app shutdown

This is my first time developing an app with Phonegap. I am looking to create a checklist feature where users can input items into an input field. However, I am struggling with figuring out how to save these items so that they remain in the checklist even ...

Confirm that only the days of the present month are eligible for selection

I have been given the responsibility of validating a date field that is populated when an invoice is created. The date field consists of a text box and three button objects allowing users to select a date from a calendar, input today's date, or remove ...

What is the best way to use PHP in order to retrieve the element containing the visit ID from the specific row within an HTML table?

I am working on a project that involves populating an HTML table with data from a database. The table includes an approval button for administrators to approve visits and change their status to APPROVED. I am trying to figure out how to select the element ...

The functionality of CSS scroll snap does not seem to be compatible with the CSS Grid

When utilizing CSS scroll snap with Flexbox, the snapping functionality works adequately: * { box-sizing: border-box; margin: 0; padding: 0; } .slider { font-family: sans-serif; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vw ...

Checking boxes on Android for compatibility with Firefox, Chrome, and Internet Explorer 8

Could anyone assist me with finding Android checkboxes styled with CSS and/or jQuery for my project? I haven't been able to find any resources or plugins that could help. Any links you can provide would be greatly appreciated as I've come up empt ...

Printing content from an Angular dashboard can be achieved by following a few

Currently, I am working on developing a legal document automation program for my company. However, I have encountered an issue during the final stages of completing this web application. For the layout and setup, I am using the default angular Dashboard l ...

What is the best way to embed a video and playlist onto a webpage using HTML5?

After switching from the flash-based JWPlayer 4 to JWPlayer 5 with HTML5 support, I noticed that while the player degrades gracefully on mobile devices without Flash but with HTML5 support, it breaks when the playlist option is enabled. Can someone please ...

Displaying images above a Bootstrap Carousel

I am looking to create a dynamic mobile image slider using Bootstrap carousel with overlaying images. However, I am facing two issues with responsive resizing and the overlapping of sections due to absolute positioning. <section> <h2 style= ...

Tips for deactivating all JavaScript events on a webpage that has been loaded within an iframe

My website is equipped with various JS click/mouseover/mouseout/scroll events and incorporates several JS libraries to operate features such as carousels, bootstrap modal pop-ups, and more. I am looking to halt all events on a specific element. Although I ...