简体   繁体   English

在边界半径图像背景上添加边框渗透

[英]Adding border on border radius image background bleed through

I'm adding a black border on images with a blue background and while doing so, it appears to add an ever so noticeable background colored outline on the INSIDE of the border when doing so. 我在蓝色背景的图像上添加了黑色边框,这样做时,它似乎会在边框的内侧添加一个非常明显的背景色轮廓。 Is there a way to get rid of this? 有没有办法摆脱这个? The code I'm using is simple: 我正在使用的代码很简单:

border-radius: 100%;
border: 3px solid rgb(0, 0, 0);

And you can see the background color edging in by adding a radius to any image, as an example: 并且您可以通过向任何图像添加半径来查看背景颜色边缘,例如:

在此输入图像描述

There are several ways to avoid that annoying border-radius background bleed: 有几种方法可以避免恼人的边界半径背景流血:

Method 1: Wrapper with Background Color 方法1:具有背景颜色的包装纸

Put the <img> in a wrapper element, and add padding to the wrapper, with a background color that matches the <img> 's border. <img>放在包装器元素中,并向包装器添加填充,背景颜色与<img>的边框匹配。 This way, any antialiasing that happens on the image will take into account the background color of the wrapper, rather than the background color of the page. 这样,图像上发生的任何抗锯齿都会考虑包装器的背景颜色,而不是页面的背景颜色。

Method 2: Add Background Color to Image 方法2:向图像添加背景颜色

Add a background color to your <img> that matches the border color. <img>添加与边框颜色匹配的背景颜色。 It'll use the <img> 's background color instead of the page background color to do the antialiasing. 它将使用<img>的背景颜色而不是页面背景颜色来进行抗锯齿处理。

Method 3: Use Padding Instead 方法3:使用填充代替

Don't bother with a border. 不要打扰边框。 Add padding to your <img> equal to the border size you want, and add a background color in the border color you want. 添加填充到<img>等于所需的边框大小,并添加所需边框颜色的背景颜色。 This gets you the same effect with the least amount of code. 这可以用最少量的代码获得相同的效果。


Here's a JSFiddle with each of these methods: https://jsfiddle.net/4zpL98au/14/ 以下是这些方法的JSFiddle: https ://jsfiddle.net/4zpL98au/14/

@stvnrynlds gave an interesting answer and I wanted to test this out myself with actual code. @stvnrynlds给出了一个有趣的答案,我想用实际代码自己测试一下。

I have created a snippet below with both versions for comparison. 我在下面创建了一个代码片段,用于比较。

.test1 - uses padding with a wrapper instead of a border .test1 - 使用包装而不是边框​​填充

.test2 - uses border only .test2 - 仅使用边框

 .test1{ border-radius: 50%; width:50px; height: 50px; padding:5px; background:black; } .test1 img{ border-radius:50%; } .test2 img{ border-radius: 50%; border: 5px solid black; } 
 <div class="test1"><img src="http://i.imgur.com/4LM6DpN.gif"/></div> <div class="test2"><img src="http://i.imgur.com/4LM6DpN.gif"/></div> 

Zooming in 500% into the browser you can see the end results: 将500%放大到浏览器中即可看到最终结果:

在此输入图像描述

There's a quite simple solution for this problemen, just by adding a background color: 这个问题有一个非常简单的解决方案,只需添加背景颜色:

background:#000;
border-radius: 100%;
border: 3px solid #000;

在此输入图像描述

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

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