Conceal content in Bootstrap navbar when clicking on #link

I am facing an issue with the navbar-fixed-top element as it is causing content to be hidden from the container.

To resolve this common problem, you can add the following CSS code:

body { padding-top: 70px; }

After applying the above code, the container is no longer hidden by the navbar when the page is loaded. However, an issue arises when trying to navigate to a specific item on the page using a href=#item. In such cases, the item remains hidden behind the navbar.

I have created a simplified example on Codeply to demonstrate this problem. When clicking on "Got to test3", the item

<h2 class="font-weight-light">TEST3</h2>
gets hidden by the navbar.

Below is the code snippet:

<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
HELLO NAVBAR
</nav>
<div class="container py-2">
    <div class="row">
        <div class="col">
            <div class="sidebar-sticky" id="menu">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a href="#test">Go to test1</a>
                    </li>
                    <li class="nav-item">
                        <a href="#test2">Go to test2</a>
                    </li>
                    <li class="nav-item">
                        <a href="#test3">Go to test3</a>
                    </li>
                    <li class="nav-item">
                        <a href="#test4">Go to test4</a>
                    </li>
                </ul>
          </div>
        </div>
        <div class="col">
            <div id="test">
                <h2 class="font-weight-light">TEST1</h2>
                <p>
                    This is a Bootstrap starter example snippet.
                </p>
                <br/><br/><br/><br/><br/>
            </div>
            <div id="test2">
                <h2 class="font-weight-light">TEST2</h2>
                <p>
                    This is a Bootstrap starter example snippet.
                </p>
                <br/><br/><br/><br/><br/>
            </div>
            <div id="test3">
                <h2 class="font-weight-light">TEST3</h2>
                <p>
                    This is a Bootstrap starter example snippet.
                </p>
                <br/><br/><br/><br/><br/>
            </div>
            <div id="test4">
                <h2 class="font-weight-light">TEST4</h2>
                <p>
                    This is a Bootstrap starter example snippet.
                </p>
                <br/><br/><br/><br/><br/>
            </div>
        </div>
    </div>
</div>

Answer №1

Presenting the answer

 body { 
     padding-top: 70px; 

 }

 #example{

     padding-top:90px;



 }

 #sample{
      padding-top:90px;

 }


  #demo{
      padding-top:90px;

 }


  #trial{
      padding-top:90px;

 }

Answer №2

To address the current situation, consider incorporating a padding style to the div elements that are immediately followed by a h2 tag (such as "Test3"). By including div h2 { padding-top: 70px; }, you can achieve this within your existing layout. However, for future-proofing your project and avoiding reliance on specific HTML structures, it may be beneficial to establish a unique class that governs the padding-top rule and apply it selectively to elements where necessary. I hope this explanation proves helpful.

Answer №3

In response to the question raised in Fixed page header overlaps in-page anchors, it was discovered that the solution did not lie within the accepted answer but rather within alternative answers provided. Therefore, I am reiterating the solution below:

To resolve the issue, a specific CSS class was added into the stylesheet:

.hreflink::before {
  content: "";
  display: block;
  height: 70px; /* height of fixed header */
  margin: -70px 0 0; /* negative offset equal to fixed header's height */
}

Subsequently, each element targeted for navigation was assigned this newly created class. For instance:

<div id="test3" class="hreflink">

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

When the left/top/right/bottom properties are not specified, using position:fixed produces the desired results in Firefox and Internet Explorer, but not in Safari

It's unfortunate that I have to bring up another question related to position:fixed, but after going through various other questions and forum threads, I'm still not clear on this particular issue. The code below is a simple illustration of how ...

Encountering an error: [nsIWebProgressListener::onStatusChange] when utilizing jQuery AJAX within a click event?

Greetings! I am currently learning how to implement AJAX with jQuery to load an HTML document into a div element within another HTML document. Here is the approach I am using: function pageload() { $.ajax({ url: 'Marker.aspx', ...

Having difficulty changing placeholder text style in Scss

I'm a newcomer to SCSS and I'm attempting to customize the color of my placeholder text from light gray to dark gray. Below is the HTML code snippet: <div class="form-group"> <textarea class="thread-textarea" ng-maxlength="255" ma ...

Exploring Google Go Templates for Creating Dynamic Websites using Multidimensional Arrays

In my struct example, I have a two-dimensional array: type Foo struct{ testArray[9][9] int } My goal is to access it using a template. For instance: tmpl.Execute(w, foo) //foo is a pointer to Foo struct and w represents the parsed html website How can ...

In search of a mobile web UI framework solely built with CSS styling

Currently in search of a CSS framework that specializes in styling form elements and lists for optimal small screen use. The ideal framework must be CSS only, requiring minimal to no JavaScript. Something along the lines of Twitter Bootstrap would be per ...

adjustable text size in web view interface

When loading the following html string in a uiwebview, I am trying to set the font-size as a variable value: NSString * htmlString = [NSString stringWithFormat:@"\ <html>\ <s ...

What is the best way to display grouped items horizontally in an ASP.NET ListView?

My ASP ListView currently displays items vertically in columns with GroupItemCount set to 3: A D G B E H C F I Is there a way to adjust the display so that it groups items horizontally in rows like this? A B C D E F G H I This is the ListVi ...

What's the reason this picture isn't displaying in its full size?

I am having trouble adjusting the size of this image to fit the full width, but there seems to be some remaining space on the right side. Category: CSS/HTML /* Header */ .header { background: url('https://picsum.photos/1920/1080') center ...

Modifying the background image of div elements with JQuery in a loop function is ineffective when using Google Chrome

I am facing an issue in my application where I have a for loop to change the background image of two divs. The code snippet is as follows: for (var i = 0; i < length; i++) { $("div_" + (i + 1)).css("background-image", "../imageFile.png"); ...

Exciting transition effect for font sizes in CSS

Issue: Hover over the X and wait for the div to collapse completely. Move away from the X Notice how the div jumps to the bottom and then back up. Inquiry: How can I prevent text wrapping while expanding the div during animation? (Currently using del ...

Tips for designing a button that can execute a JavaScript function repeatedly

My goal is to rotate the numbers clockwise when the rotate button is clicked. I have included all the relevant code. I have created a button that triggers a javascript function when clicked. The problem is that the function only rotates the numbers once. ...

Clicking on the dropdown in Bootstrap 4 will cause the arrow to toggle

Is it possible to change the default caret in Bootstrap 4 to an upward arrow when a dropdown is clicked? <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspo ...

When the value in a required input field is empty, the border is activated and the color is styled

Can you please explain this to me? Try running this code in Firefox: http://jsfiddle.net/eMa8y/24/ Here is the HTML: <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> </head> ...

Switch effortlessly between one animation and another with Angular.js

I have two animations named "prev" and "next". Upon clicking the prev button, the "prev" animation should play. Similarly, when you click on the "next" button, the "next" animation should be triggered. I am using ng-class to achieve this behavior. How can ...

Are HTML5 Track Element Cue Events Working Properly?

My goal is to dynamically assign functions to HTML5's cue.onenter events. This feature is relatively new and currently only supported in Chrome with specific flags enabled (Refer to the example on HTML5 Rocks here). However, I seem to be facing some ...

Intersecting interactions: Mouse events on flex boxes

I am facing a challenge in my React application where I have two panels with buttons displayed using flex properties. Below is a simplified version of the HTML/SCSS code that illustrates the setup: <div class="bottom-panel left"> <butt ...

vue-router: A guide to disabling the outline property of the CSS in router-link

I am having trouble disabling the outline of a css link element. I successfully disabled the outline of a regular link using a css class, but I am unable to do so with the <router-link> element. How can I disable the outline for this? .page-link, .p ...

Using jQuery to update the input value when the mouse hovers over a div

Attempting to update an input value using jQuery mouseover. Situation: There are 5 divs with different colors and usernames. When hovering over a div, the input text (and background color for the color input) changes based on database values. Each time a ...

Most efficient method for dynamically changing HTML content with JavaScript

Can anyone provide guidance on how to switch HTML content using JavaScript? For example: if($sid == 0) {insert one HTML code} else {insert another HTML code} Which method is preferable - LESS, jQuery, CSS3, etc? ...

Adding a logo to a website that utilizes CSS, HTML, JS, and Bootstrap

Hey there! I'm new to the world of Front-end development and I've been learning by watching videos and reading everything I can find on the Internet. Currently, I'm using HTML, CSS, and JS for my website. Can anyone help me figure out how to ...