Tips for Preventing Page Scrolling When Clicking on a Hashed A Tag in Content with a Fixed

Currently stuck dealing with a challenging problem related to hashed anchor links. Here's a basic representation of the HTML code:

<div class="main-wrap">
<header></header>

<aside>
    <nav>
        <ul>
            <li><a href="#article1">Article1</a></li>
            <li><a href="#article2">Article2</a></li>
            <li><a href="#article3">Article3</a></li>
        </ul>
    </nav>
</aside>

<section>
    <article id="article1">Content Here</article>
    <article id="article2">Content Here</article>
    <article id="article3">Content Here</article>
</section>

<footer></footer>

The CSS for styling:

body{overflow: scroll;}

.main-wrap{
    overflow: hidden;
    position: relative;
}

header{
    position: relative;
    z-index: 3;
    height: 10vh;
}

aside{
    position: fixed;
    width: 22%;
    height: 84vh;
    top: 10vh;
    left: 0;
    bottom: 6vh;
}

section{
    position: relative;
    min-height: 84vh;
    width: 78%;
    left: 22%;
}

footer{
    position: relative;
    z-index: 3;
    height: 6vh;
}   

A sidebar menu has been set up using hashed links for navigational scrolling, which seems to be functioning correctly. However, upon clicking on the hashed anchors, there is an issue where all elements shift slightly upwards, including the header and footer, being concealed by the overflow:hidden; property of the .main-wrap element. Furthermore, this problem persists even when returning to the non-hashed page without resolution.

Struggling to find a solution. Any suggestions?

Update: It's worth mentioning that a reset.css file is in use, which sets the padding and margins of body and html to 0.

Update 2: A potential root cause has been identified. Clicking on an anchor link forces the body to scroll within the .main-wrap div, causing it to appear as if everything has shifted upwards. Essentially, the overflow:hidden; property of .main-wrap is simply extending downwards, resulting in the incorrect hiding of certain sections.

Answer №1

Understanding Browser Behavior:

Whenever a named or hashed link is clicked, the browser's default action is to scroll the target named link to the top of the view.

This behavior is applicable in your code as well.

Consideration of Another Browser Default:

Most browsers come with default margins and padding values greater than 0 for various HTML elements, such as html and body.

If you calculate

height: 84vh; /* Equal to 100vh - (header + footer)vh */
, it might result in the content height exceeding the viewport height due to margins or paddings added on html and body tags (and making it scrollable when body {overflow: scroll;} is applied).

Resolution:

To address this issue, consider using a css reset or simply include the following lines in your css:

html,
body {
    margin:0;
    padding:0;
}

View the Demo here: https://jsfiddle.net/vkrhnf75/1/

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

Problem occurs when tab links vanish after clicking on another part of the website

Exploring the world of Javascript as a newcomer has been quite an adventure, but I've encountered a hurdle with tab links on my website. Everything seems to be working fine – the links cycle through and display the correct content. However, when it ...

Guide to accessing the updated content of a webpage

the entire document: <!doctype html> <html> <head> <meta charset='utf-8'/> <title>DATA</title> <link href='img/fav.png' rel='icon'> <link href='index.css' rel='sty ...

Border becomes dashed when dragging and dropping

I am working on enabling drag and drop functionality for users. When an item is lifted from its original position, a gray dashed box should appear in its place. As the item is moved closer to another spot, the containers adjust to create a target area (gra ...

What is the best way to transform this into an HTML readable code?

While browsing through a template, I came across this code and I'm unsure of how to get it functioning. I've tried removing the comments, but unfortunately it doesn't seem to be working as expected. li.dropdown.navbar-cart ...

Achieve button highlighting by hovering over the card - Bootstrap 5

Is there a way to make a button focus when hovering over a card in Bootstrap from any place on the card? Currently, the button only focuses when directly hovered over. I want to achieve the effect shown in the second image after hovering over any part of t ...

Unable to set the width for a div element within the bootstrap grid layout

When trying to set the width of a div tag to 100% within a bootstrap grid system (col-lg-3), I noticed that the div ends up taking the full width of the browser instead of just 100% of its parent(col-lg-3). .sidebar { color: #fff; f ...

I haven't quite mastered the art of styling and JavaScript, but I'm currently focused on creating an HTML form with

I attempted to create a button, but it's not functioning correctly. It should display 'Submit', but instead, it just shows a square. Since I haven't learned CSS yet and my online video instructor hasn't covered it either, I need as ...

"Troubleshooting: Fixing the issue with jQuery datepicker not

After implementing the jQuery Datepicker on a field, I noticed that even though assigning a value to the field shows in Chrome's developer tools... https://i.sstatic.net/We7Ua.png Unfortunately, the assigned value does not show on the front end. I ...

The onClick event cannot be triggered within a div that is generated dynamically

I am having an issue with a jquery code that dynamically generates a div. The problem I'm facing is that the onclick function for the anchor tag is not calling the required function. Below is the code snippet: $("#new").append(' <ul cla ...

Adding to the beginning of a list in JQuery mobile

Having trouble figuring out how to prepend my list in jQuery mobile while keeping the divider on top of the most recent item added. I attempted to prepend the newly added item, but it ended up shifting the divider to the bottom instead. function loadScan ...

One-click URL to trigger iPhone/iPad or Android app to open automatically in browser

Is there a specific way to create a link on an HTML page that will open an app on an iPhone, iPad, or Android device if the app is installed? I have found methods for call links and launching apps via iTunes, but is there a standard format for these types ...

Transform Default Buttons to Resemble Normal Buttons

I currently have a JavaFX button that is set as the Default Button, allowing users to select it with the Enter key. The button has a blue background: https://i.sstatic.net/SYvIj.png However, I would like to change its appearance to that of a normal butto ...

Page refresh enhances hover effect style

Is there a way to maintain a "hover" style even after a page refresh if the user does not move their mouse? The hover effect is only activated on mouse enter/mouseover events, so when the page refreshes, the style doesn't persist unless the user inter ...

Is Bootstrap causing columns to not change colors as expected?

I've been attempting to alter the colors of the column names, but they remain white. Here's an example: <div class="table-responsive"> <button id="exportButton" class="btn btn-primary">Button</butt ...

What steps should I take to create a functional image scrolling effect similar to the one shown in this example?

Recently, I came across and was intrigued by the "image slide effect" featured on their homepage. The way the background image changes as you scroll up, leading you through different introductory slides before reaching the main content of the site caught ...

Unable to impose a restriction on the number input field limit

My input field has a type of "number" with the min and max attributes applied to limit user input. However, I am facing an issue where users can still enter values beyond the set limit. How can I prevent users from entering values above the specified lim ...

Adjust the arrangement of divs when resizing the window

I have utilized flexbox to rearrange the boxes. Here is a link to the demo code My issue arises when resizing for smaller screens; I need to place B between A and C. https://i.stack.imgur.com/M0dpZ.png Any suggestions on achieving this goal? Thank you i ...

What causes the width of unrelated cells to change when the contents of colspan'd cells expand in IE7?

Before we delve into the details, I invite you to take a look at this fiddle using IE7. It's quite intricate and not something I want to repeat here. The main issue is that clicking on the red block should display additional information. The top row ...

Is it possible in HTML to create an "intelligent" overflow effect where text is truncated and replaced with an ellipsis "..." followed by a link to view the full content?

I have a <div> that has a limited size, and I am looking for a way to display multiline text in it. If the text exceeds the available space, I would like to add "..." at the end along with a link to view the full content on another page. Is there a ...

Instructions for adding a unique custom external CSS link, such as Bootstrap CSS, to a specific REACT component

Is it possible to incorporate custom CSS from the Bootstrap website into a React component? Since this CSS file cannot be imported directly, what steps should I take to include it? <link href="https://getbootstrap.com/docs/4.5/dist/css/bootstrap. ...