可以使用 display:none 隐藏任何内容吗?
使用 display:none
可以暂时或永久隐藏任何内容吗?在动态站点中,页面的许多组件来自不同的插件等,很多时候,如果客户端不需要页面上的任何内容,那么我使用 dispaly:none
来隐藏页面中的内容。我不会从实际源中删除东西,因为客户可以回来要求再次启用该东西。
那么,如果我永远使用 Display:none
保持任何元素隐藏,那么让东西从 display:none 中隐藏的优点和缺点是什么?
在搜索引擎优化、屏幕阅读器、辅助功能等方面有什么缺点吗?
Is it ok to hide any thing temporarily or forever using display:none
? in dynamic site where many component of page coming from different plugins etc and many time if client doesn't want any thing on page then i use dispaly:none
to hide things from page . I do not remove thing from actual source because client can come back ask to enable that thing again.
so what are pros and cons to keep things hide from display:none if i keep any element hide using Display:none
forever?
is there any cons in terms of SEO, Screen reader, Accessibility etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里可能还值得一提的是,一些搜索引擎(例如谷歌)确实会评估隐藏内容。
使用
display:none;
隐藏大量文本是许多搜索引擎认为是关键字垃圾邮件的事情之一。:)
It's probably also worth mentioning here that some search engines (Google for example) do take stock of hidden content.
Hiding huge amounts of text using
display:none;
is one of the things many search engines pick up on as keyword spamming.:)
当您执行客户端 Ajax 时,使用“display:none”隐藏/显示内容是有意义的。这样您就可以切换视图/选项卡,而无需进行服务器往返。
当存在安全隐患时,需要从页面标记中实际删除某些内容。如果用户无权查看某些敏感信息,那么当他们单击“显示源”时,这些信息就不应该出现。
It makes sense to hide/show stuff with 'display:none' when you do client-side Ajax. This way you can switch views/tabs without doing server round-trip.
It is needed to actually remove something from the page markup when there are security implications. If a user doesn't have the right to see some sensitive information, it shouldn't be there when they click "Display source".
display: none
适合在人们关闭 CSS 或使用不支持 CSS 的浏览器时隐藏您希望可见的内容。display: none
is good to hide things you want visible when people turn css off or use browsers that doesnt support css.就可访问性而言,很有可能用“display: none;”隐藏了一些东西。屏幕阅读器不会读取。如果您打算这样做,这可能是可以接受的。
仅隐藏屏幕阅读器/css-offers 内容的可能替代方法是使用此类:
以及以下 HTML:
有关隐藏技术的完整信息:
http://www.access-matters.com /2005/04/23/屏幕阅读器测试结果/
As far as accessibility goes, there is a strong chance that something hidden with "display: none;" will NOT be read by a screen-reader. This may be acceptable if you intend for it to be that way.
A possible alternative to hiding content for screen-readers/css-offers only is to use this class:
And the following HTML:
For full information on hiding techniques:
http://www.access-matters.com/2005/04/23/screen-reader-test-results/
如果客户希望将其删除,请创建该页面的备份,并发布实际已删除该页面的页面。不要用 CSS 代替实际删除项目。如果他们决定将来需要它,那么就进去将您的备份交换为实时副本。如果您正在处理动态输出(在 PHP 或类似技术的情况下),您可以使用注释来停止该特定输出,这样它们就不会包含在响应中。
If the client wants it removed, then create a backup of the page, and post a page that actually has it removed. Don't substitute CSS for actually removing an item. If they decide they want it in the future, then go in and swap your backup for your live copy. If you're dealing with dynamic output (in the case of PHP or a comparable technology) you could stop that particular output with comments so they're never included in the response.
优点:非常容易做到
缺点:
您可以简单地在服务器端“注释”这些部分,以节省服务器上的大量处理、带宽等。
Pros: Very easy to do
Cons:
You can simply "comment" these section server side to save a lot of processing on the server, bandwidth, etc.