最佳图像替换技术
在 CSS 中进行图像替换的最佳(如跨浏览器)技术是什么? 我使用 sprites 进行导航,但我希望标记保持 SEO 友好。 给定以下 HTML 结构...
<div id="menu">
<ul>
<li><a href="#">Test</a></li>
<li><a href="#">Tester</a></li>
<li><a href="#">Testing Testing</a></li>
</ul>
</div>
仅使用 CSS 将文本替换为背景图像的最佳方法是什么?
我目前正在使用这个...
text-indent: -9999px;
但是,它在 CSS 打开且图像关闭的情况下失败。
What is the best (as in cross-browser) technique to do image replacement in CSS? I am using sprites to do my navigation, but I want the markup to remain SEO friendly. Given the following HTML structure...
<div id="menu">
<ul>
<li><a href="#">Test</a></li>
<li><a href="#">Tester</a></li>
<li><a href="#">Testing Testing</a></li>
</ul>
</div>
What is the best way to replace the text with a background image using CSS only?
I am currently using this...
text-indent: -9999px;
But, it fails with CSS on, and images off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
display:block 很重要,否则你的宽度和高度可能看起来不正确。
The display:block is important or else your width and height may not look right.
这是我用来用图像替换徽标文本的代码,同时将文本保留在代码中但不向用户显示(这是 Google 批准的)。 在此处查看完整的示例:
http://discretiondesigns.com/overflow/imagereplacement/
这是完整的代码(图像可以在上面的链接中找到 - 图像可以有不同的尺寸 - 整个图像是可点击的,并且在悬停时会发生变化):
This is the code I use for replacing logo text with an image while keeping the text in the code but not shown to the user (this is Google approved). View the completed example here:
http://discretiondesigns.com/overflow/imagereplacement/
Here's the full code (images can be found at the above link - images can be varying sizes - the entire image is clickable and changes upon hover):
无论 css/images 的设置如何,这都可以工作:
http://www.tjkdesign.com /articles/tip.asp
This is touted to work no matter the settings of css/images:
http://www.tjkdesign.com/articles/tip.asp
CSS Tricks 在此处有关于该主题的最详细的帖子之一,
它们展示了各种技术。 解决 css 打开和图像关闭问题的方法是:
HTML:
CSS Tricks 是有关该主题的最详细的页面之一 这里
他们展示了各种技术。 解决 css 打开和图像关闭问题的方法是技术#8:
HTML:
CSS:
编辑:将代码更新为提供的示例。
PS:我没有测试上面的代码。
CSS Tricks has one of the most detailed posts on the subject here
They show various techniques. The one that solves your problem of css on and images off is:
HTML:
CSS Tricks has one of the most detailed pages on the subject here
They show various techniques. The one that solves your problem of css on and images off is the technique #8:
HTML:
CSS:
EDIT: Updated the code to the sample provided.
PS: I didn't test the code above.
CSS:
HTML:
编辑:将链接文本添加回...,因为它被错过了。 :-)
CSS:
HTML:
Edit: added the link text back in... 'cause it was missed. :-)
如果这是 html:
这是 css:
那么图像将是 300 像素宽,每个选项卡将是 100 像素宽。
If this is the html:
And this is the css:
The image would then be 300px wide, and each tab would be 100px wide.
2008 年,Google 在 An Event Apart 上的演示中明确表示,有效图像替换不会受到 Google 的处罚。 查看 Mezzoblue 的帖子
基本上,只要您的图像将文本替换为其中包含相同文本的文本,它将被视为有效并且不会试图欺骗搜索引擎。 他们如何确定图像是否有效? 我不知道...OCR? 人工审核?
至于 CSS 打开/图像关闭,没有完美的解决方案,它们都需要额外的非语义标记。 请参阅 css-tricks 链接,该链接要求发布有关不同技术的内容。 我个人并不关心使用 CSS 浏览但不使用图像的极少数用户。
您的选择很简单。 额外的标记,或者不关心 css 打开/图像关闭。
In 2008, Google's presentation at An Event Apart made it clear that valid image replacement will not be penalized by Google. See Mezzoblue's post about it
Basically, as long as the image you replace the text with has the same text in it, it will be considered valid and not trying to cheat search engines. How do they determine whether the image is valid or not? I have no idea... OCR? Manual review?
As far as CSS on/images off, there is no perfect solution, all of them require extra non-semantic markup. See the css-tricks link that beggs posted on the different techniques. I personally do not bother with the very small percentage of users who browse with CSS but no images.
Your choice is simple. Extra markup, or don't care about css on/images off.
背景图像通常应用于
链接,为整个可点击区域提供图像。 要隐藏文本,您可以为
text-indent
使用一个非常大的负值。The background image is usally applied to the
<a>
link, giving the entire clickable area an image. To hide the text you can use a very big negative value fortext-indent
.我刚刚想出这个,它似乎适用于所有现代浏览器,我刚刚在(IE8/兼容性、Chrome、Safari、Moz)
HTML
CSS
Pro 上测试了它:
我能想到的最大缺点是 css off/images on 情况,因为你只会发送透明的 gif。
也许可以编写一些 javascript 来帮助解决这个问题,用背景图像 css 属性替换所有图像源。 但这只有在浏览器仍然将 css 属性附加到元素然后忽略它们的情况下才有效。 我不知道是不是这样,我得测试一下。 您还想开发一个基于 javascript 的测试来查看 css 是否应用于页面(也许检查某些测试元素的位置)。
顺便说一句,我想知道,谁使用没有样式表的图像? 某种手机之类的?
编辑:
基于下面的评论...内联样式hrm...也许我应该制作一个像
生成这样的代码:
HTML
then just Attach some CSS in the stylesheet
这是我正在使用的一种较旧的技术,但不确定我在哪里找到它。 它适用于 CSS 打开/图像关闭、CSS 关闭/图像打开、CSS 打开/图像打开。
如果关闭 CSS/关闭图像的用户访问,他们会看到双倍的文本。 如果搜索引擎蜘蛛访问,他们会看到替代文本和常规文本,智能蜘蛛可以轻松识别它是什么,这是一种无辜的图像替换技术。
因此,这种技术对于屏幕阅读器来说最糟糕,因为会读取替代文本,但这些用户应该能够跳到下一段,这就是为什么我卡住了
围绕“我的文字”。
其他所有关闭 CSS 和图像的人都是某种机器人,对吧?
I just came up with this, it seems to work in all modern browsers, I just tested it then on (IE8/compatibility, Chrome, Safari, Moz)
HTML
CSS
Pro's:
The biggest disadvantage that I can think of is the css off/images on situation, because you'll only send a transparent gif.
It might be possible to write a little javascript to help out with this, replacing all the image sources with their background-image css properties. But this would only work if the browsers still attaches css properties to elements and then ignores them. I don't know if this is the case or not, I'll have to test it out. You'd also want to develop a javascript-based test to see if css is being applied to the page (maybe check the position of some test element).
btw, I'd like to know, who uses images without stylesheets? some kind of mobile phone or something?
edit:
Based on comment below... inline styles hrm... maybe I should just make a php helper function like
<?php echo css_image('image_id','my text','image_url');?>
to generate some code like this:HTML
then just attach some CSS in the stylesheet
it's an older technique that I'm using, not sure where I found it though. It works with CSS on/images off, CSS off/images on, CSS on/images on.
If a user with CSS off/images off visits, they'll see doubled up text. If a search engine spider visits, they'll see alt text and regular text, an intelligent spider could easily identify this for what it is, an innocent image replacement technique.
So, this technique is worst for screen readers, since alt text is read, but these users should be able to skip to the next paragraph, which is why I stuck
<p></p>
around "my text".Everyone else with both CSS and images turned off is some kind of bot, right?