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

Table design with fixed left columns and header that adjust to different screen sizes

After reading various articles on this issue, I have been unable to find a solution. Most of the examples I've come across feature tables with a single row for the header and rows that consist of just one row, similar to a calendar. My table, on the o ...

CSS - Organization of Rows and Columns

I am in the process of designing a website that will feature news content, and I would like to give it a layout similar to a reddit news box. The layout would resemble the following structure. I am using angular material and CSS exclusively, without any fr ...

Error sound produced when detecting KeyCode on the keyboard

I'm currently working on a JavaScript project that involves capturing keyboard input. However, I'm encountering an issue where every time the user presses a key, it triggers an error sound. Is there a way to disable this sound? ...

Animating toggles with JQuery

I am attempting to create a toggle effect for my div using this jQuery script: $(document).ready(function(){ $("button").click(function(){ $("div").animate({left:'250px'}; }, function() { $("div").animate({left:'0px'}; }, }); ...

The auto-fill feature in Chrome mistakenly inputs the saved username in the wrong field

My web application, coded in HTML/PHP with a login screen, is encountering an issue with Chrome users. After logging in and navigating to the home page, some users are prompted to save their password. This leads to the username appearing in a field unrelat ...

Struggling with getting the JavaScript, scss, and CSS television animation to turn on and off properly? Seeking assistance to troubleshoot this

After finding this javascript code on Codepen and seeing that it worked perfectly in the console there, I tried to run it on my computer with jQuery but it didn't work outside of Codepen. Even when attempting to use it on JSfiddle or compile the SCSS ...

Animating CSS styles in Vue.js based on JavaScript triggers

Within my Vue.js application, I've implemented a login form that I'd like to shake whenever a login attempt fails. Though I have achieved the desired shaking effect, I'm not completely satisfied with my current solution. Here's a simpl ...

Pictures in Windows 7 have shifted to the left

After a recent migration to the Windows 7 operating system at my company, I encountered an issue. While using the same Visual Studio 2010 master file that was working fine on my Windows XP machine, I noticed that all the images below were now shifted to t ...

How can I use JavaScript to modify the style of the first unordered list (ul) element without affecting the

function displayMenu(){ var parentElement = document.getElementById("menuItem1"); var lis = parentElement.getElementsByTagName("ul"); for (var i = 0; i < lis.length; i++) { lis[i].setAttribute("style","display: block"); } } When the button is clicke ...

Can JavaScript trigger an alert based on a CSS value?

Hello, I am facing an issue. I have created a blue box using HTML/CSS and now I want to use JavaScript to display an alert with the name of the color when the box is clicked. Below is my code: var clr = document.getElementById("box").style.background ...

difficulty in obtaining information submitted through a form

I'm having an issue with my contact form. I tried testing to see if I can retrieve the values from the form, but it's giving me back: name: undefined I'm not sure why it's not working! Here is the code for the form: <form met ...

Combining the lower margin of an image with the padding of a container: a step-by-step guide

There are two divs with a 50px padding, making them 100px apart. The top div contains an image floated to the right with paragraphs wrapping around it. The requirements team requests a 20px margin below the image if text flows under it, but no margin if th ...

Creating a Tree Checkbox using jQuery without relying on pre-made plugins

The data structure provided appears to be hierarchical, but I am unsure if it follows the Adjacency list or Nested List model. Regardless, it is relatively simple to gather this hierarchical data. For instance, to retrieve the entire tree, you can use: SE ...

Steps to insert a column within an HTML document for PDF printing

Is there a way to create an HTML document that can be printed in PDF format but the column feature isn't functioning as expected? Would I need to use a specific library or tool? ...

The CSS filter "progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=80)" is not functioning properly in Firefox

Trying to apply a filter effect using CSS but encountering issues in Firefox: progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=80 ) opacity: 0.5; -moz-opacity: 0.5; Any s ...

Stop the caching of HTML pages

After adding the lines below to prevent caching and display the content within an iframe, there seems to be no effect: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-Equiv="Cache-Control" Content="no-cache"&g ...

Compilation of CSS by Webpack resulting in peculiar class identifiers

Currently, I am using webpack for developing a React app with a NodeJS backend. I've encountered an issue with styling the app as webpack seems to be interfering with my CSS, causing changes in class names. For example, in my base.css file, I have d ...

Using a PHP variable to trigger the jQuery .show() function

I'm attempting to trigger jQuery .show() events based on PHP variables. Below is my PHP code (retrieved from a form submission on another page): $last_pic_displayed = trim($_POST["last_pic_displayed"]); if (strlen($last_pic_displayed) <= ...

Silky animations in kinetic.js (html5 canvas)

I am seeking a better grasp on kinetic.js animation. Recently, I came across a tutorial at . After experimenting with the code provided, I managed to create an animation that positions my rectangle at x coordinate 100. However, I am struggling to achieve s ...

CanJS utilizes mustache templates to escape HTML tags

I have a requirement to present a series of choices to the user. I am using a mustache template along with the CanJS JavaScript framework to showcase these options. The problem arises when attempting to display an option such as: Potato Rs. 12 The mustac ...