简体   繁体   English

如何使CSS3圆角隐藏Chrome / Opera中的溢出

[英]How to make CSS3 rounded corners hide overflow in Chrome/Opera

I need round corners on a parent div to mask content from its childen. 我需要父div上的圆角来掩盖其孩子的内容。 overflow: hidden works in simple situations, but breaks in webkit based browsers and Opera when the parent is positioned relatively or absolutely. overflow: hidden在简单的情况下工作,但在父级相对或绝对定位时,基于webkit的浏览器和Opera中断。

This works in Firefox and IE9: 这适用于Firefox和IE9:

CSS CSS

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

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

HTML HTML

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

Example on JSFiddle 关于JSFiddle的例子

Thanks for the help! 谢谢您的帮助!

UPDATE: The bug causing this issue has been since fixed in Chrome. 更新:导致此问题的错误已在Chrome中修复。 I have not re-tested Opera or Safari however. 我没有重新测试Opera或Safari。

I found another solution for this problem. 我找到了解决这个问题的另一种方案 This looks like another bug in WebKit (or probably Chrome), but it works. 这看起来像WebKit(或可能是Chrome)中的另一个错误,但它确实有效。 All you need to do - is to add a WebKit CSS Mask to the #wrapper element. 您需要做的就是向#wrapper元素添加WebKit CSS Mask You can use a single pixel png image and even include it to the CSS to save a HTTP request. 您可以使用单个像素的png图像,甚至可以将其包含在CSS中以保存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 JSFiddle示例

将z-index添加到border-radius'd项目,它将掩盖其中的内容。

Nevermind everyone, I managed to solve the problem by adding an additional div between the wrapper and box. 没关系所有人,我设法通过在包装器和盒子之间添加一个额外的div来解决问题。

CSS CSS

#wrapper {
    position: absolute;
}

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

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

HTML HTML

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

Thanks everyone who helped! 谢谢大家的帮助!

http://jsfiddle.net/5fwjp/ http://jsfiddle.net/5fwjp/

opacity: 0.99; 不透明度:0.99; on wrapper solve webkit bug 在包装器上解决webkit bug

Seems this one works: 似乎这个工作:

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

http://jsfiddle.net/qWdf6/82/ http://jsfiddle.net/qWdf6/82/

Supported in latest chrome, opera and safari, you can do this: 支持最新的chrome,opera和safari,你可以这样做:

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

You should definitely check out the tool http://bennettfeely.com/clippy/ ! 你一定要查看工具http://bennettfeely.com/clippy/

Not an answer, but this is a filed bug under the Chromium source: http://code.google.com/p/chromium/issues/detail?id=62363 不是答案,但这是Chromium来源下提交的错误: http//code.google.com/p/chromium/issues/detail?id = 62363

Unfortunately, doesn't look like there's anyone working on it. 不幸的是,看起来并没有人在研究它。 :( :(

change the opacity of the parent element with the border and this will re organize the stacked elements. 使用边框更改父元素的不透明度,这将重新组织堆叠元素。 This worked miraculously for me after hours of research and failed attempts. 经过数小时的研究和尝试失败后,这对我来说奇迹般地起作用。 It was as simple as adding an opacity of 0.99 to re organize this paint process of browsers. 就像添加0.99的不透明度来重新组织浏览器的绘制过程一样简单。 Check out http://philipwalton.com/articles/what-no-one-told-you-about-z-index/ 查看http://philipwalton.com/articles/what-no-one-told-you-about-z-index/

based on graycrow's excellent answer... 基于graycrow的优秀答案......

Here's a more real world example that has two cicular divs with some filler content. 这是一个更真实的例子,它有两个带有一些填充内容的cicular div。 I replaced the hard-coded png background with just a hex value, ie 我用十六进制值替换了硬编码的png背景,即

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

is replaced with 被替换为

-webkit-mask-image:#fff;

See this JSFiddle... http://jsfiddle.net/hqLkA/ 看到这个JSFiddle ... http://jsfiddle.net/hqLkA/

As for me none of the solutions worked well, only using: 至于我,没有一个解决方案运作良好,只使用:

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

on the wrapper element did the job. 在包装元素上完成了这项工作。

Here the example: http://jsfiddle.net/gpawlik/qWdf6/74/ 这里的例子是: http//jsfiddle.net/gpawlik/qWdf6/74/

Here look at how I done it; 看看我是怎么做到的; Jsfiddle 的jsfiddle

With the Code I put in, I managed to get it working on Webkit (Chrome/Safari) and Firefox. 通过我提供的代码,我设法让它在Webkit(Chrome / Safari)和Firefox上运行。 I don't know if it works with the latest version of Opera. 我不知道它是否适用于最新版本的Opera。 Yes it does work under the latest version of Opera. 是的,它可以在最新版本的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;
}

If you are looking to create a mask for an image and position the image inside the container don't set the 'position: absolute' attribute. 如果要为图像创建蒙版并将图像放置在容器内,请不要设置“position:absolute”属性。 All you have to do is change the margin-left and margin-right. 您所要做的就是更改margin-left和margin-right。 Chrome/Opera will adhere to the overflow: hidden and border-radius rules. Chrome / Opera将遵循溢出:隐藏和边界半径规则。

// 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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM