revealing the excessive vertical space in the div upon clicking the link

Here is the structure:

+-----------------------------------------------------------------------+
|                                                                       |
|                      heading {position: fixed;}                        |
|_______________________________________________________________________|
|                                                                       |
|                     title1  |   title2  | title3                      |
|                                                                       |
|                                                                       |
|                                                                       |
|   tile1: here is an explanation ...                                   |
|                                                                       |
|                                                                       |
|   title2: here is an explanation ...                                  |
|                                                                       |
|                                                                       |
|   title3: here is an explanation ...                                  |
|                                                                       |
+-----------------------------------------------------------------------+

Here are my codes:

<div id = "heading">header</div>    

<a href="#title1">title1</a>
<a href="#title2">title2</a>
<a href="#title3">title3</a>

<div id="title1">tile1: here is an explanation ...</div>
<div id="title2">tile2: here is an explanation ...</div>
<div id="title3">tile3: here is an explanation ...</div>

When I click on the title1 (link), the related div is displayed at the top of the page, under the header (due to the fixed position of the header).

Here is my output: (when I click on title1)

+-----------------------------------------------------------------------+
|                                                                       |
|                      header {position: fixed;}                        |
|_______________________________________________________________________|
|   title2: here is a explanation ...                                  |
|                                                                       |
|                                                                       |
|   title3: here is a explanation ...                                  |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
+-----------------------------------------------------------------------+

However, I want this output:

+-----------------------------------------------------------------------+
|                                                                       |
|                      header {position: fixed;}                        |
|_______________________________________________________________________|
|   title1: here is a explanation ...                                  |
|                                                                       |
|                                                                       |
|   title2: here is a explanation ...                                  |
|                                                                       |
|                                                                       |
|   title3: here is a explanation ...                                  |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
|                                                                       |
+-----------------------------------------------------------------------+

How can I achieve that?


Edit:

Here is my JSFiddle link.

Answer №1

In order to achieve a fixed position for your content without using JavaScript, you must enclose it within a div:

Here is an example of the HTML structure:

<div id="header">header</div>    
<div id="content">
    <ul class="nav">
        <li><a href="#title1">title1</a></li>
        <li><a href="#title2">title2</a></li>
        <li><a href="#title3">title3</a></li>
    </ul>

    <div id="title1">tile1: here is a explanation ...</div>
    <div id="title2">tile2: here is a explanation ...</div>
    <div id="title3">tile3: here is a explanation ...</div>
</div>

This is how you can style the elements using CSS:

#header {
    background-color: lightGrey;
    padding: 10px;
    height: 80px;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}

#content {
    background-color: Grey;
    position: fixed;
    top: 100px;
    left: 0;
    width: 100%;
    bottom: 0;
    overflow-y: scroll;    
}

You can view a demonstration of this setup on JSFiddle: http://jsfiddle.net/tmvmazdg/3/

Answer №2

To achieve this effect, it is recommended to utilize styling techniques.

Start by applying padding to the top of your body element that matches the height of your banner. Next, adjust the positioning of your banner relative to the HTML element rather than the body element.

This should ideally be implemented in a separate stylesheet file, but for immediate changes (assuming your banner is 50px tall), you can modify your existing code as shown below:

<body style="padding-top: 50px;">
    <div id="header" style="position: fixed; top: 0px; left: 0px;">header</div>
...
</body>

Answer №3

If you're looking to manage this with Javascript, here's a solution for you.

Check out this demonstration: http://jsfiddle.net/h2020bpn/3/

Below is an illustration using jQuery for versatility across different browsers. This code can be easily customized to suit your needs.

This snippet utilizes jQuery's .scrollTop() method:

Adjust the current vertical position of the scroll bar for each element matched in the set.

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

if else statement fails to work in ajax-based login form

I tried to create the code below by referencing processes.php, submit form, and ajax from various sources on the internet. However, my code doesn't seem to be working properly. proccess.php <?php session_start(); ini_set('display_errors&apos ...

Automatically generated Dropdown failing to respond to jQuery event

$(document).ready(function() { $('[id^=drop_id]').on('change', function() { selected = $(this).find('option:selected').data('tid') $.getJSON('path/'+selected,function(data){ v ...

Tips for verifying the input field with specific requirements in Angular 6

There is an input field that needs to validate text based on certain logic conditions: No spaces should be allowed in the formula. The operators (and,or) must be lowercase and enclosed in curly brackets {}. The number of opening '(&apos ...

Having trouble with Laravel 5.8 Bootstrap integration within JQuery?

i found a helpful resource at everything seems to be working well in the view, but i'm encountering issues when trying to implement it in JQuery. if anyone has suggestions on how to resolve this problem, please let me know. here is the PHP code for ...

Is there a way to turn off step navigation in bootstrap?

Displayed below is a visual representation of the bootstrap step navigation component. Presently, there is an unseen 'next' button located at the bottom of the page. When this 'next' button is pressed, it transitions from 'step-1 ...

jQuery Files in Conflict

The issue I am facing involves the code below. The first function requires linked js and css, while the second one needs a jQuery and javascript file inherited from base.html. However, there seems to be a conflict in loading these files (possibly because o ...

Struggling with establishing a connection between Jquery and a .php file

I've encountered an issue with my jquery code. I'm uncertain if it's correctly connecting to doc.php as nothing is getting inserted into the database. In doc.php, I have an insert command that I know is functioning properly. My goal is to ...

What is the role of z-index in relation to floating elements?

Why is it impossible to assign a z-index to a floated image and what are the reasons behind this limitation? ...

Guide on coding in Node.js to showcase an image on a web browser

Is there a way to set a local image file as the background for my browser page? I am experiencing an issue where the image does not display when I run my app index.js file through the node.js terminal and visit localhost:3000, even though it works fine whe ...

Modify the button's border color upon click action

I am looking to implement a feature where the border of a button changes when clicked once and reverts back when clicked again. This should apply individually to each of the 16 buttons with the same class. Additionally, I want to enable the ability to clic ...

What could be causing this excessive lag?

I've been developing a new website, but the "buttons" I'm using seem to be causing significant lag. I need help resolving this issue. You can view the website here: The problematic code snippet is shown below: <td> <a href="templi ...

Using jQuery Validator Plugin to verify the existence of an email address

I've integrated the jQuery Validator Plugin into my newsletter form registration for validation purposes. Utilizing the remote validate method, I'm able to verify if an email address already exists in the database. The validation method is funct ...

Trouble with Updating Div Content?

Within my HTML code, I have included this JavaScript snippet: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> <script> var auto_refresh = setInterval( function() { $('#loaddiv').fadeOut ...

Lightbox.options does not exist as a function within the lightbox plugin

I recently incorporated a lightbox plugin into my website, which can be found at the following link: For displaying items on the page, I am using markup similar to this example: <a href="images/image-2.jpg" data-lightbox="my-image">Image #2</a&g ...

Issue with Submit Button Functionality in Django Form Submission

I'm currently facing an issue with an HTML template I built using Bootstrap. My problem lies in the fact that I have a JavaScript function that dynamically adds rows to a Django form with specific fields whenever I need to. However, when I add a row, ...

Toggle button to collapse the Bootstrap side bar on larger screens

I am currently utilizing the following template: An issue I am facing is that I want my sidebar to either shrink or hide when a button is clicked, specifically on a 22" PC screen. I have experimented with several solutions without achieving success. Alt ...

Seamless infinite background scrolling on the canvas without any disruptions

In my HTML5/JS project, I have implemented infinite background scrolling using multiple canvases. The challenge I am facing is that while the animation is smooth after rendering, there is a quick hiccup when transitioning to the next frame due to the need ...

Tips for transferring data from a pop up window to a grid

Currently, I am facing a challenge where I need to store data from a popup window into a grid table cell ID. Is it possible to accomplish this using Jquery or is there another solution available? Any suggestions or examples would be greatly appreciated. ...

Activate doughnut chart animation when scrolling

I stumbled upon an interesting donut chart animation and I'm curious about how to activate the animation when scrolling? $(document).ready(function() { $('#circle-1').circliful(); $('#circle-2').circliful(); $('#circle ...

Vue component retrieval on the client side

I am attempting to retrieve a component using Vue with cdn in order to dynamically re-render from a public project that contains a file named cash-sale.vue with the .vue extension. <script> export default { data(){ return { "rows&quo ...