The overflow:hidden property does not completely conceal the parent div when it has a border-radius applied

I've noticed some residual overflow in my simple layout that becomes quite apparent when setting a border radius. The expected behavior is for the white div class='inner' to fully cover the red div class='outer'. However, there seems to be overflow at both ends of the div.

Example:

.outer {
  background-color: red;
  width: 500px;
  height: 50px;
  position: relative;
  overflow: hidden;
  border-radius: 5rem;
}
.inner {
  background: white;
  width: 100%;
  height: 100%;
  position: absolute;
}
<div class="outer">
  <div class="inner"></div>
</div>

I'm using this layout for a loading bar effect where inner translates during an animation while media plays. I've tried adding properties like z-index and using masks, along with exploring webkit, but nothing has resolved the issue so far.

Edit

To illustrate more clearly: Overflow hidden is applied so that any overflow from the inner translation is hidden outside the parent div. Below is an example showing a horizontal translation of 10%. I'm trying to achieve the overflow effect without bleeding from the edges with border radius applied.

The translation begins at 0%, which is similar to the previous example.

This issue was also reported on Issue 491574: border-radius bleeds background-color

Similar to this question - CSS border radius background color bleed but the use case of overflow doesn't apply here.

.outer {
  background-color: red;
  width: 500px;
  height: 50px;
  position: relative;
  border-radius: 5rem;
  overflow: hidden;
}
.inner {
  background: white;
  width: 100%;
  height: 100%;
  position: absolute;
  transform: translateX(10%);
}
<div class="outer">
  <div class="inner"></div>
</div>

The code above, though not exact, is embedded in a ReactJS app and I am experiencing this issue in both Chrome and Mozilla browsers.

Answer №1

In my proposed answer regarding the duplicate, it is noted:

The solution would need to be customized for each specific case... by rearranging the elements so they are positioned as top and bottom rather than parent and child.

Therefore, the fix for your scenario involves placing the red background color within a child element of the container... with the progress bar positioned on top. By removing the background color from the container, issues caused by anti-aliasing can be avoided.

Furthermore, applying the same border-radius to the "background" div along with a 1px white border creates the desired effect.

Below, I have animated the width of the .progress-bar at intervals to simulate a reactive state-based animation.

// Simulating an animation... Just for this demo.

let outer = document.querySelector(".outer")
let progress = document.querySelector(".progress-bar")
let outerWidth = outer.getBoundingClientRect().width
let progressWidth = progress.getBoundingClientRect().width

let interval = setInterval(function(){
  progressWidth = progress.getBoundingClientRect().width
  progress.style.width = progressWidth + 10 + "px"
  if(progressWidth > outerWidth) clearInterval(interval)
},500)
.outer {
  background-color: transparent;
  width: 500px;
  height: 50px;
  position: relative;
  border-radius: 5rem;
  overflow: hidden;
}
.progress-background {
  background: red;
  width: calc(100% - 2px);  /* to compensate the white border space */
  height: calc(100% - 2px); /* to compensate the white border space */
  position: absolute;
  border-radius: 5rem;      /* Same radius than the container */
  border: 1px solid white;  /* white border */
}
.progress-bar {
  background: white;
  width: 0;
  height: 100%;
  position: absolute;
  transition: width 1s;  /* Just to have the animation a bit smooter */
}
<div class="outer">
  <div class="progress-background"></div>
  <div class="progress-bar"></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

What is Causing The Card to Not Expand?

I wanted to enhance my project by incorporating a responsive card template, so I turned to one of the templates on CodePen for help. However, after copying the code into my own platform, I encountered an issue where the card wouldn't expand when click ...

Debugger for Visual Code unable to locate URL in Microsoft Office Add-in

I recently installed the Microsoft Office Add-in Debugger VS code extension, but I'm having trouble getting it to work despite following the instructions. Every time I try, an error message pops up: https://i.sstatic.net/h2FYs.png Upon inspecting my ...

Surprising behavior of translateZ in Framer Motion

Trying to position elements in a circular layout has led to unexpected behavior when using framer motion's translateZ. By applying styles with a template literal without shorthand for transforms, the desired effect is achieved: transform: ` rotateY( ...

What is the default parameter type in react-native?

Currently, I am delving into React-native framework. While browsing through the Facebook ListView sample page, I stumbled upon the below code snippet. _renderRow: function(rowData: string, sectionID: number, rowID: number) { .... Interestingly, I have n ...

menu featuring a creative favicon and an accompanying label

I am aiming to replicate this menu design: It consists of a flavicon with a label below it. Menu I attempted to create it in two different ways, but both attempts were unsuccessful. Using Bootstrap groups. <link rel="stylesheet" href="https://u ...

Is the Owl carousel Auto Play feature malfunctioning when hovered over?

I seem to be encountering an issue with the auto play functionality of the owl carousel. I have uploaded and included all the necessary files, and it is functioning properly when initially loaded. The auto play feature works perfectly, but when I hover ove ...

Jest came across a surprising token that it wasn't expecting while working with React and TypeScript in conjunction with Jest and React-testing-L

An unexpected token was encountered by Jest while using React and TypeScript along with Jest and React-testing-Library. Error occurred at: E:\Git\node_modules@mui\x-date-pickers\internals\demo\index.js:1 ({"Object." ...

Understanding the Hierarchy of CSS and Bootstrap Styles

I have been using a custom CSS file along with bootstrap. Initially, I had my custom CSS written before the Bootstrap CSS. However, I recently discovered that it's recommended to include Bootstrap CSS first before any custom CSS. As a result, I'm ...

Set the dropdown item as selected using AngularJS

I have created a demo of an AngularJS dropdown menu, and it's working perfectly. Now, I want to make the selected item appear active. I'm not sure how to achieve this. Can anyone please assist me? I am using the Angular-Dropdown.js library. Here ...

The input tag loses focus after its value is updated using a class method in Angular 8.x

Currently, I am working on integrating a credit card payment method and formatting its number through specific methods. Here is how it is done: HTML <div class="form-group" *ngFor="let formField of cardFields; let cardFieldIndex = index;"> ...

I'm puzzled, can anyone provide clarification on the concept of xml?

Recently, Sheshank S. provided me with a solution to my question, but I failed to grasp its meaning. Can someone please explain it to me? Despite looking through various tutorials on websites and videos, none of them have been able to clarify what this cod ...

Modify the conditions of a CSS file in Bootstrap using JavaScript

My project requires internationalization support for right-to-left languages like Arabic and Hebrew, so I need to modify some Bootstrap classes (such as col) to float right instead of left. I am using create-react-app with babel/webpack and react-bootstra ...

Words spilling onto the picture

The issue of the text col-xs-9 col-md-9 overlapping with the image col-xs-3 col-md-3 has been persistent. Even after attempting to add the img-responsive class to the img src, it continues to not work as expected. Interestingly, CSS was intentionally avoid ...

Is there a way for AJAX to dynamically insert new rows into a table?

I am seeking assistance in dynamically adding rows to an HTML table using AJAX whenever additional records are retrieved from a database query. My workflow involves utilizing Python Flask and Pandas to generate a dataframe containing information about node ...

Is there an issue with loading CSS and JavaScript in mobile view on a website built with CodeIgniter?

My experience with codeigniter has been great so far, but I encountered an issue when trying to view my work on different devices. Here is the problem: Web: Broswer View | Browser (Responsive Design View) Mobile: Screenshot 1 | Screenshot 2 _htaccess: ...

Is there a way to modify the default color when the header is assigned the "sticky" class?

Currently, I am in the process of building my own website and have implemented an exciting hover effect that randomly selects a color from an array and applies it when hovering over certain elements. However, once the cursor moves away, the color reverts b ...

The Python error message, "JSONDecodeError: Expecting value: line 1 column 1 (char 0)," indicates that there

What is causing this error to occur? Are there any alternative methods available for converting it into a json file? import requests import json url = 'https://finance.yahoo.com/quote/AMZN/balance-sheet?p=AMZN&.tsrc=fin-srch' r = requests. ...

the new adjustment has not taken effect on index.php

I have been working on my index.php file and have included various other files as well. Let me show you the structure: <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css" /> <style type= ...

Even if I were to return a Promise.reject, Redux Thunk still remains "fulfilled"

I've been using Redux for a short while and I'm facing some challenges with a Thunk response... Within my slice, I am invoking an asynchronous function called "handleBoxDeliveryAsync" extraReducers: (builder) => { builder .addCase ...

What is the best way to display sanitized text on a webpage?

I have implemented express-validator in my project to sanitize and escape user input before saving it to my MongoDB database. However, when I try to store a URL in the database after sanitizing it, it gets stored as: https:&#x2F;&#x2F;www.youtube. ...