What causes my absolutely positioned element to disappear when using overflow-x: hidden?

I've been experimenting with the CSS property overflow-x: hidden on a container and noticed that it causes my element with position: absolute to disappear.

To see this effect in action, check out this demo: https://codepen.io/Karina1703/pen/LYMzqEj

Take a look at the code below:

.container {
    overflow-x: hidden;  /* Removing this line restores the element visibility */
}

.box {
    position: relative;
    width: 160px;
}

.button {
    width: 100%;
    background-color: blue;
}

.menu {
    position: absolute;
    width: 160px;
    top: 20px; 
    width: 100%;
    background-color: green;
}
<body>
  <div class="container">
    <div class="box">
      <button class="button">Button</button>
      <nav class="menu">
        <div>1</div>
        <div>2</div>
      </nav>
    </div>
  </div>
</body>

Answer №1

The issue arises because the div.container currently has dimensions that match the only relative element inside it, which is the button. As a result, since the navigation is absolutely positioned below the button, it remains hidden from view.

To comprehend the explanation above, it is essential to grasp how position: absolute functions. When an element is set to an absolute position, it is taken out of the normal document flow, and no space is allocated for it in the layout. The element's positioning is with respect to its closest positioned ancestor (if any) or the initial containing block. Its final location is dictated by the values of top, right, bottom, and left.

source

Keeping this in mind, adjusting the heights of both div.container and div.box ensures there is ample space for displaying the inner elements.

This link leads to a visualization of the solution on CodePen.

<html>
    <head>
      <style>
        .container {
            width: 100vh;
            height: 100vh;
            overflow-x: hidden;  /* Removing this line resolves the issue */
        }


        .box {
            position: relative;
            width: 160px;
            height: 100%;
        }


        .button {
            width: 100%;
            background-color: blue;
        }


        .menu {
            position: absolute;
            width: 160px;
            top: 20px; 
            width: 100%;
            background-color: green;
        }
      </style>
    </head>
    <body>
      <div class="container">
        <div class="box">
          <button class="button">Button</button>
          <nav class="menu">
            <div>1</div>
            <div>2</div>
          </nav>
        </div>
      </div>
  </body>
</html>

Answer №2

By using position: absolute, you are indicating that .menu will not affect the layout of its parent container, causing it to have a size of 0. This behavior is explained in the documentation.

If the overflow-x property is set to hidden, scroll, or auto while overflow-y is visible (default), the value for overflow-y will be automatically computed as auto.

The height of the container is calculated based on its elements excluding those with absolute positioning. However, absolutely positioned elements will only be visible within this specific container. To see the effect, try adding top:4px;left:10px; to your .menu CSS rules and it should become visible.

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

Preventing div contents from shrinking with changing screen sizes

I am currently working on creating a portfolio website similar to this one. Check out the Website Here Typically, when the screen size is reduced to less than half of the full width, the content of a website tends to be hidden rather than shrinking. Howe ...

Tips for examining a rate-limited HTTP request function

I am trying to test a naive rate limiter I implemented in Node.js for making HTTP requests to an external service. The service has a limit of 10 requests per second, and I am facing timing issues with my current implementation. Despite being aware of the i ...

interactive division element

I'm facing an issue with making my div tag clickable in a five-star rating system. The stars are supposed to represent different values from 1 to 5. Even though the image changes on mouseover, clicking the div tag does not yield any result. This is ...

What is the reason behind the inability of this YouTube instant search script to enable fullscreen mode?

Looking to implement a Youtube instant search on my website, I came across this script that seems ideal for my needs. However, I'm facing an issue where the iframe is not displaying the allowfullscreen property. Can anyone assist with this problem? Th ...

What is the best way to navigate to the top of a form panel?

I'm currently developing a Sencha Touch mobile app. I have a form panel with multiple fields, so I made it scrollable. However, every time I open the screen (panel), scroll down, navigate to other screens, and then return to the form panel, it stays s ...

The action of JQuery is modifying the value of the checkbox, but it is not visually showing as checked

I am working on a list that contains checkboxes and text, similar to an email inbox. My goal is to be able to check or uncheck the checkbox anytime I click anywhere on the list item (row). To achieve this functionality, I have used the following code withi ...

Creating a sticky popup feature for your Chrome extension

Just starting out with chrome extensions and looking to create one that appends an external class to a selected tag. For example, let's say we have the following tag: <h1>extension</h1> When the user clicks on this element, I want to ad ...

What is the best way to reset an event back to its original state once it has been clicked on again

As a newcomer to web development, I'm currently working on creating my own portfolio website. One of the features I am trying to implement is triangle bullet points that can change direction when clicked - kind of like an arrow. My idea is for them to ...

Retrieving checkbox value upon form submission

Imagine having a form containing several checkboxes. Upon submitting the form, you aim to display all values of the selected checkboxes. <form> <input type="checkbox" id="product1" name="product1" value="12"> <input type="checkbox" id="prod ...

Determining if a value is an Object Literal in JavaScript: Tips for judging

As someone with limited experience in object type judgment, I find myself unsure of how to determine if a value is an Object Literal in JavaScript. Despite my efforts to search online for answers, I have not been successful. Any guidance or ideas on how to ...

What is the method for using jQuery to retrieve hidden fields with the visible attribute set to "false"?

How can I access a control with "visible = false" attribute using jQuery? Some code examples would be appreciated. <tr>   <td class="TDCaption" style="text-align: left">     <asp:Label ID="lblMsg" runat="server" EnableViewState="False" F ...

Changing the style of opening curly braces in JavaScript code styling

I have a piece of JavaScript code written for Vue that I would like to discuss. It is common practice in the JavaScript world to place the opening curly brace at the end of a line of code. <script> export default { name: 'newUser', data ...

Express Js EJS Layouts encountered an issue: No default engine was specified and no file extension was included

Hey there! I'm currently experimenting with implementing Express EJS Layouts in my application. However, as soon as I try to include app.use(expressEjsLayouts), an error is being thrown. The application functions perfectly fine without it, but I reall ...

Can I combine the col and d-flex classes in Bootstrap 5 without breaking any rules?

Is it possible to use both col and d-flex classes together? I want to center the element with the class "description" by combining them. <section class="container-fluid mb-5"> <div class="row"> <div class=&q ...

Display everything when an option is clicked

I have a filter that is working perfectly. When I select a specific category, it filters out only rows with that category. However, I am stuck on how to display all rows again after clicking on the first option. My objective is to show all rows when "Categ ...

Ways to position a child element at the center of its parent container?

Hey there, is there a way I can achieve this specific layout without relying on position: absolute? Flexbox would be the preferred method. The main goal here is to have the middle element horizontally centered within its parent element, regardless of other ...

Showing attributes of models using Sequelize and Handlebars

As a novice in the world of programming, I am currently immersed in a website project where users can write and post articles. One crucial aspect I am focusing on is displaying the history of articles written by each user on their account page. Despite uti ...

How can I remove a row from a JavaScript array based on the value of the first item in the row?

Creating an array in JavaScript can be done like this: var myArray = new Array(); myArray.push({ url: urlValue, filename: fileNameValue }); As time goes on, the array will accumulate various items. If you need to delete a specific row based on the urlVal ...

Calculate sums in ReactJS without the need for a button

Adding a few numbers might seem like an easy task, but I've been unable to do so without using an explicit button. // Using useState to handle state changes const [ totalCount, setTotalCount ] = useState(0) // Function to add numbers of differ ...

The jQuery closest selector seems to be malfunctioning when trying to scroll and focus on a specific class

My HTML code snippet is as follows: <div class="main"> <div class="sub-1"> <select> <option>1</option> <option>2</option> </select> </div> <div class="sub-2"> ...