Using overflow-x: auto causes an unnecessary appearance of a vertical scroll bar

On my website, I have a collection of buttons displayed as a list using inline-block. Some of these buttons include div elements with additional content that is positioned absolutely below the parent li element. The list is contained within a div with overflow-x: auto to account for any unknown number of buttons. However, this setting inadvertently creates a vertical scroll bar that I am trying to avoid, and I'm puzzled as to why it's appearing despite absolute positioning removing elements from the document flow. Here's an excerpt of the code:

#links, #content { 
  width: 100%; 
  position: relative; 
}
#links { 
  z-index: 1; 
  overflow-x: auto; 
  white-space: nowrap; }
#links li { 
  display: inline-block; 
  width: 100px; 
  height: 40px; 
  background: brown; 
  text-align: center; 
  color: #fff; 
  position: relative; 
}
#links .link-content { 
  position: absolute; 
  top: 40px; 
  left: 0; 
  width: 100%; 
  height: 100%; 
  background: #66f; 
}
#content { 
  z-index: 0; 
  background: #ccc; 
  height: 100px; 
}
<div id="links">
    <ul>
        <li>Link 1<div class="link-content">Link Content</div>
        </li>
        <li>Link 2</li>
        <li>Link 3</li>
        <li>Link 4</li>
        <li>Link 5</li>
    </ul>
</div>
<div id="content">
</div>

This is the desired outcome where I removed 'overflow-x: auto' from '#links ul', but still need it in case more buttons exceed the available space:

#links, #content { 
  width: 100%; 
  position: relative; 
}
#links { 
  z-index: 1; 
  /*overflow-x: auto;*/ 
  white-space: nowrap; }
#links li { 
  display: inline-block; 
  width: 100px; 
  height: 40px; 
  background: brown; 
  text-align: center; 
  color: #fff; 
  position: relative; 
}
#links .link-content { 
  position: absolute; 
  top: 40px; 
  left: 0; 
  width: 100%; 
  height: 100%; 
  background: #66f; 
}
#content { 
  z-index: 0; 
  background: #ccc; 
  height: 100px; 
}
<div id="links">
    <ul>
        <li>Link 1<div class="link-content">Link Content</div>
        </li>
        <li>Link 2</li>
        <li>Link 3</li>
        <li>Link 4</li>
        <li>Link 5</li>
    </ul>
</div>
<div id="content">
</div>

To achieve my goal, I plan to utilize JavaScript to toggle the visibility of '.link-content' upon button clicks. Thank you.

Answer №1

Your answer can be found in the following example:

The misconception lies in your understanding of position:absolute. Position absolute disrupts the normal flow and moves based on its anchor element (with position: relative), without affecting its height or width. When trying to compress pages or create a wrapper encompassing the entire structure, the height and width of an element with position:absolute still exist as an entity. By forcing and adding a scroll (overflow:auto), this will be recognized as a valid HTML Element with defined dimensions.

This scenario is not commonly encountered because position:absolute is typically used in menus, which are usually small and do not extend beyond the page boundaries.

section {
  overflow:auto;
  height:110px;width:110px;
}

div {height: 100px;width:100px;background: red; position:relative;}

p {height: 200px; width: 200px;background: blue; position:absolute;left:5px;top:5px;}
<section>
  <div>
    <p></p>
  </div>
 </section>
 
<div>
  <p></p>
</div>

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

Creating HTML tabs using jQuery with input[type="radio"] tutorial for beginners

I'm having trouble creating a tab with input[type="radio"] using Jquery. The tab works fine on the first click, but it doesn't work on the second click. I can't figure out where the issue is in the Jquery code. Can someone assist m ...

Steer your keyboard attention towards the parent element that embodies a list

My implementation focuses on making drop down menus accessible via keyboard input using HTML/CSS and JS/jQuery events. The goal of keyboard accessibility includes: Tab key to navigate the menu elements. Pressing the down arrow key opens a focused menu. ...

Be notified when multiple checkboxes with identical class names are checked with similar values

Is there a way to use jQuery to alert the user if they try to select the same value from a checkbox more than once? <div> <input type="checkbox" class="check" value="1"/> </div> <div> <input type="checkbox" class="check" ...

Ensuring my form adjusts effortlessly to the sprites

I need assistance in making my contact form responsive to match the eagle sprite on this specific webpage: The goal is to ensure that as the screen size changes, the eagle's mouth aligns perfectly with the form. While the sprites and form are both re ...

The React application is experiencing difficulty selecting CSS files through the code

I've encountered an issue with my React app where the button.css file is not rendering properly even though I've kept both the buttn.css and button.js in the same folder. Button.css .Button { background-color: transparent; border: none; ...

Measuring the space between components

I am looking for a way to dynamically calculate distance on a canvas, similar to the example shown in the figure. I want to be able to drag the highlighted joints in order to adjust the span for calculating distances. Are there any plugins available for th ...

Is there a way to stack multiple Divs on top of each other using Float instead of relying on absolute positioning?

I've decided to revamp my approach and transition from using absolute positions to utilizing floats for positioning elements the way I desire. Now the challenge lies in figuring out how to float multiple divs on top of each other, allowing users to s ...

The fascinating CSS Circle interaction on mouse click or hover

I am encountering a challenge with my circular element: Here is the HTML code for it: <div id="quadrant-sizer"></div> And here is the CSS styling: #quadrant-sizer { width: 40px; height: 40px; border-radius: 999px; } The issue I ...

Show secret information once radio button is selected through JQuery code

I am dealing with two checkboxes ABC XYZ When the ABC checkbox is checked, the abc content div is displayed, and when the XYZ checkbox is checked, the xyz content div is shown. Everything works fine, except that if the ABC checkbox is checked by defaul ...

Is there a way to prevent Angular Material Buttons from overlapping a sticky bar when scrolling?

In my Angular application, I have a navigation bar at the top that sticks in place (position: sticky; top: 0;). Beneath the navigation bar is the content, which includes Angular material components such as mat-buttons or mat-cards. The issue arises when ...

Is there a way to prevent users from copying data or switching tasks when using my program or website?

Is it feasible to develop a website or application for Windows desktop machines that can remain focused until the user completes a specific task? For instance, if my YYY app/website is open and I require the user to input some text, I want to restrict the ...

Building a vertically expanding search box using HTML and CSS

I am working on creating an expandable search box for a navbar. The goal is to have the search box expand when the user clicks on it, revealing another input box with a different name input. I would like to have two search boxes, stacked one under the othe ...

Tips for identifying HTML tags that include a specific attribute automatically

Is there a way to automatically identify an HTML tag that contains a specific attribute like data-wow-delay? This query revolves around wow.js. The goal is to have the classes wow bounce added automatically to any HTML tag that has this particular attribu ...

Delete the value from the array and refresh the HTML with the updated total amount, or display '$' if there is no total amount

Currently, I have two inputs: debit and credit. On button click events, they update an array called 'debits'. Debit adds positive values while credit adds negative values. The next step is to sum the values in the array and assign it to a variab ...

C++ HTML parsing tool inspired by Jsoup

In my quest to extract data from various web pages using Java, I relied on the Jsoup library for its efficiency. However, I now face the challenge of having to convert my code to C/C++. The problem is, I'm struggling to find a suitable HTML parser for ...

HTML5 validation fails to activate

Addressing the issue head-on Check out the form I have created: <form action="<?php echo base_url()?>" method="post" id="formfield"> <center> <label>How much? <small>( Minimum of 100 )</small></label&g ...

Creating a stunning effect with radial-gradient on an image element

I am looking to achieve a fading effect on an element using a radial gradient. While it works when I set an image as a background image, it does not work on the element directly. Is there a way I can accomplish this? My goal is to have the image fade fro ...

Guide to invoking an HTML document through PHP

I'm currently in the process of developing an administrator login page using a combination of HTML and PHP, with PHP serving various other functions as well. Once the administrator successfully logs in, I need to execute an HTML file. Below, you&apos ...

Responsive design of HTML forms

I'm curious about the technology or concept behind Google's animations on their forms. I'm interested in incorporating it into my personal project. The use of radio buttons, checkboxes, and placeholders is particularly appealing. Is there ...

Tips for concealing images within a designated list and revealing them in a separate list

I have a question regarding image visibility. Is it possible to hide certain images from a "SHOW ALL" list, but have them appear in a different category list? Below is the code snippet: <section> <ul class="portfolio_filters"> < ...