Updated navigation bar and optimized for mobile devices

I'm struggling to get my navigation menu to stick at the top and be responsive on mobile with a toggle bar icon. I'm using Bootstrap 4, and I've tried using sticky-top and fixed-top in the div classes, as well as position fixed in my header CSS, but it's not working properly.

Here's the code snippet:

header {
  border-bottom: 1px solid pink;
}

.nav {
  font-family: 'Lato', sans-serif;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 3px;
  padding: 5px;
}

.nav-link {
  color: black;
  margin-right: 20px;
  margin-left: 20px;
}

.dropdown-menu {
  font-family: 'Lato', sans-serif;
  font-size: 12px;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 3px;
  border: 0px;
}

.dropdown-menu-center {
  left: 50% !important;
  right: auto !important;
  transform: translate(-50%, 0) !important;
  margin-top: 30px;
}

.dropdown-menu a:hover {
  color: pink;
  background-color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!-- Nav menu -->
<header style="background-color:fff;">
  <div class="container">
    <div class="row justify-content-center">
      <nav>
        <ul class="nav">
          <li class="nav-item">
            <a class="nav-link" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Portfolio</a>
          </li>
          <div class="nav-item dropdown show">
            <a class="nav-link" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Services
  </a>
            <div class="dropdown-menu dropdown-menu-center" aria-labelledby="dropdownMenuLink">
              <a class="dropdown-item" href="#">Wordpress Design</a>
              <a class="dropdown-item" href="#">Add-Ons</a>
              <a class="dropdown-item" href="#">Branding</a>
            </div>
          </div>
          <li class="nav-item">
            <a class="nav-link" href="#">How To Start A Blog</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contact</a>
          </li>
        </ul>
      </nav>
    </div>
  </div>
</header>
<!-- End of nav menu -->
<div style="padding:1000px;">hi</div>

Answer №1

The issue lies in the fact that you have not loaded the necessary files.

Solution: Update your outdated jQuery with the following three files:

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

Edit:

After further examination, it appears that the navbar has been significantly altered, leading to its malfunction.

To rectify the situation, here is a fully functional example of a navbar with centered links. Utilize this as a foundation and gradually introduce your custom CSS modifications to prevent any disruptions. Also, take note of the mx-auto class utilized for horizontal centering of items ("mx" denotes margin-horizontal).

Below is the functioning code:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mx-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>
    </div>
</nav>
<!-- End of nav menu -->
<div style="padding:1000px 0;">hi</div>

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

Ways to prevent a background image from centering at a designated width

Is it possible to prevent a background image from centering at a specific width? To demonstrate the issue, try narrowing the width of the website linked below until scroll bars appear. Take note of how the background image stays centered while other eleme ...

What is the method for adding the square root symbol to a button's value?

I am attempting to display the square root symbol inside a button, but I am having trouble making it work. Below is my button code: <input type="button" id="sqrt" value="sqrt" onclick="insert function here"> For the value, I want the square root sy ...

Switch between divs using an update panel

Consider this scenario: Numerous div controls are present on an aspx page, and update panels are being used to prevent page refresh. These divs contain various controls like buttons and textboxes: <asp:UpdatePanel ID="UpdatePanel1" runat="server"> ...

What is the best way to add borders to table cells that have a non-zero value

I am trying to create a table using a function and it currently looks like this: table { border: 1px solid; } td { width: 30px; height: 30px; text-align: center; } <table> <tr> <td>2</td> <td>0</td> ...

Mastering CSS positioning and perfect alignment

I'm currently working on a website where I want the text to be centered on the page without scrolling. Although I have the container div centered, I'm struggling to align the text properly. It's crucial that h1 has a position: fixed because ...

Having issues with CSS list hovering functionality in Google Chrome

One of the challenges I'm facing with my HTML list is that when hovering over each list item, the text along with the bullet point should change color. Here's the structure: <ul> <li>A bullet point</li> <li>Another ...

Turn off scrolling but allow dragging on iPhone

Is it possible to disable scrolling but still allow dragging on the <canvas> element in iPhone? Currently, when you drag the <canvas>, the entire page also scrolls, which is not the desired behavior. <meta name="viewport" content="wid ...

Positioning of DIV elements in relation to each other

I'm currently enrolled in Codecademy's HTML & CSS course and I'm finding the topic of positioning a bit perplexing. Here is an example of my HTML file: <!DOCTYPE html> <html> <head> <style> div { pos ...

What advantages does avoiding the use of float:right offer?

On my website, I have two divs - one on the left side and the other on the right side. However, the client has requested that I do not use float:right. Instead, I have opted for margin-left. What are the advantages of avoiding float:right? Is it better to ...

Exploring IP geolocation integration within Rails 3

I am looking to add a dynamic feature to my homepage that will display the location object closest to the reader's physical location. Currently, I have a Location model with longitude and latitude fields. My goal is to retrieve the location model obj ...

What is the best way to ensure the sidebar occupies the entire page height?

Hey there! I am currently working on a Bootstrap page that includes some lorem text and a sidebar. However, I am facing an issue where the sidebar does not fill the screen vertically when the content on the page is smaller than the window size. Can someone ...

Angular 8: Implementing Form Validation with a Boolean Flag

Within my HTML code, I have a function (change)="limitUser($event)". In Typescript, I utilize a for loop to iterate through each element and determine if the value is less than 10. If it exceeds 10, the inValid = true condition is set. All form fields in m ...

By setting up a keydown event listener, I am unable to inspect HTML elements by using the F-12 key shortcut in Chrome

Recently, I encountered an issue where adding a keydown event listener in my JavaScript code prevented F-12 from working properly. window.addEventListener("keydown", function (event) { if (event.defaultPrevented){ retu ...

Importing images in React for CSS does not function properly

I've encountered an issue while trying to import an image for CSS. I successfully imported the image and used it in the img tag as src={furnitureBG} for testing purposes. Following the documentation, I imported the image and set the path as the value ...

Embed a data entry point into an existing picture

Looking for assistance inserting a field within an image for users to enter their email. Can someone provide the HTML code for this? Appreciate your help! ...

Easily done! Using JavaScript to generate a blinking cursor on a table

Working on a project to develop a dynamic version of the classic game Tic-Tac-Toe... However... Every table cell is displaying an irritating flashing cursor, almost like it's behaving as an input field. Any insights into why this is happening...? O ...

Upon calling the createModalAddPost() function, a single window is triggered to open

Hey there, I'm having a JavaScript question. So, I have a panel that opens a window, and it works fine. But the issue arises when I close the window and try to open it again - it doesn't work. I have to reload the page every time in order to open ...

Introducing a novel class designed to leverage the attributes of another class

Is there a way to use the @extend feature in CSS to inherit properties from one class to another? Imagine having two CSS files loading on a page in this order: <link rel="stylesheet" href="/css/one.css" media="screen"> <link rel="stylesheet" href ...

Is it possible to use CSS to swap out main menu items for submenu items?

I have created a basic vertical menu that looks like this: Home Information Info I am wondering if it is possible to replace a list item with its own sublist when the item is active. Specifically, I want to change Home to Sub1 and Sub2 when the item is ...

The intricate arrangement of multiple rows and columns using Bootstrap 4 technology

Struggling with multi-nested rows and columns in Bootstrap 4, I encountered an issue where the line2 was positioned next to line1 instead of below it with a horizontal bar separating them. Can someone help me understand why this is happening? <link h ...