Home >Web Front-end >CSS Tutorial >How to Make Child Elements Respect Curved Borders in CSS: A Question of Overflow?
Solving the Child-Parent Border Obedience Dilemma in CSS
When dealing with nested elements in CSS, it's often necessary for the child element to adhere to the curved borders of its parent element. However, this can sometimes be an issue, resulting in the child element extending beyond its parent's limits.
The Problem:
Consider the following HTML and CSS code:
<code class="html"><div id="outer"> <div id="inner"></div> </div></code>
<code class="css">#outer { display: block; width: 200px; background-color: white; overflow: hidden; border-radius: 10px; } #inner { background-color: green; height: 10px; }</code>
In this scenario, the #inner div will extend beyond the curved borders of the #outer div, creating an unwanted overlap.
The Solution:
According to the CSS3 specifications, elements such as block-level elements are clipped to the curve of their parent's border. However, there are some exceptions, one of which is replaced elements.
Replaced Elements:
Replaced elements, such as and
The Fix:
To ensure that the #inner div adheres to the curved borders of the #outer div, we can use CSS3's overflow property. By setting overflow: hidden on the parent element (#outer), we force the content within it to be masked by its borders.
<code class="css">#outer { display: block; width: 200px; background-color: white; overflow: hidden; border-radius: 10px; } #inner { background-color: green; height: 10px; }</code>
Note: This trick works for Firefox 4 and above. For older versions of Firefox, additional vendor prefixes may be necessary.
The above is the detailed content of How to Make Child Elements Respect Curved Borders in CSS: A Question of Overflow?. For more information, please follow other related articles on the PHP Chinese website!