What are some strategies to prevent a fixed sidebar from obscuring content while zooming in?

I've been working on adding a fixed sidebar for a table of contents (TOC) to a website, where the sidebar stays in place as the user scrolls up and down.

My implementation includes the following HTML code:

https://i.sstatic.net/jAY8l.png

and CSS code:

https://i.sstatic.net/Z0LZl.png

I took inspiration from a tutorial I found here: https://www.w3schools.com/howto/howto_js_dropdown_sidenav.asp

Although everything appears fine when viewed at 100% zoom in Chrome, I'm facing issues when zooming in further. The sidebar starts to overlap with some of the adjacent content, causing it to hide behind the sidebar. You can see an example of this problem below: https://i.sstatic.net/Q8cwj.png

If anyone has any suggestions on how to correct this issue and ensure that the table of contents functions properly even when zoomed in, your help would be greatly appreciated!

Answer №1

Managing the Width with CSS

To control the width of your sidebar, consider setting a maximum width using viewport width units (vw).

Unlike other units like %, vh, vmin, and vmax, vw is not affected by zoom variations.

This technique allows you to maintain your sidebar width in pixels or any CSS unit while limiting its maximum size based on the absolute screen width.

Here's an example:

width: 100px;        // Default width
max-width: 25vw;     // Maximum absolute width (not impacted by zoom)

You can also set different max-width values for various screen sizes using media queries. If you prefer your sidebar to always be a certain percentage of the screen width, you can use vw directly in the width property without defining a max-width.


Considerations:

When choosing a CSS unit for width, remember to take into account how they are affected by zoom levels.

Answer №2

When a position is fixed, it does not impact the placement of other elements:

This element is taken out of the regular document flow, creating no space for it in the overall page layout.

-- https://developer.mozilla.org/en-US/docs/Web/CSS/position#fixed

To work around this behavior, you can align the div.contents's padding-left to match the value set for the width of div.toc. If you're comfortable with CSS variables, utilizing one here would be recommended.

body {
  --toc-width: 30vw;
  --toc-background: red;
}

div.contents {
  /* adjust contents based on toc width */
  padding-left: var(--toc-width);
}

div.toc {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--toc-width);
  background-color: var(--toc-background);
}
<div class="contents">
  <div class="toc"></div>
  <h1>not toc</h1>
</div>

Answer №3

If you're looking to ensure consistent width, consider using percentage values. For instance:

div.toc {
  position: fixed;
  height: 100%;
  width: 15%;
  ...
}

When zooming in, px, em, and rem values may change, but percentages remain constant.

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

The embed video is camouflaged within the bootstrap mobile display

I am currently using the latest version of bootstrap and bootswatch united theme to build and explore a standard website. The website is live at this link live site, featuring an embedded section on the right side as shown. My choice for the view engine is ...

Issues with Bootstrap Container on Smartphones

Currently tackling the responsive design aspect of my website, however, I've encountered a bug specifically with mobile devices. Interestingly, testing it on my desktop browser proves smooth sailing, but when attempting on my iPhone, I faced some chal ...

I am facing an issue with grabbing text using simple HTML DOM in PHP with cURL

I have implemented the following code in my script to extract a specific part of the source code that I require, but I encountered this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ')' in /home/cyberhos/pub ...

Unable to submit form using the submit button

Can anyone explain why the form won't submit in this scenario? I prefer not to use input type='Submit', but the function call seems to be working. <button onclick='submitForm()'>Save</button> function submitForm() { ...

Dynamic extension of classes in SCSSORExtending classes

When working with Vue, I encountered a situation similar to :style="{ [`--chip-bg-hover`]: hoverColorValue, [`--chip-bg-active`]: activeColor, [`--chip-border-hover`]: borderHoverColorValue, }" Then, i ...

Traverse div elements using jQuery and showcase the text within them

I've got a setup like the one below. I want to use jQuery to grab each link and display its href right below it. Instead of writing code for each div individually, I'm looking for a solution that works for all divs with the 'col' class, ...

What is the process for converting HTML to RTF using Java programming language?

When working with the basic API of JAVA using RTFEditorKit and HTMLEditorKit, I encountered a limitation where certain tags like <br/> and <table> were not recognized. In my search for better solutions to convert HTML to RTF, I came across two ...

Divide Footer Information into Two Sections: Left and Right

<footer> <div class="copyrights-left"> <p>&copy 2015. <a style="color: #fff;" href="index.html">Free Xbox Live Gold Membership and Free Xbox Live Gold Codes</a>. All rights reserved.</p></div> <div class="co ...

Connecting to particular sections on other pages

My website setup includes a page titled "news.html" that contains an iframe with fixed size. The iframe is linked to "innernews.html", which is the actual content to be displayed. I structured it this way for consistent page sizing, as the iframe prevents ...

The justify-content property aligns single-line text horizontally, but its alignment may not be consistent when the text expands over

Seeking a deeper understanding of flexbox alignment, I decided to experiment with aligning content using only flexbox properties. One puzzling observation is how "justify-content" centers items with single lines of text (flex-item-1 and flex-item-5), but n ...

CSS exclusive - achieving a horizontal scrolling list with all list items in a single row

I am working with a div that has a fixed width. Inside this div, there is an unordered list (ul) containing a random number of list items (li) with varying widths. My project utilizes the Bootstrap framework. Below is an example of the code structure: & ...

Guide on implementing iterative data path in v-for loop using Vue

I'm just starting out with Vue and I want to use image file names like "room1.jpg", "room2.jpg", "room3.jpg" in a loop Below is my code, where the second line seems to be causing an issue <div v-for="(p,i) in products" :key="i"> <img src ...

Enhance user interactivity on your website by incorporating jQuery and CSS for

<table id="tab"> <tr aaa="one" bbb="ooo"><td>xxx</td><</tr> <tr aaa="two" bbb="one"><td>xxx</td><</tr> <tr aaa="three" bbb="one"><td>xxx</td><</tr> ...

Is there a way to allow users to edit all the input fields within the td elements when they click the edit button for a specific row?

I am completely new to web dev and I'm struggling with what should be a simple task. My goal is to enable editing of all inputs in a table row when the edit link is clicked. Since I am not using jQuery, I prefer a pure JavaScript solution if possible. ...

What is the best way to include scrollbars with bootstrap?

I recently came across this HTML code snippet from a learning website: <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <title>Example of Bootstrap 3 Simple Tables</title> <link rel="stylesheet" href="htt ...

Separate the iframe sessions

I am working with 6 iframes from the same domain but with different URLs and subdirectories. Each iframe sets a cookie with the same name but a different value using the HTML header "set-cookie". To prevent interference between these cookies, I need to fin ...

The issue with the Material UI Drawer component is that it is casting a shadow even when

Having some trouble with the Material UI Drawer component. Whenever I try to display it on my webpage, it automatically focuses on the inner div, adding a shadow or border if the focus is anywhere else. Does anyone know why this shadow appears and how to r ...

"Encountering a 404 error while attempting to forward to an HTML

I've been encountering an issue while trying to transition from a JSX page to an HTML page. Every time I attempt to do so, I receive a 404 error stating that the page doesn't exist. It's puzzling because it is clearly present in my files and ...

unique map customized in a separate browser window

Attempting to complete the code provided here but encountering issues as a novice in java-scripting. It seems like the map is not loading correctly. A new tab and text are generated when DIV is created. Seeking assistance on how to open a custom map in a ...

Is there a way for me to perfectly align the image and the h1 header on the webpage?

I'm facing some challenges when it comes to aligning an image and an h1 tag on the same line. I've already tried using display: inline and inline-block, but they only affect the container of the two elements. I also set the width to 100% on the s ...