How to Make an Inverted Border-Radius?

Asked

Viewed 1,814 times

8

I was testing the border-radius and a debt has arisen... Is there any way with CSS to reverse the radius of curvature of the border-radius?

This is the Shape format I’d like to get

exemplo visual de elemento com border-radius invertido

But from the paperwork, it looks like border-radius does not accept negative values

.container {
    width: 300px;
    height: 160px;
    background-color: red;
    border-radius: -20px; /* não aceita valor negativo */
}
html {
    width: 100%;
    height: 100%;
    background-image: linear-gradient(to bottom, green, blue);
    background-repeat: no-repeat;
}
<div class="container"></div>

Then there was the question, how to achieve this shape in the corners only with CSS and continue viewing the image or the background color of background page?

OBS: I can’t use SVG or Clip-Path

  • 1

    It has to be with <div>? That there can be easily solved with svg.

  • 1

    @Renan with SVG or even Clip-Phat gets easy even, wanted with CSS only.

  • 1

    Check out this link: https://codepen.io/sergejmueller/pen/fJEml

  • 1

    @Pedropaulo cool example, but if you put there a background: red; no body vc will see what happens... will not serve for me

  • 2

    You have a question in the OS which should assist: https://stackoverflow.com/a/22422105/4730201

  • @Ricardopunctual I will check! [s]

  • My tip is to use SVG

  • @Wallacemaxters think I already have a solution :), I will post soon, no need for SVG, although that would also serve, but wanted only with CSS

  • It’s very complicated. The programmers who did html should have thought about it. It’s almost like we’re in the time of the html square wheel. I was needing to round open (outside and not inside) the rounded corners of a div, to make a tab similar to the tabs of the browsers.

  • @Alencar If you want the normal format just use border-Radius, and the blame is not html CSS ;)

Show 5 more comments

4 answers

12


Following the idea similar to Wallace’s, I found this link from Lea Verou:

The difference is that with this it is possible to apply transparency and so can change the background color easily.

In the example has rounded reverse edge and also corners in diagonal cut (chamfer).

Note that in the example both inside linear-gradient When radial-gradient has #fff, to change the element color change these #fff, and the same should be done in:

background: #fff; /* fallback */

/**
 * Beveled corners & negative border-radius with CSS gradients
 */

.bevel {    
	background: #fff; /* fallback */
	background:
		linear-gradient(135deg, transparent 10px, #fff 0) top left,
		linear-gradient(225deg, transparent 10px, #fff 0) top right,
		linear-gradient(315deg, transparent 10px, #fff 0) bottom right,
		linear-gradient(45deg,  transparent 10px, #fff 0) bottom left;
	background-size: 50% 50%;
	background-repeat: no-repeat;
}

.bevel.round {
	background-image:
		radial-gradient(circle at 0 0, rgba(204,0,0,0) 14px, #fff 15px),
		radial-gradient(circle at 100% 0, rgba(204,0,0,0) 14px, #fff  15px),
		radial-gradient(circle at 100% 100%, rgba(204,0,0,0) 14px, #fff 15px),
		radial-gradient(circle at 0 100%, rgba(204,0,0,0) 14px, #fff 15px);
}

/* esta parte a seguir é somente para testes */
html, body {
	height: 100%;
}

body {
    background: red;
    background-image: radial-gradient(ellipse at center, rgba(255,255,255,.8) 0%,rgba(255,255,255,0) 100%);
    background-repeat: no-repeat;
    animation: cores-animadas 5s infinite;
}

.exemplo {
    width: 200px;
    margin: 15px auto;
    padding:13px 15px;
    color: #0c0c0c;
    line-height:1.5;
}

@keyframes cores-animadas
{
    0%   { background-color: red; }
    20%  { background-color: green; }
    40%  { background-color: blue; }
    60%  { background-color: violet; }
    80%  { background-color: orange; }
    100% { background-color: red; }
}
<div class="exemplo bevel">
   foo bar baz<br>foo bar baz<br>foo bar baz
</div>

<div class="exemplo bevel round">
   foo bar baz<br>foo bar baz<br>foo bar baz
</div>

  • 2

    Great result, and the additional explanation helped a lot!

9

Man, my humble suggestion would be to use a radial-gradient.

In this case, in each corner of a div daughter, would you use a radial-gradient with the same color you use in the parent container.

Thus:

*{
box-sizing: border-box;
}

.internal-radius{
    background-color: #fff;
    width: 300px;
    height: 200px;
    background-image: radial-gradient(circle at left top, lightblue 20px, transparent 0), 
    radial-gradient(circle at left bottom, lightblue 20px, transparent 0),
    radial-gradient(circle at right top, lightblue 20px, transparent 0),
    radial-gradient(circle at right bottom, lightblue 20px, transparent 0);
    width: 100%;
}

.container{
   background-color: lightblue;
   padding: 15px;
   
}
<div class="container">
  <div class="internal-radius"></div>
</div>

Of course, when adding a text within the div who carries the radial-gradient, the text will be on top of the "ball" top. The solution I arranged was to put padding in div.internal-radius of the same value as radial-gradient, that is to say, 20px.

*{
box-sizing: border-box;
}

.internal-radius{
    padding: 20px;
    background-color: #fff;
    width: 300px;
    height: 200px;
    background-image: radial-gradient(circle at left top, lightblue 20px, transparent 0), 
    radial-gradient(circle at left bottom, lightblue 20px, transparent 0),
    radial-gradient(circle at right top, lightblue 20px, transparent 0),
    radial-gradient(circle at right bottom, lightblue 20px, transparent 0);
    width: 100%;
}

.container{
   background-color: lightblue;
   padding: 15px;
   
}
<div class="container">
    <div class="internal-radius">
      Meu nome é Wallace e eu sou bonito
    </div>
</div>

Observing: For the joy of being happy: it works on Internet Explorer 10.

Honestly, it’s not one of the best solutions, but CSS is still like this.

7

The solution I arrived at was similar to that of Mr. Wallace, but I used 4 Divs inside a container to have transparency in the corners.

Face one of the 4 Divs has a radial gradient with the center of the circle aligned at the corner going from transparent to background color.

To better understand follow the example based in that article.

html {
    width: 100%;
    height: 100%;
    background-image: linear-gradient(to bottom, red, blue);
    background-repeat: no-repeat;
}
.container {
    position: relative;
    width: 400px;
    height: 200px;
    margin: 3rem auto;
}

.tl, .tr, .br, .bl {
    width: 200px;
    height: 100px;
    /* background-color: #fff; */
    float: left;
}
.tl {
    background-image: radial-gradient(circle at 0 0,transparent 20px, green 20px);
}
.tr {
    background-image: radial-gradient(circle at 100% 0,transparent 20px, green 20px);
}
.bl {
    background-image: radial-gradient(circle at 0 100%,transparent 20px, green 20px);
}
.br {
    background-image: radial-gradient(circle at 100% 100%,transparent 20px, green 20px);
}
.container p {
    position: absolute;
    text-align: center;
    padding: 20px
}
<div class="container">
    <div class="tl"></div>
    <div class="tr"></div>
    <div class="bl"></div>
    <div class="br"></div>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Animi, dolores.</p>
</div>

-6

Resolve, Border corner Shape the css box { background-color: #3498DB; border-corner-Shape: Scoop; border-Radius: 30px; and width: 200px; height: 200px; }

  • 1

    This does not work in any browser, and nor can it be said that someday this will be approved by W3C, this response at the moment only serves to confuse users. Try to test things locally before posting as an answer, it’s just a hint...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.