Enhance your website navigation with Bootstrap Scrolling Nav: maintain consistent section heights and highlight the active

  1. Can you help me with an issue I'm having when clicking on the <a> link in the navigation? The anchor point seems to be misplaced as the section headline (<h2>) is not visible.
  2. Is there a way to highlight the <a> links, perhaps by adding a border or something similar?

Below is the HTML code causing this issue:

<!-- Fixed navbar -->
<nav id="navbar" class="navbar navbar-default">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="page-scroll"><a href="#">Home</a></li>
        <li><a class="page-scroll" href="#ark">Architektur</a></li>
        <li><a class="page-scroll" href="#">Wohnen</a></li>
        <li><a class="page-scroll" href="#ausstattung">Ausstattung</a></li>
        <li><a class="page-scroll" href="#lage">Lage</a></li>
        <li><a class="page-scroll" href="#">Galerie</a></li>
        <li><a class="page-scroll" href="#kontakt">Kontakt</a></li>  
        </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>
<!-- === END NAV === -->

UPDATE:

See the Navigation Problem Here

Answer №1

Regarding your initial query, here is the code snippet that includes highlighting for <a></a>:

HTML

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Clients</a></li>
    <li><a href="#">Contact Us</a></li>
  </ul>
</nav>

CSS

li{
  padding: 8px;
}

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

a:hover, a:focus{
  color: red;
  text-decoration: none;
  outline: none;
}

.active{
  font-size: 30px;
  color: red;
  border-bottom: 5px solid red;
}

jQuery

$(document).ready(function(){
    $('a').click(function(){
        $('a').removeClass("active");
        $(this).addClass("active");
    });
});

Check out the live demonstration on:

JSFiddle

Answer №2

  1. One issue is that the fixed navbar overlaps your heading, causing it to appear incorrectly. To fix this, you can adjust the top position of the anchor point by adding an extra 50px or so.
  2. To solve this problem, you can create a class called "active" or "selected" with the desired styles:
    selected{
        background-color: silver;
        color: #fff;
    }

var links = $(".page-scroll"); 
links.on('click',function(){ 
  links.removeClass("selected"); 
  $(this).addClass("selected"); 
});
li{

  list-style-type: none;
}
.page-scroll{
  float:left;
  padding:15px;
  text-align: center;
  background-color: black;
  color: #fff;
}

.selected{
  background-color: silver;
  color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="navbar" class="navbar navbar-default">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li><a class="page-scroll" href="#">Home</a></li>
        <li><a class="page-scroll" href="#ark">Architektur</a></li>
        <li><a class="page-scroll selected" href="#">Wohnen</a></li>
        <li><a class="page-scroll" href="#ausstattung">Ausstattung</a></li>
        <li><a class="page-scroll" href="#lage">Lage</a></li>
        <li><a class="page-scroll" href="#">Galerie</a></li>
        <li><a class="page-scroll" href="#kontakt">Kontakt</a></li>  
        </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

In addition, don't forget to apply this class when clicking on the anchor link in the navbar

Answer №3

When troubleshooting problem number 1, the solution entails adding the following snippet to your CSS file (the first three lines are simply comments and can be omitted):

/* RESOLVES issue:
Navbar conceals initial content when navigating to in-page anchor #1768
https://github.com/twbs/bootstrap/issues/1768 */

*[id]:before { 
    display: block; 
    content: " "; 
    margin-top: -75px; 
    height: 75px; 
    visibility: hidden; 
}

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

Difficulty Communicating Recapcha 2 with the PHP Script

I am currently testing the installation of reCaptcha 2 with server-side verification using a form with one input field. My process involves sending the reCaptcha response via Ajax to a PHP page for verification. While I am able to capture the information p ...

Exploring Selenium WebDriver: Manipulating Table Elements and Iterating Through Rows

Is there a more efficient way to iterate through tables row by row without using Absolute Xpath? In my test case, I need to search for specific data in each row. This was the previous logic of my code: private static int iRow; WebElement sWorkUnitRows = ...

Is there a way for me to align my div with my header on the same line?

HTML: <h2> RAN shares false news story about Hershey's Oil Commitment</h2> <div class="date"> <div class="number">27</div> <div class="month">Oct</div> </div> <p>The Rainforest Action Netwo ...

Root location for offline and pre-production in the Backbone boilerplate router

I am currently utilizing the backbone boilerplate found at https://github.com/tbranyen/backbone-boilerplate My development process involves working on static HTML/JS files offline and conducting tests offline before deploying them to another site for pre- ...

What is the best way to incorporate a logo container and a horizontal divider into my top navigation bar using Bootstrap?

I'm currently working on designing a top navigation bar that features a logo with color #1 as the background on the left side. The logo is then separated by a grey horizontal divider, followed by color #2 representing the application name beside the d ...

The z-index property does not seem to apply to pseudo elements

I am facing an issue while trying to add a pseudo element after a div in my react project. Surprisingly, the code works perfectly fine in CodePen but fails to work on my local machine. I have applied z-index only in CodePen, which seems to resolve the issu ...

How to customize TextField error color in Material-UI using conditional logic in React

Currently, I am incorporating the React Material-UI library into my project and faced with a challenge of conditionally changing the error color of a TextField. My goal is to alter the color of the helper text, border, text, and required marker to yellow ...

Unable to output the string using htmlspecialchars

Oops! I made a mistake... just came across a JavaScript alert box! I just included htmlspecialchars. $src = './imgs/flag.jpg'; echo sprintf("<img alt=\"'.htmlspecialchars($name).'\" src=\"%s\" />", $src); I ...

Issue with font icons not displaying properly on Firefox

In Firefox, the icon is not displaying in the center, but it works fine in Opera. body { height: 100vh; display: grid; place-items: center; } .link { background-color: red; padding: 3px; display: grid; place-items: center; ...

Content within the p tag is failing to wrap onto separate lines

Take a look at the image below: Here is the code causing issues: <div class="excerpt"> <p>Manual for downloading and installing digital content from the Educational Canaima Project, comprised of learning resources that aim to promote interac ...

What is the best way to change the status of a disabled bootstrap toggle switch?

I'm working with a read-only bootstrap toggle that is meant to show the current state of a system (either enabled or disabled). The goal is for it to update every time the getCall() function is called. However, even though the console logs the correct ...

"Unlocking the power of your device's microphone with

Hello, I'm wondering if it's possible to utilize a device's microphone through trigger.io. Perhaps by integrating the native plugin functionality and creating a wrapper for microphone access using a forge native API, or exploring alternative ...

Display flash messages consistently in a designated area on the webpage instead of appearing at the top of the page in Flask

{% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} <div class="alert alert-{{ category }}" style=" ...

What is the best way to implement JavaScript for loading and removing content based on button clicks on a webpage?

I'm in need of a vanilla JavaScript solution (I know JQuery options exist, but I want to stick to vanilla JS for now)? Currently, I am using a simple page as a testing ground for my ongoing project. The page consists of two buttons that load HTML pag ...

Ensure that the "select" dropdown menu remains a constant size

One section of my Android app, powered by JQuery Mobile, displays a table with a combobox. The issue arises when selecting the option with a very long text, causing the combobox to expand and show half of the table off-screen. My goal is to set the combob ...

Enlarging the current division while reducing the size of the others when hovering

Currently, I am working on a school project that involves creating a slideshow on a webpage. However, I have reached a point where I am unsure of how to proceed. Here is the progress I have made so far: body { background-color: #252525; } #wrapper { ...

Attempting to create a child process within the renderer by triggering it with a button click

I'm currently developing an electron application where I am attempting to initiate a child node process (specifically to run a Discord.JS bot). Below is the code snippet in question: index.html: <tr> <th class="title-bar-cell" ...

Grid alignment issue with images on Bootstrap 4.0 - resolution needed

Currently, I am working on a mini project that involves recreating a website using Bootstrap 4. I am at a stage where I need to display three images in a grid format with two on the top and one on the bottom. The challenge is to make the top two images eve ...

The submit button on the HTML form is not functioning properly and requires activation

Currently, I am a student focusing on honing my skills by working on an app for a blog post. To kickstart my project, I downloaded the "clean-blog" template from the startbootstrap website. If you are interested, here is the link to the template: My curr ...

Title of the most recently created file on GitHub

Being new to web designing and javascript, I am seeking help in understanding how to display the latest PDF file in my small website hosted on GitHub. Despite successfully displaying a PDF file, I encountered difficulties when attempting to showcase the mo ...