How to calculate the border-radius of nested elements
The border-radius
property allows you to round the edges of an element. Giving the same border-radius
value to the parent element and child element doesn't result in the best appearance, so how do you calculate the border radius of nested elements?
Check out the demo below:
In the example above, the two circles have the same radius and are inscribed in squares with rounded edges. Note that the radius of the border is the same as the radius of the circle. We want the end of the arc of the circle each edge starts to be coincident with both squares. This can be done in two ways:
- Radius of the border of the inner square (Ri) + spacing between squares (E) = radius of the border of the outer square (Re);
- Outer square border radius (Re) - spacing between squares (E) = inner square border radius Ri.
Note that the center of the circles inscribed in the squares do not coincide, as well as the end of the border arc. Still, the result is satisfactory.
Bonus
In this interactive example, made by Jhey Tompkins, you can see how this rule applies in practice: