Trick to keep horizontal element alignment intact when scroll bar appears

Currently, all my pages are designed with the standard fixed-width-margin-left-right-auto layout.

.container{
     width:900px;
     margin:0 auto;
}

An issue arises when some of these pages exceed the height of the window, requiring a vertical scroll bar to appear on the right side. Consequently, switching between these longer pages and shorter ones causes the main container to shift slightly (about half the width of the vertical scrollbar). This occurs due to changes in the width of the parent element (body).

Is there a library or workaround available to prevent this behavior, aside from using body{overflow-y:scroll;}?

In response to @Mateusz:
Thank you for your suggestion, Mateusz. I tested the following code:

console.log($('body')[0].offsetHeight+' '+$('body')[0].scrollHeight+' '+$('html'[0].offsetHeight+' '+$('html')[0].scrollHeight);

The test results are as follows:

           document smaller than window      document larger than window
firefox    1012 1012 1008 1362               1012 1012 1008 1007
chrome/ie  549 1525 545 545                    549 545 545 545

It appears that different browsers exhibit varying behaviors, with different comparison thresholds (1 and 4).

Answer №1

To determine the appropriate position for your container, consider comparing element.offsetHeight with element.scrollHeight and adjusting accordingly.

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

ion-grid featuring alternate columns

As someone who is not very familiar with Angular, I am trying to create a grid layout like the one shown here: https://i.stack.imgur.com/nBavk.png The desired layout includes alternating rows with one image followed by two images. My current attempt at a ...

The makeStyles feature is currently not functioning properly in the latest version of Next.js with Material UI v5

Currently, I am utilizing nextjs along with material ui in my project "@mui/material": "^5.0.1", "@mui/styles": "^5.0.1", Below is the code snippet of my component structure import { AppBar, Toolbar, Typography, Box ...

Updating rows within a table dynamically using AJAX

I currently have a table with an empty body: <table id="tablem" border="1" width="100"> <thead> <tr> <th style="width: 10%">PageName</th> <th style="width: 5%">Lang</th> ...

Is there a way to modify an existing job in Kue Node.js after it has been created?

Utilizing Kue, I am generating employment opportunities. jobs.create('myQueue', { 'title':'test', 'job_id': id ,'params': params } ) .delay(milliseconds) .removeOnComplete( true ) ...

Utilizing Node and MongoDB to generate a shareable Favorites list with a unique URL

After spending a significant amount of time searching, I seem to be using the wrong search terms because I am struggling to find what I need. Essentially, my goal is to allow users to select items to add to a favorites list and then generate a unique URL t ...

What is the best way to manage Page Refresh using Angular.js?

I recently followed the tutorial at http://scotch.io/bar-talk/setting-up-a-mean-stack-single-page-application This guide went over controllers and services using angular.js for a single-page application. However, when I try to directly access /pageName o ...

Adding a local image to Firebase Storage in Angular5 / Ionic3

Uploading images is a breeze using the following method (select input file): import { AngularFireStorage } from 'angularfire2/storage'; @Component({ selector: 'app-root', template: '<div>' + '<input c ...

What is the CSS solution for eliminating a line break immediately following the first word?

While designing an email template in html/css for a mailing list, I encountered an issue where on mobile devices the line breaks after the first word for every paragraph. You can see an example of this problem here. The code for the email template was gen ...

Ways to generate multiple void components side by side using React

What is the most efficient method for creating multiple empty inline elements using React in a declarative manner? Suppose I need 8 empty divs, I tried the following approach but it didn't work. Is there a more effective way? render() { return ( ...

Is there a way to continuously submit a form in React every 10 seconds, even if it includes events?

I have a form with input fields where I collect data upon form submission. However, I want the form to submit automatically every 10 seconds without the need to click a button or press enter. I tried using useEffect to create an interval, but it resulted i ...

Exploring the Differences in Site Navigation: PHP/HTML, Ajax, and CSS/Javascript

Lately, I've been weighing the pros and cons of using AJAX for my website navigation to transfer only necessary updated HTML elements. Alternatively, if there isn't a significant difference between new elements and current ones, just loading the ...

Disabling directional focus navigation in CSS: A comprehensive guide

Is there a way to disable directional focus navigation properties, such as 'nav-up', 'nav-right', 'nav-down', 'nav-left', for elements on an HTML page? button { position:absolute } button#b1 { top:0; left:50%; ...

Container holding a Material-UI drawer

Is it possible to set the material-ui drawer inside a container in reactjs? I have wrapped my app page in a container with a maximum width of 600px. I want the drawer to start within that container instead of on the body page (picture). The app bar positi ...

I am puzzled as to why it is searching for an ID rather than a view

Currently, I am attempting to navigate to a specific route that includes a form, but for some unknown reason, it is searching for an id. Allow me to provide you with my routes, views, and the encountered error. //celebrities routes const express = requir ...

Transforming HTML into PDF files with PHP, not the other way around

As a PHP developer, I have a project that requires converting a set of HTML documents (approximately 30 to 50 pages) into PDF files. After conducting some research, I have found several potential solutions. These include various PHP libraries and command ...

Unexpected behavior: jQuery AJAX request isn't triggering the alert function as expected

I can't figure out why this code isn't working: $.get("/some/url/", function(event) { alert('hey'); }); When I click the button, I can see the response being executed in the Firefox console, and it shows a successful status (200 O ...

How to implement Bootstrap 5 Floating Label in Contact Form 7 for maximum impact?

I'm having trouble with the new form feature, specifically Floating labels (Bootstrap 5) in Contact Form 7 on Wordpress. The issue arises when a <p> tag is added inside the <input> tag. It seems to break the functionality, but I'm not ...

Building a nested table within a parent table in bootstrap is a breeze

Can you provide guidance on nesting tables in Bootstrap-3? Whenever I attempt it, the second table ends up being displayed after the first one. ...

Using AJAX POST requests with PHP and SQL queries seems to function properly but unfortunately, it only

I am facing an issue with deleting an item from a list using AJAX, PHP, and MySQL. The first time I try to delete an item, the AJAX request works perfectly. However, on subsequent attempts, although the AJAX request returns success, the item is not deleted ...

Tips for setting up npm dependencies in a subfolder

Within the main directory, I have a package.json file that contains this command: "install": "cd packages/my-package && yarn". Every time I execute yarn run install, my intention is for it to enter into the specified package, set up the node modul ...