Creating an element that remains a consistent height despite zooming or resizing the window: A guide with HTML and CSS

Recently, I created a custom scrollbar but faced an issue with its width. Whenever I zoom in or out (Ctrl + mousewheel), the width (set to 10px) changes due to being in pixels. When switched to width: 1vw, it works well with zooming but gets affected by window resizing once again.

Even when attempting to use width: 1vh, the size of the scrollbar alters upon vertical window resizing.

What is the best approach to ensure that the scrollbar's size remains fixed and independent of zooming or window resizing?

CSS Code

#scroll-bar {
    position: fixed;
    width: 1vh;
    height: 100%;
    top: 0;
    right: 0;
    background: black;
    
}

Javascript Code

<script>
    let bar = document.getElementById('scroll-bar');
    let totalHeight = document.body.scrollHeight - window.innerHeight;
    window.onscroll = function () {
        let bar_height = (window.pageYOffset/totalHeight)*100;
        bar.style.height = bar_height+'%';
    }
</script>

Answer №1

It seems like you may encounter several issues with your current setup, as vw is tied to window width and px is influenced by zoom levels.

The vh unit is relative to window height, so using it in this context might not be ideal for your situation.

To address this, consider setting min-width and max-width on your scrollbar to establish boundaries:

#scroll-bar {
    position: fixed;
    width: 1vw;
    height: 100%;
    top: 0;
    right: 0;
    background: black;
    min-width:12px;
    max-width:48px;
}

Perhaps revisiting your strategy to accommodate browser zoom through Javascript could offer a more effective solution that doesn't affect the scrollbar.

Alternatively, you could monitor the browser's zoom level and calculate the required pixel width for your scrollbar accordingly: How to detect page zoom level in all modern browsers?

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

SCRIPT438: The operation failed because the object does not have the ability to use the 'forEach' property or method

Issue with IE8: Property or method 'forEach' not supported $('.tabs').tabs(); $('#search-consumables [data-ajax-call]').change(function() { var $this = $(this), settings = $this.data(), $target = $(setti ...

Menu stack with content displayed to the right on larger screens

Given the content div's position (up middle), what method can I use to stack the divs in this manner? Is it possible to accomplish with only CSS or is jQuery necessary to move the div content out on larger screens? ...

Multer throws error when trying to upload files as an array instead of individually, displaying an 'Unexpected File Error'

Multer is a key module utilized in conjunction with node js and express for file uploads. On the angular side, I am utilizing the ng-file upload module. When sending multiple files individually, everything works smoothly without any errors. However, when ...

Move the internal array pointer forward to the next record in order to apply the insertAfter function within the jquery code

As a new jQuery user, I'm attempting to develop a jQuery function using data provided by PHP. My goal is to arrange DIV elements in order of their values using insertAfter method. However, I seem to be encountering some difficulty in advancing the poi ...

How can a tooltip be displayed on a disabled toggleButton?

When trying to display a tooltip for MUI's ToggleButton while it is disabled, I am encountering an issue where the tooltip works fine for enabled ToggleButtons but breaks for disabled ones. <ToggleButton value={item?.key} aria-label="left aligned" ...

AngularJS: How do I stop the $interval from running indefinitely?

After spending hours reading the documentation, it's now 3am and I'm at my wit's end. Below is my controller code: controller('makeDashCtrl', function ($scope, $rootScope, $cookies, $location, $http, $interval) { var userId ...

Locate a specific element within an array and retrieve its corresponding index

Need help updating a boolean property for all objects in an array with the same user ID. I've outlined my MVC framework below in a concise manner. Model: var posts = [{id:1, user_id:1, is_following:true}, {id:2, user_id:1, is_cool:true}, ...

Occupying room while using display: none in <td>

As a newcomer to media queries and .net, I find myself struggling with the code below. At max-width 775px, my goal is to have the first td disappear completely and be replaced by the second td. However, it seems that while the first td becomes 'invisi ...

Tips for troubleshooting JavaScript errors in Internet Explorer 7 & 6 using Dreamweaver CS3

Is there a way to track and debug JavaScript errors in Internet Explorer 7 & 6 on web pages using Dreamweaver CS3? I am experienced with debugging in Visual Studio, but unsure how to do it in Dreamweaver CS3. ...

None of the stylesheets are functioning properly in WordPress except for the menu

After installing a new theme and making some menu edits, my site suddenly broke. Strangely, only the menu css seems to be working fine. Instead of images, I am now seeing a string of array codes and the css is not functioning properly. It seems like the ...

What is the correct way to refresh v-for loops in Vue3?

Here is a brief overview of the project: We need to display an invoice card that contains details about an invoice. Users should be able to assign payments to the invoice and also remove them. These payments are stored as objects in an array. <tr class= ...

Scrolling triggers the click event

Within my JavaScript code, I have a rather simple event listener set up to listen for a click: element.addeventlistener('click', ()=>{ #do somthing }) However, I am encountering an issue on IOS (iPhone) where scrolling triggers the event list ...

Utilizing a custom function to filter Firestore collection data based on location proximity

I have a question about filtering a Firestore collection using a function where the values of the documents in the collection are used as arguments. Let's say we have a Firestore collection with documents structured like this: { pointOfInterest: "S ...

What could be causing the excessive vertical spacing in an HTML list on Chrome: PHP output or CSS styling?

I'm having difficulty understanding why there is a significant vertical offset (around 170px) between one <li> element and the next. This issue seems to only occur in Chrome and not in Firefox, specifically within <li> elements when PHP co ...

The JavaScript onClick function is unable to identify the object

"Newbie" JavaScript Question:-) I'm struggling with a JS function in my code: <script type="text/javascript"> $(function () { $('.shoppinglist-item-add').ShoppinglistItemAdd(); }); </script> This "shoppinglistI ...

Exploring the World of Dynamic Table ID Access in Rails 5 with Coffeescript

Within my Index view, I have a table that I want to dynamically populate with an ID. This is what I've attempted so far: id="table_<%= @controller_name %>" The method in my controller looks like this: def get_controller_name @controller_nam ...

Transforming an array of Instagram photos into an array specifically consisting of chosen photos

Trying to configure my Instagram API website to allow users to pick photos. I have managed to store the data in a database and display the photos, but I'm struggling to enable photo selection in a different array. Below is the code snippet: <?ph ...

What is the process for obscuring items using the depth buffer in three.js?

In my current project using three.js, I am working on a 2D game where I need to render background scenery followed by a transparent quad that still writes values to the depth buffer even though it has an opacity of zero. The challenge arises when trying to ...

The background extends beyond the confines of its container

I'm facing some challenges trying to create this specific layout, particularly with setting a background that should cover 50% of the screen size. My initial thought was to use an image as the background, but I need different colors for each page. ht ...

"Upon clicking the submit button, no action is triggered and no _POST values are being received

When a user clicks on my submit button, nothing seems to happen. The HTML code for the button is as follows: <td colspan="3"> <div align="center"> <input name="Decline" id="Decline" value="Decline" type="submit"> ...