Is using the logo tag in sprites good or bad?

2020-05-24 23:54发布

When building web pages, one of my colleagues displays any logo using the CSS background-image property, rather than embedding the image using an HTML <img> tag. The colleague reported it was to reduce the number of HTTP requests. He also showed me an image sprite and said that Google displays its logo with sprite images.

I don't agree with his approach and showed him that the main Google.com page loads their logo in an <img> tag.

Which is a better practice?

EDIT: Facebook also do the same thing on their homepage, loading the logo in an img tag while on their profile pages they display their logo using a CSS sprite.

From this my conclusion was that perhaps you should load your main logo in an img tag while for the other logos such as in a footer or subpage you might want to load them in the background using CSS sprites.

UPDATED: I am routinely loading logos with img tags and also know why we might use sprites. My main question is: if you have three or more logos on a page, what is the better way to load them?

14条回答
何必那么认真
2楼-- · 2020-05-25 00:28

When an image has semantical meaning, so it is considered to be content, use an IMG tag, without sprites, and a correctly set up ALT.

When an image is just decoration, like the background of a box, background of a button, background of a menu option, etc., it has no semantical meaning, so you can just use it as a background of a SPAN, DIV, etc. from CSS. You can use sprites in this case.

The reason Image Sprite is a best practice is because of how the HTTP protocol works, and because we want to minimize the time a webpage takes to load (there are many reasons why you should want to make your site load faster, one of them is because Google incorporated a while ago site speed in it’s ranking algorithm) HTTP is a non-connection based protocol, this means that for every request the browser does a new connection has to be done and the route to the server has to be recalculated. So, if every image was in the same file, the browser saves multiple requests.

Every request the browser does is divided in steps: DNS lookup, connecting, sending, waiting, receiving. We can use firebug to see all of the requests done during the loading of a webpage.

enter image description here

I took a WordPress theme and measured the time taken for every image resource at each step (ok, Firebug did that, not me) and calculated that 38.8% of the time corresponds to latency (in this case latency = DNS lookup + connecting + sending), while only 14.4% corresponds to downloading data (the 46.7% remaining corresponds to waiting for the server to respond). Latency time should be minimized, since it’s not time invested in actually downloading the resources the browser needs to show.

Steps DNS lookup, connecting and sending are redundant for every static image request on the same server. So, how can we cut them off? Guess what? Using image sprites! Every image request will be grouped in only one, resulting in only one set of latency time for all the image kilobytes the browser is going to download.

查看更多
神经病院院长
3楼-- · 2020-05-25 00:31

I generally prefer defining logos in an IMG tag. A simple, practical advantage is that if someone prints your page, the logo will show up, if the logo was set as a background sprite, it would not.

Viewing a site with CSS disabled is helpful when making decisions like this. It gives you a good idea about the lowest common denominator viewing experience of your site. If your site make sense under those circumstances, it's like having your house built on rock.

查看更多
爷的心禁止访问
4楼-- · 2020-05-25 00:34

There is no reason not to use sprites for optimization, even if the image logo is semantic information. The overall meaning (sēmantikós) of the page is not lost if the use of sprites is done adeptly, namely by use of the image within a properly identified container. There is no universally agreed upon semantic web – semantics is a philosophical art, and open to heavy interpretation.

Perhaps arguably, more appropriate than an image for a logo is a heading (h1, by its very definition) set as a block element with visibility hidden. The text becomes searchable, carries semantic meaning for the page (allowing a proper replacement of content in text-based browsers and screen readers), and the background of the h1 set to the positioning of the sprite as necessary.

There are arguments for both sides, at least from the SEO point of view – each image is a searchable object, and an opportunity for inbound traffic; but each http request takes up bandwidth and slows the load time of the page, making it less optimized and thus lower-ranking.

edit:

When you have 3 or more logos, the point of http requests is moot; subsequent logos would presumably be the same image, even if resized. If not, reconsider why you're using the logo thusly.

If they are 3 different logos for the same company, they are no longer semantic, and no longer affect the meaning of the page. This is akin to saying a page about ABC Company is also about their DBA (Registered business name, "Doing Business As") XYZ Corporation, which is poor practice. Having a page for the company, and then another for the DBA is best practice in this situation. Either the page is for the main company, or for its subsidiary LLC. Even when you're saying "ABC Company is:" then list each DBA with their respective logos, the page's meaning is the description of ABC Company, which should have the h1 or h2 carrying the ABC Company logo, with lower level headings carrying the other logos; at this point, reducing the load time is priority, and not granting meaning to the other logos. Creating searchable content for the DBAs should be relegated to h3 and lower headings.

查看更多
Anthone
5楼-- · 2020-05-25 00:36

you can use a sprite in an img element via the css clip: property. using sprites correctly is always a good thing for optimization. sometimes it's not practical. that is a judgement call you have to make for each different circumstance (site) that you come across.

查看更多
萌系小妹纸
6楼-- · 2020-05-25 00:39

A logo is part of the content of your site, therefore it should be in an img tag, not as a background image. It will help to increase SEO (adding a title and alt attribute will too) and the reason companies like Google, Facebook, et al put their image in a sprite is for load times - not SEO enhancement.

Does your company have the same SE rank as Google or Facebook? No. Until then, keep putting the logo in an img tag where it belongs. When your site is consistently the most viewed site on the internet, then you can start thinking about performance more than SEO.

Also, as an aside, if the logo ever had a tweak (size, color, etc), the sprite would have to be recreated as well as the CSS. If it was just an img tag, this hassle is nonexistent.

查看更多
霸刀☆藐视天下
7楼-- · 2020-05-25 00:40

I think you should stick with the <img> tag until Google invents "Background Image Search" -- a service that searches background images, breaks sprites into individual images and intelligently distinguishes between decorative and meaningful images by analyzing CSS.

Edit: Ask yourself this question: is your logo something you want to emphasize upon; or is it just another decoration. Answer and implement accordingly.

查看更多
登录 后发表回答