简体   繁体   English

将div的边缘与圆角混合

[英]Blending the edges of a div with rounded corners

I'm trying to get a smooth gradient on all edges of a div with rounded corners. 我试图在圆角的div的所有边缘上获得平滑渐变。

Here's a JSfiddle of what I have so far. 这是我到目前为止的JSfiddle。

HTML: HTML:

<div id=container>
  <div id="test">
  </div>
</div>

CSS: CSS:

#container {
  padding-top:150px;
  height: 350px;
  background-color: black;
}

#test {
  width: 200px;
  height:200px;
  margin-left:auto;
  margin-right:auto;
  background-color: rgba(247, 250, 252, 0.8);
  box-shadow: 0 0 100px 100px rgba(247, 250, 252, 0.8);
  border-radius: 20px;
}

In Chrome (54.0.2840.71 m) it looks basically like what I want, but there are artifacts at the rounded corners ( here is an image of what it looks like for me, the color banding is just from the image compression ). 在Chrome(54.0.2840.71米)中,它看起来基本上就像我想要的那样,但是在圆角处有一些文物( 这里是我看起来像的图像,颜色条带只是来自图像压缩 )。 In Firefox the color of the shadow and div don't match up exactly at the edge and the artifacts are still there, and in Edge the colors match but the artifacts are worse. 在Firefox中,阴影和div的颜色在边缘处不完全匹配,并且工件仍然存在,而在Edge中颜色匹配但是工件更糟。

Is there some way to get rid of these artifacts, or a different (simple) method to get a similar effect? 有没有办法摆脱这些工件,或者一种不同的(简单)方法来获得类似的效果? I don't care about older browsers and would be happy even if it only works in chrome. 我不关心旧浏览器,即使它只适用于chrome,也会很开心。

I figured out a way to at least hide the artifacts: Adding a filter: blur(5px) (the necessary amount seems to depend on the size of the div) to the div with the box-shadow and rounded corners. 我找到了一种方法来至少隐藏瑕疵:添加一个filter: blur(5px) (必要的数量似乎取决于div的大小)到div的阴影和圆角。 Because the goal is a blurred edge anyway this looks perfect. 因为目标是模糊的边缘,这看起来很完美。 It works in current versions of Chrome, Edge and Firefox (the blur even smears out the color mismatch in Firefox). 它适用于当前版本的Chrome,Edge和Firefox(模糊甚至抹掉了Firefox中的颜色​​不匹配)。

The blur acts on all child elements so we need a separate div that provides the background color, shadow and blur. 模糊处理所有子元素,因此我们需要一个单独的div来提供背景颜色,阴影和模糊。 The main one gets background-color: transparent so the backgrounds don't overlap. 主要获得background-color: transparent因此背景不重叠。 The blurred div is positioned exactly overlapping the main one with position:absolute and given a lower z-index so it doesn't obstruct the content. 模糊div的位置与主要位置完全重叠, position:absolute ,给定较低的z-index因此不会阻碍内容。

Here's a jsfiddle showing the solution. 这是一个显示解决方案的jsfiddle。

HTML: HTML:

<div id=container>
  <div id="test">
    <div id="blur">
    </div>
    test
  </div>
</div>

CSS: CSS:

#container {
  z-index:-100;
  height: 500px;
  width: 500px;
  background-color: black;
}

#test {
  z-index:1;
  position: relative;
  top: 150px;
  width: 200px;
  height:200px;
  margin-left:auto;
  margin-right:auto;
  background-color: transparent;
}
#blur {
  z-index: -10;
  width: 200px;
  height:200px;
  position:absolute;
  top:0px;
  right:0px;
  bottom:0px;
  left:0px;
  background-color: rgba(247, 250, 252, 0.8);
  box-shadow: 0 0 100px 100px rgba(247, 250, 252, 0.8);
  border-radius: 20px;
  filter: blur(5px);
}

And here is the site I needed this for, I think it looks pretty good for programmer art. 这里是我需要的网站 ,我觉得它对程序员艺术看起来很不错。

I pretty like your work, improved it a little :) code 我非常喜欢你的工作,改进了一点:) code

https://jsfiddle.net/wrd2b1xr/5/ https://jsfiddle.net/wrd2b1xr/5/

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

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