简体   繁体   English

CSS背景颜色和图像冲突

[英]CSS Background Colour and Image Conflicting

I have a piece of CSS that I've used a number of times that combines a background image and a background colour and, to this point, I've not had a problem with it. 我有一段CSS,我已经使用了很多次,将背景图像和背景颜色结合在一起,到目前为止,我还没有遇到任何问题。 However, in this instance, I've got a weird issue with mobile browsers where it's shrinking the background image by 1px all the way around, revealing the background colour where I don't want it to be seen! 但是,在这种情况下,移动浏览器出现了一个奇怪的问题,即它一直将背景图像缩小1像素,从而显示了我不希望看到的背景颜色!

The CSS: CSS:

.navlink {
background-color: #000000;
color: #FFFFFF;
text-decoration: none;
height: 20px;
width: 200px;
margin: 0;
padding: 0;
float: left;
background-image: url(images/navarrowblank.gif);
background-position: top right;
background-repeat: no-repeat;
}

It doesn't matter what HTML element the class is applied to. 将类应用于哪个HTML元素都没有关系。 It also doesn't matter if I'm using an iOS device or an Android device (but if I try the same code in any desktop browser [and it's been checked in all the obvious ones!], I don't see any issues at all). 我使用的是iOS设备还是Android设备也都没有关系(但是,如果我在任何桌面浏览器中尝试相同的代码[并且所有显而易见的代码都已对其进行检查!),我看不到任何问题完全没有)。

I have already tried background-size and setting the object's border to 0 - neither of which resolved the issue. 我已经尝试过background-size并将对象的边框设置为0-都不能解决问题。

The image itself is 24px by 25px but appears to be displaying at 18px by 23px. 图片本身为24px x 25px,但显示为18px x 23px。

Any suggestions, or should I just bite the bullet and create the whole background as an image and lose the background colour? 有什么建议,还是我应该咬一下子弹,将整个背景创建为图像,并失去背景颜色?

JSFiddle set up here: http://jsfiddle.net/frustratedwithdotnet/a64VP/2/ (note: red border removed) JSFiddle在这里设置: http : //jsfiddle.net/frustratedwithdotnet/a64VP/2/ (注意:红色边框已删除)

In this case, you can not specify a background color and image on the same level. 在这种情况下,您不能在同一级别上指定背景颜色和图像。 The hierarchy pretty much says that images take priority over colors. 层次结构几乎说图像优先于颜色。 To get the effect you are looking for, you'll need to wrap the element in a div and style it with your background color. 为了获得想要的效果,您需要将元素包装成div并使用背景颜色设置样式。

So, you'll be looking at something like: 因此,您将看到以下内容:

.wrap { background: #000; }
.nav-link { background: url(images/navarrowblank.gif) top left no-repeat; }

<div class="wrap"><div class="nav-link">Lorem Ipsum</div></div>

Hope this helps. 希望这可以帮助。

After four days of hair-tugging, and with no explanations available as to why code that I've used dozens of times in other places should suddenly go doolally, I've solved it by making the background image the full width of the container (instead of just 24px by 25px) and filling it black instead of relying on the background colour to do that (leaving the right hand end white). 经过四天的摸索,并且没有任何解释说明为什么我在其他地方使用过数十次的代码突然突然消失了,我通过使背景图像达到容器的整个宽度来解决了该问题(而不是仅仅24px x 25px),然后将其填充为黑色,而不是依靠背景颜色(右端保留为白色)。

Not happy with that solution, but absent anything else it's the best I've got for now. 对这种解决方案不满意,但除此之外,这是我目前所拥有的最好的解决方案。

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

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