Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can <figure> tag in html5 be used for background images?

I started reading upon html5 and I am trying to work on a project so that I can see how things work. I know that the tag can be use like this:

<figure id="car">
   <img src="img/car.jpg" alt="the car">
   <p>The car</p>
</figure>

Though I need to have 6 of those figures, as such I want to use sprites and (unless I dont know something important) sprites only work if i add the image width css (background-image). So what I would do is something like:

<figure id="car">
   <pre></pre> <!-- add image from css -->
   <p>The car</p>
</figure>

can I use figure tag like that?

Thanks a lot

like image 535
aurel Avatar asked Dec 17 '22 08:12

aurel


2 Answers

The figure and figcaption elements are fairly specific in their semantic purpose. If you don't care for semantics, there is really not much of a reason to use them. Divs will work fine. Here is how figure should be used:

<figure>
  <img src="/image.jpg" alt="A description of what this image is">
  <figcaption>A description that may or may not describe the image specifically and accompanies content from within the article.</figcaption>
</figure>

edit: I should mention that a pre with a background is likely not how this is intended to be used and will not help people with screen readers at all. Bots for search engines may respond differently as well, which could be a negative for you.

like image 106
Steve Adams Avatar answered Jan 13 '23 10:01

Steve Adams


Your use case is why object-fit was created.

img {
    object-fit: cover;
}

See this stackoverflow question.

like image 44
nikk wong Avatar answered Jan 13 '23 11:01

nikk wong