Having an absolute element can lead to overflow causing a scroll

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .wrap {
        overflow-x: auto;
      }

      .content {
        position: relative;
        width: 100%;
        min-width: 800px;
        height: 200px;
        background-color: green;
      }

      .absolute {
        position: absolute;
        top: 50%;
        right: 0;
        background-color: red;
        height: 200px;
        width: 100px;
      }
    </style>
  </head>
  <body>
    <div class="wrap">
      <div class="content">
        <div class="absolute"></div>
      </div>
    </div>
  </body>
</html>

On my webpage displayed in the code above.

Due to setting a minimum width for .content, I had to include overflow-x: auto; in .wrap.

The .content contains an absolute element.

Outcome:

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

As shown in the image, a single scroll bar appears.

If I were to remove overflow-x: auto; from .wrap:

https://i.sstatic.net/7m9vR.png

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .wrap {
      }

      .content {
        position: relative;
        width: 100%;
        min-width: 800px;
        height: 200px;
        background-color: green;
      }

      .absolute {
        position: absolute;
        top: 50%;
        right: 0;
        background-color: red;
        height: 200px;
        width: 100px;
      }
    </style>
  </head>
  <body>
    <div class="wrap">
      <div class="content">
        <div class="absolute"></div>
      </div>
    </div>
  </body>
</html>

overflow-x: auto; triggers scrolling when a child has an absolute element. However, I still require overflow-x: auto; for scrollability of the content on smaller screens.

Is there a way to resolve this issue?

Answer №1

The reason for the vertical scrollbar appearing is due to the specifications outlined in the Overflow CSS documentation:

When either overflow-x or overflow-y is not set to visible or clip, the values of visible/clip will compute to auto/hidden respectively.

In this case, with overflow-x set to auto and the default initial value of overflow-y being visible, the computed result is auto, resulting in the appearance of the vertical scrollbar.

To eliminate the vertical scrollbar, simply adjust the overflow-y property to hidden:

.wrap {
  overflow-x: auto;
  overflow-y: hidden;
}

.content {
  position: relative;
  width: 100%;
  min-width: 800px;
  height: 200px;
  background-color: green;
}

.absolute {
  position: absolute;
  top: 50%;
  right: 0;
  background-color: red;
  height: 200px;
  width: 100px;
}
<div class="wrap">
  <div class="content">
    <div class="absolute"></div>
  </div>
</div>


Modification

If the goal is to display the absolute element fully, increasing the height of wrap to 300px could be a solution:

.wrap {
  overflow-x: auto;
  overflow-y: hidden;
  height: 300px;
}

.content {
  position: relative;
  width: 100%;
  min-width: 800px;
  height: 200px;
  background-color: green;
}

.absolute {
  position: absolute;
  top: 50%;
  right: 0;
  background-color: red;
  height: 200px;
  width: 100px;
}
<div class="wrap">
  <div class="content">
    <div class="absolute"></div>
  </div>
</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

Having trouble loading CSS in an express view with a router

I am encountering an issue where I am unable to load my CSS into a view that is being rendered from a Router. The CSS loads perfectly fine for a view rendered from app.js at the root of the project directory. Below is my current directory structure, node_ ...

Having trouble retrieving data from a remote URL with angucomplete-alt

Utilizing the angucomplete-alt directive for creating an autocomplete list through a get request. Successfully fetching results from the Github API: <div angucomplete-alt id="ex5" placeholder="Search projects" pause="500" selected-object="selectedProj ...

Issue with PHP/HTML header() function not directing to URL page

Situation: In my member database overview, users can click on an 'update member' link in a table that redirects them to a page displaying the data of the selected member. The URL receives the parameter lid.php?lidnummer=4 (or any other ID). On t ...

What is the purpose of using the "::before" CSS modifier to display the fontawesome chevron in the specified Bootstrap accordion code?

I stumbled upon a great example of adding arrows to the Bootstrap accordion on CodePen. Check it out: https://codepen.io/tmg/pen/PQVBGB HTML: <div class="container"> <div id="accordion"> <div class="card"& ...

Retrieve information from internet sources

I'm attempting to extract the initial paragraph from Wikipedia by utilizing only JavaScript. Essentially, my goal is to: document.getElementsByTagName("P")[0] However, this content is not present on my own website; instead, I aim to retrieve a speci ...

Are Firefox and Internet Explorer experiencing issues with CSS rendering accurately?

Experiencing some problems with IE and Firefox CSS To see the issue, visit Check it out in Chrome - it's displaying correctly there But when you view it in IE or Firefox, it's acting strange. Am I making a mistake somewhere? Any help would b ...

Issues following compilation with npm run prod

I am currently working on a small website using Laravel and Tailwind CSS on shared hosting. While I am able to use a command line tool on the host, I have encountered an issue. In my local environment, Tailwind works perfectly fine. However, after running ...

Tips for embedding multiple video players on a single HTML page

I'm currently experiencing an issue with the YouTube API. I am in the process of developing a website where I want a video to automatically play when I open its corresponding image. The code I used initially worked successfully: <script> // 2. ...

How can I use querySelector in JavaScript to target all div elements that have the same class?

I'm having an issue with the document.querySelector in my JavaScript code. It currently only selects the first div that contains the class "test", but I need it to select all such divs. Is there a way to achieve this using my existing JavaScript? ...

What is the best way to stop a jQuery function from applying titles extracted from the first row th's in thead's to multiple tables in a document?

My circumstances: I am new to jQuery and Stack Overflow, seeking assistance for my website project. The challenge: Building a website using Bootstrap 3.3.6 with intricate data tables that need to be stacked dynamically on mobile devices using CSS. It is c ...

Ways to release the binding of an element and then re-enable it for future use

Encountering an issue with dynamically unbinding and binding elements using jQuery. Currently working on creating a mobile tabbing system where users can navigate using left and right arrows to move content within ul.tube. The right arrow shifts the ul.tu ...

The string obtained from input.getAttribute('value') is lacking one character

While developing e2e-tests for an angular application, I encountered a puzzling issue. When trying to retrieve the value from an using the .getAttribute('value') method, I noticed that a single character was missing. Checking the HTML properties ...

Switching on Closed Captions for HTML5 video while deactivating the standard video controls

I am facing two issues. Whenever I include the track tag in my video element, the default controller of the video pops up, which is causing conflicts with my custom controls. Secondly, I am unable to figure out how to turn closed captions on and off. Thi ...

Eliminate the lower boundary of a divisional table

I'm having an issue with displaying images in a div table. Each image has a border at the bottom, which is around 2-5 pixels wide. Unfortunately, as a new user, I am unable to send any images. My main goal is to remove this unwanted border from the ...

styling a flex container with aligned text and buttons

I'm attempting to align text on the left and button controls on the right within a display: flex div. <CustomFieldContainer> <StyledWellContainer> <FieldDetails key={id}> <H4 bold>{label}</H4> <Styled ...

Expanding the background color of a Div within a Grid System

Note: Utilizing the skeleton grid system. I'm trying to expand the background color of a specific div beyond the 960px container, but I'm facing some difficulties. Any ideas? Current: Desired outcome: HTML: <!DOCTYPE html> <!--[if l ...

How can I make my modal box appear after someone clicks on selecting a college?

Can someone help me figure out how to display my modal box after clicking into the selecting list? I've already coded it, but I'm struggling with getting it to show up after the click event. Any assistance is appreciated! In the image provided, ...

Image source not functioning properly

I am attempting to dynamically load an image and HTML using Angular but it doesn't seem to be working. Here is what I have tried: <img ng-src="img/{{product.Category}}/{{product.id}}.jpg"> and also this: <img src="img/{{product.Category}}/ ...

Utilize CSS styling on dynamically loaded HTML content

I am working on a project similar to the StackOverflow editor. I am fetching markdown text, converting it to HTML, and then displaying it in a preview area. However, when I try to style the injected HTML elements using CSS, the styles are being ignored. Up ...

Troubleshooting Full Screen Problems with Three.js

Despite my efforts to troubleshoot, I am still encountering a frustrating issue with the Three.js API. Even after thoroughly studying the API documentation, browsing through StackOverflow questions, and using debugging tools like firebug and chrome's ...