Skip to content

Remove requirement for src or srcset attribute on img element#12627

Open
johannesodland wants to merge 1 commit into
whatwg:mainfrom
johannesodland:remove-required-src-or-srcset
Open

Remove requirement for src or srcset attribute on img element#12627
johannesodland wants to merge 1 commit into
whatwg:mainfrom
johannesodland:remove-required-src-or-srcset

Conversation

@johannesodland

@johannesodland johannesodland commented Jun 27, 2026

Copy link
Copy Markdown

Solves: #11313

The current spec requires an img or an alt attribute on the img element, even though the source can be provided through source elements. This PR removes that requirement to ease authoring of valid img elements.

Browsers already handle img elements without src and srcset attributes. There's an existing web platform test that relies on this behavior: https://github.com/web-platform-tests/wpt/blob/5b9051943bd631d20f1f1681778d655cbfbeceb8/html/semantics/embedded-content/the-img-element/responsive-image-select-print.html#L19

A PR for a more specific test has been added here: web-platform-tests/wpt#60621

A separate PR will be filed to update the complete getter steps under the corresponding issue: https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete

(See WHATWG Working Mode: Changes for more details.)


/embedded-content.html ( diff )

Comment thread source

<p>At least one of the <code data-x="attr-img-src">src</code> and <code
data-x="attr-img-srcset">srcset</code> attributes must be present.</p>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an img element is not a child of a picture element, aren't the src or srcset attributes required on that img element?
Therefore, simply deleting this sentence is not appropriate.

@johannesodland johannesodland Jun 28, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to add this, but I was looking to the video and audio elements and could not find a similar requirement. If the img element is a child of a picture element, do we need to validate the source elements?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the text to depend on whether or not the parent element is a picture element:

If the img element's parent is not a picture element, then at least one of the src and srcset attributes must be present.

Do you think that is sufficient?

It is still very much possible to have markup like this:

<picture>
  <img alt="some alt text">
</picture>

If we additionally require a source element, then its still possible to write markup like this:

<picture>
  <source media="print" srcset="">
  <img alt="some alt text">
</picture>

@mtrootyy

Copy link
Copy Markdown

The following sentence should be added.

If the itemprop attribute is specified on the img element, then the src attribute must also be specified.

Ref:
In 4.8.11.2 Location of the media resource;

If the itemprop attribute is specified on the media element, then the src attribute must also be specified.

@johannesodland johannesodland force-pushed the remove-required-src-or-srcset branch from 886b3c0 to 11a5342 Compare June 28, 2026 07:47
Solves: whatwg#11313

The current spec requires an img or an alt attribute on the img element,
even though the source can be provided through source elements.
This PR removes that requirement to ease authoring of valid img elements.

Browsers already handle img elements without src and srcset attributes.
There's an existing web platform test that relies on this behavior:
https://github.com/web-platform-tests/wpt/blob/5b9051943bd631d20f1f1681778d655cbfbeceb8/html/semantics/embedded-content/the-img-element/responsive-image-select-print.html#L19

A PR for a more specific test has been added here: web-platform-tests/wpt#60621

A separate PR will be filed to update the complete getter steps under the corresponding issue:
https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
@johannesodland johannesodland force-pushed the remove-required-src-or-srcset branch from 11a5342 to d76ee3c Compare June 29, 2026 17:53
@johannesodland

Copy link
Copy Markdown
Author

The following sentence should be added.

If the itemprop attribute is specified on the img element, then the src attribute must also be specified.

I added the sentence. This requirement should probably have been added previously when removing the requirement for the src attribute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants