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:

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

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

<!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

How to Extract Minutes in Datatables Timestamps and Apply Custom Styling

Working with Datatables v1.10 Right now, my table is showing a date in the second column in the format 17-04-2019 14:34, which is how it's stored in the database. The filtering and searching functionality are all working as expected. The current HTM ...

Build interactive web pages using Python scripts

By utilizing the Python scripts provided below, you can generate an HTML file with pre-set global variables (a, b, c, d). However, there might be instances when certain variables are not set globally, resulting in errors like "global 'a' is not d ...

Guide to using Ajax to send a form and receive text in response

Check out my code on Fiddle: $("form.signupform").submit(function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr("action"); var form = $(this); // Added this line for reference $.post(url, data, function(data) { $(for ...

- Prefixing with -webkit-transform and overlooking z-index

When using Safari 5.1.7 on Windows, I noticed that some rotated elements are overlapping others: However, when I test it on Firefox, Chrome, and IE, the issue doesn't occur: Is there a solution to prevent this overlap problem specifically on Safari? ...

Prevent users from selecting an option depending on a specific criteria

I need help with disabling select options based on specific time instances. For example, I want the user to provide specifications by 6:00 am in order to use the system at 10:00 am, ensuring there is a 4-hour gap between the two times. So, after 6:00 am, t ...

"Enhance user experience with jQuery's text expansion and collapse

Figuring out how to collapse and expand text with a button click has been challenging for me. I managed to make the text collapse when the button is clicked, but now I also need it to expand back again. The goal is to have the text initially hidden, then e ...

Issues with Vue enter transitions not functioning as expected

I am currently involved in a project where I need to implement enter and exit animations for some components. When a component enters the screen, it should come from the bottom, and when it leaves, it should go upwards. The intended functionality is that w ...

Unable to see background image inside div

Why is the background image not showing up in the left column? I've applied the CSS background-image to .left but it's still not working. Any suggestions on what might be causing this issue? It seems like a small problem, but it's really bot ...

Navigate to the element by clicking on the link, then apply a class to that specific element

I'm currently working with this HTML code: <div id="main"> <p>some text</p> <a id="goto-notes" href="#">see notes</a> <p>some text...</p> <div id="notes"> <p>notes here< ...

The document.write function in JavaScript is malfunctioning

I've been attempting to utilize the javascript function document.write to incorporate an external css file in my template. However, I am aiming to achieve this using Twig, like so: document.write('<link href="{{ asset('bundles/activos/cs ...

Applying toggle() using css to manipulate various elements at once

Is there a way to toggle multiple divs of varying heights, all with the same class? What is the most effective approach to achieve this? http://jsfiddle.net/hS6RH/41/ $('.smallify').on('click', function() { if ($(this).parent(&apo ...

Ensuring consistent column height in HTML Tables

Hey there, I'm just starting out with CSS and I have a question about table design. I have three separate tables below and I want to ensure that all the columns in each table are of equal height. Is there an easy way to achieve this? Any suggestions o ...

The process of creating a functional search bar in Django

I created a search bar, but I am facing an issue where no titles appear until I start typing. Once I type in one title, all the titles suddenly appear. How can I fix this problem? index.html def index(request): query = request.GET.get('srh' ...

angular and node: troubleshooting the $http.get error

I have encountered an issue with the $http.get instruction. Without it on the main page, I receive a result of "random 46" which is correct. However, when I include $http.get, I get a result of "random {{ number }}". How can this problem be resolved? -se ...

What is the best way to combine a custom CSS class and a bootstrap class to style an element in next.js?

In the following example, the CSS class is imported and then applied to the element: import customStyles from "./Home.module.css"; <div className={(customStyles.collection, "card")}> Although they work individually, when combined as shown above, t ...

Issues with CSS not being applied when the page content exceeds the viewport

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>Unique Title</title> <style> #div1 { posit ...

Creating dynamic transformations and animations for characters and words within a paragraph in 3D

Looking to add animation effects to specific parts of a paragraph, but transforming the entire box instead. Remembering seeing a solution on StackOverflow before, now regretting not saving it. Spent over an hour searching for a similar answer without succ ...

Exploring Checkboxes in Hypertext Markup Language

Can anyone advise on the proper HTML input attribute to limit user selection in a list of checkboxes? Specifically, I want users to select no more than three out of five checkboxes without using radio buttons. Please provide solutions using only HTML and n ...

Align both the image and text in the center with no space between

How can I center two blocks containing both images and text vertically and horizontally while using flexbox? I notice that when the text is longer, padding appears between the image and the text. How can I remove this padding?1 .product-teaser { width ...

Modify styling of complete list upon clicking in React

For my latest project, I developed a basic todo application using React. One of the key features is that when a user clicks on a checkbox, the corresponding todo item's CSS changes to show a strike-through effect. Additionally, a button appears on hov ...