简体   繁体   English

带有图片背景的Divs与img标签

[英]Divs with image backgrounds vs. img tags

I was wondering what is the best practice regarding divs with backgrounds and img tags. 我想知道关于带背景和img标签的div的最佳实践是什么。 I understand that divs with backgrounds can have stuff on top of them and what not, but if its the case with just have an image, which is the preferred method? 我知道,具有背景的div可以在其顶部添加内容,但不能包含其他内容,但是如果仅包含图片,则是首选方法? Maybe a better question is.. are img tags obsolete? 也许更好的问题是.. img标签是否已过时? When you have an image thats a link should you use an img tag or a div? 当您的图像多数民众赞成在链接时,您应该使用img标签还是div?

Thanks! 谢谢! Matt Mueller 马特·穆勒(Matt Mueller)

Div backgrounds should be just that: background images for style. Div的背景应该就是:风格的背景图片。 img tags should be for when you're displaying an actual image as an image itself, say you are showing a picture of something. img标签应该用于当您将实际图像显示为图像本身时(例如,您正在显示某物的图片)。 you should use an image tag and not a div bg 您应该使用图片标签而不是div bg

Think about it as semantic markup: 将其视为语义标记:

  1. If it is an "image" on the page, as far as the meaning of the page, use the img tag. 如果它是页面上的“图像”,就页面的含义而言,请使用img标签。

  2. If it something that is not that significant to the page's meaning, ie. 如果它对页面的意义不重要,即 background image, use a background image on any sort of element (not just a div). 背景图片,请在任何类型的元素(不仅是div)上使用背景图片。

This difference really doesn't matter to how the page displays in most browsers, but has a different meaning to those who aren't interpreting the images visually. 这种差异实际上与大多数浏览器中页面的显示方式无关紧要,但与那些不以可视方式解释图像的人具有不同的含义。

Try to imagine how the elements will be interpreted by visually impaired. 尝试想象一下视觉障碍者将如何解释这些元素。

There also may be a slightly different behavior by search engines-- I don't know whether search engines will pick up background images for their image search. 搜索引擎的行为也可能略有不同-我不知道搜索引擎是否会拾取背景图像进行图像搜索。 If you really want the image out there, an img tag is safer. 如果您确实需要图像,则img标签更安全。

is the image for layout or content? 是用于布局还是内容的图像?

if the image is layout related i would use CSS and have it in a div ...if its content related i would have it in an img ... 如果图像与布局有关,则我将使用CSS并将其放在div中...如果与内容相关,则将其在img中...

Hope that helps! 希望有帮助!

A good way to look at this is to view your site with stylesheets turned off. 解决此问题的一种好方法是关闭样式表来查看您的网站。 You'll quickly find out that all the DIV tags with background images do not appear. 您会很快发现没有显示带有背景图像的所有DIV标签。 All of your IMG tag images are right where they should be. 您所有的IMG标签图像都应正确放置在正确的位置。 I would use DIV tags with background images for all aspects of site design and layout and use IMG tags for everything else. 我会将DIV标签与背景图片一起用于网站设计和布局的所有方面,并将IMG标签用于其他所有内容。

IMG tags have alt properties and title properties. IMG标签具有alt属性和title属性。 These are used in place of the image when it doesn't load or in place of the image in text only or screen reader type browsers. 当不加载图像时,它们被用于代替图像,或者仅在文本或屏幕阅读器类型的浏览器中代替图像。

IMG tag is not obsolete. IMG标签不是过时的。 You use it with dynamic images, that come and go from your system. 您可以将其与来自系统的动态图像一起使用。

Background images on divs are useful when you have a static set of images that are part of your design. 当您的设计中有一组静态图像时,div上的背景图像会很有用。 Sometimes you can merge them into one big image to minimize load time and the number of HTTP requests pro page. 有时,您可以将它们合并为一张大图,以最大程度减少加载时间和Pro页面的HTTP请求数。

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

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