Use of picture element as a container instead of a div

raratiru
<div class="container">
  <img src="photo.jpg" alt="A Photo"/>
</div>

According to the picture element specification:

Zero or more source elements, followed by one img element, optionally intermixed with script-supporting elements.

The picture element is a container which provides multiple sources to its contained img element... It represents its children.

Therefore, is it safe to assume that the following HTML is more semantically correct?

<picture>
  <img src="photo.jpg" alt="A Photo"/>
</picture>
gavgrif

The semantically corect contaner for an image element is the figure element - documentation - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure

 <figure>
  <img src="photo.jpg" alt="A Photo"/>
</figure>

This element has an optional figcaption element that can be either the first child or the last child - to provide a caption (contextual text related to the image).

as the first child (above the image

 <figure>
  <figcaption>A photo</figcaption>
  <img src="photo.jpg" alt="A Photo"/>
</figure>

As the last child - below the image

 <figure>
  <img src="photo.jpg" alt="A Photo"/>
  <figcaption>A photo</figcaption>
</figure>

Note that you can style the figure and contained figcaption and img elements to be whatever style you want.

figure {
 text-align: center;
}

figcaption {
  margin-bottom: 8px;
  color: green;
}
figure img {
 border: solid 1px green;
 padding: 4px;
 border-radius: 4px;
}
 <figure>
  <figcaption>A Fluffy kitten</figcaption>
  <img src="https://i.pinimg.com/originals/3e/6b/cd/3e6bcdc46881f5355163f9783c44a985.jpg" alt="A fluffy kitten" width="150"/>
</figure>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Use image as container for text instead of div

How to use <i> element for hovering container div

Make image in picture element fit its container

JS/jQuery: When prepending a dynamic element, how to force the container div to extend upwards instead of downwards?

Append an object instead of an element to a div

How to use picture element in angular Material cards

Failed to use <picture> and <source> element to switch pictures

What to use instead of .container in Gumby2

How to use Container instead of ObjectFactory in StructureMap ServiceActivator?

How can we use <picture> tag instead of "background-image"?

where to put my icon picture for expo to use instead of default one?

Scrolling to element inside scrollable container div not working

How to lock an element to the bottom of a div that is in a container (square)?

Resize container DIV to fit element height

Div element for new section hidden behind container

Align picture element in center of parent div keeps image aligned to left

How to make a picture using only one element - div?

Img element is being selected instead of the parent div

Bringing a picture to the front in an image stack, by use of an anchor element, through user

Use one-element STL container as cache?

Equality of container object identity instead of element-wise

Position fixed within container element instead of the browser / viewport

Use a DIV as a background for another element

Can div element use formControlName?

Is there an 'alt' attribute for the <picture> tag? — Use case: picture element without <img src="">

When targeting a Picture element in CSS, should we use img or picture selector?

Get the id of the first div element inside a container div

Allow a div to cover the whole page instead of the area within the container

Use of :host selector vs container div