CSS overflow property does not take effect when set to hidden

I'm having trouble understanding why the overflow: hidden property is being overridden by a descendant element. Take a look at this example I've put together http://jsfiddle.net/2fRxc/.

HTML

<div class="relative">
    <div class="overflow">box with "overflow:hidden;" (HOVER ME)
        <div class="absolute">absolute box which ignores the "overflow:hidden;" rule of the parent box</div>
    </div>
</div>

Style.css

html, body {
    height:100%;
}
.relative {
    position:relative;
    padding:20px;
    width:50%;
    height:50%;
    background:blue;
}
.overflow {
    width:100%;
    height:100%;
    background:#ff0;
    overflow:hidden;
    float:left;
}
.overflow:hover .absolute {
    opacity:1
}
.absolute {
    width:100%;
    height:100%;
    background:#f00;
    position:absolute;
    top:0;
    left:0;
    opacity:0
}

I understand that applying position: relative to the overflow container would solve the issue, but I don't want to do that. I want the absolute container positioned relative to its container.

Any suggestions would be greatly appreciated.

Answer №1

First solution

To fix the issue with the "overflow" div, you can set it as relative and remove the float:left property:

.overflow {
    width:100%;
    height:100%;
    background:#ff0;
    overflow:hidden;
    position:relative;
    top:0;
    left:0;
}

Check out a demo here: JSFiddle

Second solution

If you have 20px padding, define the bounds of your absolute element without specifying the height and width properties:

.absolute {
    background:#f00;
    position:absolute;
    top:20px;
    left:20px;
    bottom:20px;
    right:20px;
    opacity:0
}

See a demo here: JSFiddle

Answer №2

.position-relative {
    position: relative;
    width: 200px;
}
.overflow-box {
    width: 300px;
    height: 20px;
    background: #ff0;
    overflow: hidden;
}
.overflow-box:hover .absolute-div {
    opacity: 1;
}
.absolute-div {
    background: #f00;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    opacity: 0;
    overflow: hidden;
}

http://jsfiddle.net/88fYK/438/

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 Adjust Overlapping Text in CSS?

Currently, I am facing an issue with an element overlapping in my CSS file. On a regular browser, one line of text appears fine but on mobile devices, it overlaps into two lines. This problem is specifically for the mobile version of the website, particula ...

Uploading Multiple Files Using HTML5 and AJAX

I recently stumbled upon this simple plain JavaScript AJAX upload code (apparently jQuery's $.post doesn't work properly with HTML5 for some reason), /* If you want to upload only a file along with arbitrary data that is not in the fo ...

I'm curious if there is a specific jQuery event that triggers right before an element loses focus or right before the next element gains focus

I am facing a situation where I have two input elements in a form that are closely connected. The first element triggers an ajax call to check for existing data in the database when the user tries to move away from it, while the second element opens a moda ...

Are you able to develop a customized TestNG Listener to cater to your specific requirements?

I have developed a unique concept for a TestNG listener that meets my specific requirements. Essentially, I aim to design a custom listener that generates a report using a predefined HTML format. The crux of my idea revolves around declaring the listener ...

Python web scraping: Extracting data from HTML tag names

Seeking help with extracting user data from a website by retrieving User IDs directly from tag names. I am utilizing python selenium and beautiful soup to extract the UID specifically from the div tag. For instance: <"div id="UID_**60CE07D6DF5C02A987E ...

react: implement custom context menu on videojs

Can someone assist me with adding a quality selector and disabling the right-click option in a webpage that uses videojs? I am unsure about using plugins, as there were no examples provided in react. Any guidance would be appreciated. VideoPlayer.js impor ...

The "maxlength" attribute does not function with the input type "number" in an HTML textbox

The maxlength attribute does not seem to be functioning properly when using type="number" ...

Code snippet for calculating the size of an HTML page using JavaScript/jQuery

Does anyone know of a way to calculate and display the size/weight (in KB) of an HTML page, similar to what is done here: Page size: 403.86KB This would include all resources such as text, images, and scripts. I came across a Pelican plugin that does th ...

Can Rollup be utilized solely for processing CSS files?

I am curious about whether Rollup can be used solely for processing CSS files, such as css, scss, less, etc. For example, if I have a file called index.css in my src folder (the entry folder) and want Rollup to process it in the dist folder (the output fol ...

A distinct vertical line separating two buttons

I'm currently working on an Angular 2 app using Angular material. I have two buttons labeled "sign in" and "sign up", and I'm trying to add a vertical line between them. Despite looking at various examples online, I haven't been successful i ...

"Enhance User Experience with the jQuery/JavaScript Table Pagination

I have searched extensively for a jQuery/JavaScript table pagination solution. While I found some options, they were too complex for my needs. I am looking for a simple jQuery plugin to assist with paginating an HTML table. Datatables is more than what I ...

Removing the loading spinner from Ag Grid

While utilizing ag-grid with rowModelType=serverSide, I have encountered an issue where the loading overlay includes a spinner as shown in the image below. My goal is to eliminate this spinner from the overlay. I attempted using hideOverlay(), but it seems ...

Activate simultaneous HTML5 videos on iOS devices with a simple click

I am currently in the process of developing a webpage that will play various videos when specific elements are clicked. Although the functionality works perfectly on desktop computers, it encounters an issue on iOS devices. The problem arises when the elem ...

Purchase Now with Paypal Button

Just starting out with PHP and PayPal buttons! Important: Referring to Mysql items here. I'm attempting to set up an online shop, but I have a concern about PayPal "Buy Now" buttons. If a customer purchases an item, the website receives the money, b ...

jQuery failing to append code after being removed

I need some assistance with an issue I've run into while using jQuery's .remove(). Here is a snippet of code that showcases the problem: $( '.video-button span.glyphicon-play' ).click(function() { $( '#video-player' ).ap ...

Content in Bootstrap not filling entire mobile screen

The content in this form does not stretch to full screen on mobile phones, but it appears properly on computers and tablets. Please advise on how to solve this issue. https://i.stack.imgur.com/S2XkS.jpg <link rel="stylesheet" href="https://maxcdn.bo ...

In JavaScript, a prompt is used to request the user to input a CSS property. If the input is incorrect,

Implement a while loop that continuously prompts the user to enter a color. If the color entered matches a CSS property such as blue, red, or #000000: The background will change accordingly, but if the user enters an incorrect color, a message will be dis ...

Hide an Angular button when a page is loaded, depending on the information found in a table

How can I hide the Submit button in a table row if a certain condition is met based on information from the database? I attempted to create a function that returns true or false, but it ends up crashing my program because it runs continuously. I also trie ...

Stop the form submission until validation is complete

I'm currently working on a form and encountering some validation issues. HTML: <form id="regForm" class="form-group" method="POST" action="signup.php"> <div class="col-md-12"> <h2>Job Pocket</h2> </div> <di ...

Feeling lost and frustrated trying to align a div with a menu in the center

I've been struggling for over an hour now to center a menu on my website. Despite trying all of Google's recommendations, I still can't seem to get it right. Could someone please point out what obvious mistake I might be making? Below is ...