Cómo hacer que las esquinas redondeadas CSS3 oculten el desbordamiento en Chrome / Opera


Necesito esquinas redondas en un div padre para enmascarar el contenido de sus hijos. overflow: hidden funciona en situaciones simples, pero se rompe en los navegadores basados en webkit y Opera cuando el padre se coloca relativa o absolutamente.

Esto funciona en Firefox e IE9:

CSS

#wrapper {
  width: 300px;
  height: 300px;
  border-radius: 100px;
  overflow: hidden;
  position: absolute;
}

#box {
  width: 300px;
  height: 300px;
  background-color: #cde;
}

HTML

<div id="wrapper">
  <div id="box"></div>
</div>

Ejemplo en JSFiddle

Gracias por la ayuda!

ACTUALIZACIÓN: El error que causa este problema se ha solucionado desde entonces en Chrome. No he vuelto a probar Opera o Safari sin embargo.

Author: Igor Ivancha, 2011-04-21

12 answers

Encontré otra solución para este problema. Esto parece otro error en WebKit (o probablemente Chrome), pero funciona. Todo lo que necesita hacer - es añadir una máscara CSS WebKit al elemento #wrapper. Puede usar una imagen png de un solo píxel e incluso incluirla en el CSS para guardar una solicitud HTTP.

#wrapper {
width: 300px; height: 300px;
border-radius: 100px;
overflow: hidden;
position: absolute; /* this breaks the overflow:hidden in Chrome/Opera */

/* this fixes the overflow:hidden in Chrome */
-webkit-mask-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC);
}

#box {
width: 300px; height: 300px;
background-color: #cde;
}​

JSFiddle Example

 178
Author: graycrow,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-03 11:47:20

Agrega un z-index a tu elemento border-radius, y enmascarará las cosas dentro de él.

 96
Author: Jackolai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-08 14:37:49

No importa todo el mundo, me las arreglé para resolver el problema mediante la adición de un div adicional entre la envoltura y la caja.

CSS

#wrapper {
    position: absolute;
}

#middle {
    border-radius: 100px;
    overflow: hidden; 
}

#box {
    width: 300px; height: 300px;
    background-color: #cde;
}

HTML

<div id="wrapper">
    <div id="middle">
        <div id="box"></div>
    </div>
</div>

Gracias a todos los que ayudaron!

http://jsfiddle.net/5fwjp /

 53
Author: jmotes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-30 15:58:58

Opacidad: 0.99; on wrapper resuelve el error de webkit

 16
Author: flavi1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-03 12:22:43

Parece que éste funciona:

.wrap {
    -webkit-transform: translateZ(0);
    -webkit-mask-image: -webkit-radial-gradient(circle, white 100%, black 100%);
}

Http://jsfiddle.net/qWdf6/82 /

 15
Author: nakrill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-19 10:47:32

No es una respuesta, pero este es un error archivado bajo la fuente de Chromium: http://code.google.com/p/chromium/issues/detail?id=62363

Desafortunadamente, no parece que haya nadie trabajando en ello. :(

 6
Author: ryan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-06 02:07:13

Compatible con las últimas chrome, opera y safari, puede hacer esto:

-webkit-clip-path: inset(0 0 0 0 round 100px);
clip-path: inset(0 0 0 0 round 100px);

Definitivamente deberías echar un vistazo a la herramienta http://bennettfeely.com/clippy/!

 6
Author: antoni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-26 14:30:07

Cambie la opacidad del elemento padre con el borde y esto reorganizará los elementos apilados. Esto funcionó milagrosamente para mí después de horas de investigación e intentos fallidos. Fue tan simple como agregar una opacidad de 0.99 para reorganizar este proceso de pintura de los navegadores. Echa un vistazo http://philipwalton.com/articles/what-no-one-told-you-about-z-index/

 4
Author: Rami Awar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-12 10:35:11

Basado en la excelente respuesta de Graycrow...

Aquí hay un ejemplo del mundo más real que tiene dos divs ciculares con algún contenido de relleno. Reemplacé el fondo png codificado con solo un valor hexadecimal, es decir,

-=-webkit-mask-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC);

Se sustituye por

-webkit-mask-image:#fff;

Ver este JSFiddle... http://jsfiddle.net/hqLkA /

 2
Author: gts101,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-18 06:15:09

En cuanto a mí, ninguna de las soluciones funcionó bien, solo usando:

-webkit-mask-image: -webkit-radial-gradient(circle, white, black);

En el elemento wrapper hizo el trabajo.

Aquí el ejemplo: http://jsfiddle.net/gpawlik/qWdf6/74/

 2
Author: Grzegorz Pawlik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-09 13:48:03

Aquí mira cómo lo hice; Jsfiddle

Con el Código que puse, me las arreglé para que funcione en Webkit (Chrome/Safari) y Firefox. No se si funciona con la última versión de Opera. Sí funciona bajo la última versión de Opera.

#wrapper {
  width: 300px; height: 300px;
  border-radius: 100px;
  overflow: hidden;
  position: absolute; /* this breaks the overflow:hidden in Chrome/Opera */
}

#box {
  width: 300px; height: 300px;
  background-color: #cde;
  border-radius: 100px;
  -webkit-border-radius: 100px;
  -moz-border-radius: 100px;
  -o-border-radius: 100px;
}
 1
Author: Maze,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-21 10:48:55

Si está buscando crear una máscara para una imagen y posicionar la imagen dentro del contenedor, no establezca el atributo 'position: absolute'. Todo lo que tienes que hacer es cambiar el margen izquierdo y el margen derecho. Chrome / Opera se adherirá a las reglas overflow: hidden y border-radius.

// Breaks in Chrome/Opera.
    .container {
        overflow: hidden;
        border-radius: 50%;
        img {
            position: absolute;
            left: 20px;
            right: 20px;
        }
    }

// Works in Chrome/Opera.
    .container {
        overflow: hidden;
        border-radius: 50%;
        img {
            margin-left: 20px;
            margin-right: 20px;
        }
    }
 0
Author: user6039997,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-09 14:36:47