简体   繁体   English

尝试使用CSS Sprites减少HTTP请求,但现在图像不能正常显示

[英]Trying to Reduce HTTP Requests With CSS Sprites But Now Images Don't Appear Properly

To try and reduce the number of HTTP requests I have combined groups of related images into sprites. 为了尝试减少HTTP请求的数量,我将相关图像组合成了精灵。

I understand the methodology behind it but I can't seem to get it to work correctly for the ones I am working one and would love some help. 我理解它背后的方法,但我似乎无法让它正常工作,我正在工作,并希望得到一些帮助。

Here is the new markup and css (previous markup just had individual img tags wrapped in the anchor tag): 这是新的标记和css(之前的标记只是在锚标记中包含了单独的img标记):

<div id="footer-flags">
    <ul>
        <li class="countryName">Region: </li>
        <li class="active countryFlag" id="uk"><a title="United Kingdom" href="javascript:void(0);"></a></li> 
        <li class="countryFlag" id="us"><a title="United States" target="_blank" href="http://www.us-site.us"></a></li> 
        ... 5 more
    </ul>
</div>

CSS is as seen below: CSS如下所示:

#footer-flags {
    float:right;    
}
#footer-flags ul {
    list-style-type: none;
    margin:-3px 0 0 0;
    overflow:hidden;
    padding:0;
}
#footer-flags ul li {
    background-image: url(/img/flags/flags-sprite.jpg);
    height: 11px;
    width: 14px;
    float:left;
    padding-left:5px;
    padding-top:3px;
}
#footer-flags #de {
    background-position: 0px 0px;
}
#footer-flags #fr {
    background-position: 0px -11px;
}
#footer-flags #hr {
    background-position: 0px -22px;
}
#footer-flags #ie {
    background-position: 0px -33px;
}
#footer-flags #nl {
    background-position: 0px -44px;
}
#footer-flags #uk {
    background-position: 0px -55px;
}
#footer-flags #us {
    background-position: 0px -66px;
}

#footer-flags ul li.active {
    background:url("/img/flags/flag-highlight.png") no-repeat scroll left top transparent;
    margin-right:-5px;
    padding:3px 5px 5px;
}

The sprite image is 14px x 77px (so the flags are positioned on top of each other). 精灵图像为14px x 77px(因此标志位于彼此之上)。

Here is a fiddle if anyone prefers it: http://jsfiddle.net/WNXNz/4/ 如果有人喜欢它,这是一个小提琴: http//jsfiddle.net/WNXNz/4/

The error itself is just that the flag images are strewn all over the place and the links don't seem to be there aswell, ie the images aren't clickable. 错误本身只是标志图像散布在整个地方,链接似乎也不存在,即图像不可点击。

From what I can see I think I have implemented it correctly but it's likely my CSS is a nightmare because I have only had to use a sprite before and it was just for one image. 从我所看到的,我认为我已经正确实现了它,但很可能我的CSS是一场噩梦,因为我之前只需要使用一个精灵,它只是一个图像。

CSS Sprites shouldn't be used for HTML img. CSS Sprites不应该用于HTML img。 You now have empty links : this is bad for accessibility and SEO. 你现在有空链接 :这对可访问性和SEO不利。

Accessibility: you can check the relevant WCAG 2.0 Technique H30: Providing link text that describes the purpose of a link for anchor elements 辅助功能:您可以查看相关的WCAG 2.0技术H30:提供描述锚元素链接目的的链接文本
People that disable images, CSS, use a screen reader or use the High Contrast mode in Windows won't be able to know there's a link there or won't know what it can possibly do. 禁用图像,CSS,使用屏幕阅读器或在Windows中使用高对比度模式的人将无法知道那里有链接或不知道它可能做什么。

SEO: I'm not a SEO specialist, all I can tell is that searchbots will have to guess from the destination what is the purpose of your link, that's not by far the best way to rank well in search engines. 搜索引擎优化:我不是搜索引擎优化专家,我只能说搜索机器人必须从目的地猜测你链接的目的是什么,这不是迄今为止在搜索引擎中排名很好的最佳方式。

That said, CSS sprites are great for background images :) 也就是说,CSS精灵很适合背景图片:)

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

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