简体   繁体   English

随机1px背景图片未在Chrome中显示

[英]Random 1px background image not showing in Chrome

I have a navbar with links using a 1px wide, no-repeat, right-aligned background image as a divider, but for some reason the background image doesn't appear for one of the links in Chrome - in this case between "Athletics" and "Bowling". 我有一个导航栏,其中的链接使用1px宽,无重复,右对齐的背景图片作为分隔线,但是由于某些原因,Chrome中的链接之一未显示背景图片-在这种情况下为“田径”和“保龄球”。

It works in Firefox and Explorer, when zooming in and out in Chrome, when changing the font-size, when reducing the character length of the link, etc. so I believe it has something to do with how Chrome renders the background image. 它在Firefox和Explorer中工作,在Chrome中放大和缩小,更改字体大小,减少链接的字符长度等时,我相信它与Chrome渲染背景图像有关。

I have tried different suggestions such as setting the background-size to contain/cover/100% and image-rendering to pixelated (supposedly this only targets regular images and not background images), but I can't get it to work. 我尝试了不同的建议,例如将背景尺寸设置为包含/覆盖/ 100%,并将图像渲染设置为像素化(假设这仅针对常规图像而不是背景图像),但是我无法使其正常工作。

How can I ensure that all background images are displayed correctly in Chrome? 如何确保所有背景图片都能在Chrome中正确显示?

CSS 的CSS

ul {
    display:flex;
    list-style:none;
}

ul a {
    background:url(sports-div.png) right bottom no-repeat;
    display:inline-block;
    font-family:sans-serif;
    font-size:13px;
    padding:5px;
}

HTML 的HTML

<ul>
<li><a href="#">Football</a></li>
<li><a href="#">Basketball</a></li>
<li><a href="#">Tennis</a></li>
<li><a href="#">Ice hockey</a></li>
<li><a href="#">Volleyball</a></li>
<li><a href="#">Badminton</a></li>
<li><a href="#">Snooker</a></li>
<li><a href="#">Athletics</a></li>
<li><a href="#">Bowling</a></li>
<li><a href="#">Cycling</a></li>
</ul>

http://jsfiddle.net/zc29b8no/1/ http://jsfiddle.net/zc29b8no/1/

The technical explanation is rather long and boring. 技术解释相当冗长而乏味。

But, the gist of it is: it has to do with subpixel rendering . 但是,要点是: 它与亚像素渲染有关

Your anchors' dimensions are not clear cut pixels and the browser needs to approximate what to render in each pixel. 您的锚点尺寸不是清晰的像素,浏览器需要大致估算每个像素的渲染量。 Your 1px image falls on the wrong side of rounding . 您的1px图片落在舍入的错误一侧。 To make sure it renders inside the element's painted background, you need to replace the right alignment in background-position (which translates to 100% ) with calc(100% - 1px) : 为了确保它呈现在元素的绘制背景内,您需要用calc(100% - 1px)替换background-positionright对齐方式(转换为100% calc(100% - 1px)

 ul { display:flex; list-style:none; } ul a { background:url(https://i.imgur.com/mCra304.png) calc(100% - 1px) bottom no-repeat; display:inline-block; font-family:sans-serif; font-size:13px; padding:5px; } 
 <ul> <li><a href="#">Football</a></li> <li><a href="#">Basketball</a></li> <li><a href="#">Tennis</a></li> <li><a href="#">Ice hockey</a></li> <li><a href="#">Volleyball</a></li> <li><a href="#">Badminton</a></li> <li><a href="#">Snooker</a></li> <li><a href="#">Athletics</a></li> <li><a href="#">Bowling</a></li> <li><a href="#">Cycling</a></li> </ul> 

Another solution would be to make the image 2px wide, but it might still have variable width. 另一个解决方案是将图像设置为2px宽,但它可能仍具有可变的宽度。

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

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