Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should border-radius clip the content?

Tags:

css

Shouldn't the content of my container be cut off when the container has border-radius?

Sample HTML and CSS:

.progressbar { height: 5px; width: 100px; border-radius: 5px; }
.buffer { width: 25px; height: 5px; background: #999999; }
<div class="progressbar">
    <div class="buffer"></div>
</div>

As you can see I use border-radius on the container (.progressbar), but the content (.buffer) goes 'outside' the container. I'm seeing this on Google Chrome.

Is this the expected behavior?

P.S. This isn't about how to fix it, this is about whether it should work like this.

like image 358
PeeHaa Avatar asked Dec 20 '11 21:12

PeeHaa


People also ask

Which is correct use of border radius?

The border-radius CSS property rounds the corners of an element's outer border edge. You can set a single radius to make circular corners, or two radii to make elliptical corners.

Should I use border radius REM?

There is no technical reason not to use the rem unit for border-radius . Neither is never any compelling reason to use the rem unit, for it or otherwise.

What is the difference between border and border radius?

The only difference between them is the right button has a radius of 5px applied. As with borders, radius allows you to set different properties for each side of the element using the toggle controls.

Why border radius is not working?

Your problem is unrelated to how you have set border-radius . Fire up Chrome and hit Ctrl+Shift+j and inspect the element. Uncheck width and the border will have curved corners. Show activity on this post.


3 Answers

Is this the expected behavior?

Yes, as crazy as it sounds, it actually is. Here's why:

The default overflow for <div> elements (and most other things) is visible, and the spec says this about overflow: visible:

visible
This value indicates that content is not clipped, i.e., it may be rendered outside the block box.

In turn, §5.3 Corner clipping in the Backgrounds and Borders module says:

A box's backgrounds, but not its border-image, are clipped to the appropriate curve (as determined by ‘background-clip’). Other effects that clip to the border or padding edge (such as ‘overflow’ other than ‘visible’) also must clip to the curve. The content of replaced elements is always trimmed to the content edge curve. Also, the area outside the curve of the border edge does not accept mouse events on behalf of the element.

The sentence that I've emphasized specifically mentions that the overflow value of the box must be something other than visible (that means auto, hidden, scroll and others) in order for the corners to clip its children.

If the box is defined to have visible overflow, which like I said is the default for most visual elements, then the content is not supposed to be clipped at all. And that's why the square corners of .buffer go over the rounded corners of .progressbar.

Consequently, the simplest way to get .buffer to clip within .progressbar's rounded corners is to add an overflow: hidden style to .progressbar, as shown in this updated fiddle.

like image 186
BoltClock Avatar answered Oct 21 '22 13:10

BoltClock


For anybody wondering what a fix would be. The easiest way would be to edit the css.

In the example given this would be a suitable fix:

.progressbar { height: 5px; width: 100px; border-radius: 5px; overflow: hidden; }
.buffer { width: 25px; height: 5px; background: #999999; }
<div class="progressbar">
    <div class="buffer"></div>
</div>
like image 40
owenmelbz Avatar answered Oct 21 '22 13:10

owenmelbz


Semantically speaking, it's best to simply add a border-radius inherit property to the inner div, hence the new class addition:

.buffer {
    border-radius: inherit;
}

As a consequence, for others situation, you can preserve the use of overflow: auto if the content overflows your frae and you want to see everything.

like image 6
Stéphane de Luca Avatar answered Oct 21 '22 13:10

Stéphane de Luca