After resizing, a div with a height of 100% will scroll upwards

I have a single-page website consisting of three sections, each spanning the entire width and height of the window. While resizing the window on the first or last section, everything adjusts smoothly. However, when I am on the second section, things get a bit wonky. Upon resizing the window, the first section shrinks, causing the second section to move up and reveal part of the third section.

My goal is for the second section to stay fixed within the window when resizing, so any fragments of the first and third sections are not visible. It's a bit tricky to explain in words, so here is a simple fiddle showcasing the issue: http://jsfiddle.net/4tVMk/. I'm specifically referring to the behavior of section B (div#two). To add some context, scrolling on the site is disabled, and I aimed for it to resemble a slideshow, hence why every resize messes up the visual presentation.

HTML:

<div id="one"><a href="#two">A</a></div>
<div id="two"><a href="#three">B</a></div>
<div id="three"><a href="#one">C</a></div>

CSS:

#one {
    width:100%;
    height:100%;
    background-color:red;
}
#two {
    width:100%;
    height:100%;
    background-color:green;
}
#three {
    width:100%;
    height:100%;
    background-color:blue;
    font-weight:800;
}
a {
    color:white;
}

I've attempted adjusting the size of the last div as a workaround but that caused the third div to disappear completely. Another failed attempt was reloading the entire page upon each resize that changed the height, but that proved ineffective as well since I couldn't achieve a full reload without losing my current location.

Any help is appreciated.

E: Taking the term 'fixed' literally, I used jQuery to prevent the area from moving under certain conditions. While this solution works for now, I would greatly appreciate any hints on how to automate this process or where to seek further guidance.

Answer №1

Recently, I had a similar goal in a project that I was working on. We needed tab buttons at the top of the screen to switch between different page bodies when clicked.

Although your situation may differ slightly from ours, with your focus on disabling scrolling and having one div occupy 100% width & height of the screen, it seems like you only want one div displayed at a time. The code I used for our project could work for you too. Here's how I modified your code to reflect this:

HTML:

<div class="fill-screen" id="one"><a href="#two">A</a></div>
<div class="fill-screen" id="two"><a href="#three">B</a></div>
<div class="fill-screen" id="three"><a href="#one">C</a></div>

CSS:

div.fill-screen {
    width: 100%;
    height: 100%;
    display: none;
}

div.fill-screen:target {
    display: block !important;
}

#one {
    background-color: red;
}

#two {
    background-color: green;
}

#three {
    background-color: blue;
    font-weight: 800;    
}

a {
    color: white; 
}

JS:

$(window).on('hashchange', function() {
    if (document.location.hash === "" || document.location.hash === "#") {
        document.location.hash = "#one";
    }
});

$(window).trigger('hashchange');

While JS is required for this solution, it's lightweight and more effective than using jQuery to prevent scrolling issues. The example provided uses jQuery, but achieving this in pure JS is also possible - I just opted for jQuery in my project.

In summary, this solution utilizes the CSS :target selector on the three divs. When a div is 'targeted' by the hash in the URL, the specified CSS is applied to that particular div. By default, all three divs are set to display: none;, making them invisible. However, when any div is targeted in the URL hash, its display attribute is changed to display: block !important;, causing it to fill the page while the others remain hidden.

The JS handles setting the URL hash when none is present, such as when a user first navigates to your page. It triggers the hashchange event on page load, ensuring that if no hash is provided or an incorrect one is attempted to be removed, the default hash ("#one" in this case) is enforced. This maintains consistency and prevents display issues.

If you find that you don't need the JS functionality initially or have specific requirements, adjustments can be made to tailor the behavior accordingly. Feel free to customize as needed.

For further clarification, here's a JSFiddle link: http://jsfiddle.net/Tedworthy/M33Ct/

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

Can a border not be added to a <tr> in an HTML table using CSS for any specific reason?

I am facing an issue with the borders of a table row (tr) when applying CSS styling. The tr has a class called "underRow" which is supposed to add a border at the bottom. In my CSS, I have defined the following for the ".underRow" class: .underRow { ...

How does HTML calculate the percentage-based size of a child element when the parent element has padding?

After analyzing the scenario presented, it appears that the outer yellow box is 240px wide (200 width plus 20 padding on each side). The red child inside has its width set at 50%. Surprisingly, when rendered, the red box measures 140px in width. You can v ...

Tips for displaying and concealing columns in Blazor QuickGrid based on breakpoints

I encountered an issue when attempting to hide a PropertyColumn on small screens in Blazor 8 using the regular Bootstrap 5 method. By adding Class="d-sm-none d-md-block" to the QuickGrid component's PropertyColumn, it seems that the Bootstra ...

The height of each Bootstrap column is equal to the height of its corresponding

I am trying to prevent the column height from being equal to the row height in Bootstrap. Here is my code snippet: <div class="row justify-content-center text-center"> <div class="col-sm-3"></div> <div class="col-sm-9"></d ...

What is the best way to display lengthy content using a pair of HTML tags?

I am facing an issue with my Angular4 app where I have a <md-card-content> tag on part of my page. Inside this tag, I am trying to display some content within a <p> tag which has a maximum height of 20cm. While this setup works fine for short ...

Display all attribute connections for a product in Prestashop

I have updated the products in my shop to include different combinations. These combinations involve a 'packaging' attribute with two options: a 100 units box and a 200 units box, each with its own unique reference number. On the product page, t ...

jQuery failing to recognize clicks on dynamically added element

Here's the scenario: <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header" ...

The elements in the list are too large for the designated area on Internet Explorer and Chrome browsers on Mac computers

I am facing an issue with my navigation menu on Internet Explorer and Chrome on MAC. The last element of the list is not fitting into the given width (which is fixed at 1000px). It works fine on Firefox, Opera, and Windows Chrome. I cannot test Safari at t ...

Tips for transferring an entire array to a servlet and retrieving it efficiently

I have been attempting to collect all jqgrid data into one array and send it to a servlet. So far, I have tried the following code snippet: var rows= jQuery("#list").jqGrid('getRowData'); var paras=new Arr ...

What is the process for transforming a Typescript source file into JavaScript?

I have a basic HTML file with a script source set to index.ts. index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge ...

Integrating an HTML resume, complete with its CSS style sheet, into a different HTML page

Looking for tips on seamlessly integrating one HTML page within another? I'm in the process of customizing my personal website using a template, but I recently came across a free resume HTML code that I'd like to incorporate. However, when I atte ...

Strip the pound symbol from the end of a URL during an AJAX request

When I click on the News tab in my Rails application (version 2.3.4 and Ruby 1.8.7), an Ajax call is triggered to load data. <a href="News" onclick="load_feed();"><u>Show More...</u></a> <script> function load_feed() { $.a ...

What is the event in jQuery that behaves differently in Firefox Developer Browser compared to other browsers?

My observation revealed that the symbols "-" and "+" (not those on the numpad) yield different values. I conducted a comparison using this resource: http://api.jquery.com/event.which/ Please refer to this image for a visual of a Swedish keyboard layout: ...

Locate the submenu item in relation to the main menu item within an external container

I'm working on modifying the behavior of a site's sub-menu. The current sub-menu is displayed as a drop-down, but I want it to show up in a separate horizontal div instead. Here's what I have accomplished so far: jQuery(document).ready(fun ...

Deleting validation messages from my MVC controls

I set up some validation in my Model by adding the following code: [Required] [StringLength(60, MinimumLength = 4)] [Display(Name = "Users code")] public string UserCode { get; set; } When c ...

Check for compatibility of overflow:scroll with mobile browsers

Is there an easy JavaScript method that works across different devices and libraries? I am looking to assign a class to the html element in order to enable scrollable containers on mobile devices when needed. I want to follow a similar approach to Modern ...

In order to display the particle-slider logo effect, the JavaScript on the page needs to be refreshed

I have a website frontend integrated from WordPress using an HTML 5 Blank Child Theme. The site features a logo effect utilizing particle slider for screen sizes greater than 960px, and a flat logo image for screen sizes less than 960px. Everything works p ...

Extract information from the table using $_POST

I possess a datatable within a form. Inside that table, there is a column designated for setting a price. Upon submitting the form on the same page, the $_POST data fails to populate. Consequently, I am in need of inserting it into the MySQL db post-submit ...

Trigger an immediate Primefaces update using a JavaScript function

Having an issue where: upon page load, I attach a 'keypress' event listener to every input field on the page. The goal is to check for special characters in the input and remove them. Below is the function that executes on page load: function ...

What is the most efficient method for updating URLs in CSS files before the website goes live?

The project I am currently working on has been worked on by various coders with different backgrounds. One of my recent achievements was writing a clean Capistrano recipe to minimize CSS and JS files. Now, the challenge ahead is figuring out how to identif ...