Skip to content

Warn if there is more than one main HTML element, or if the main is missing #35354

Open

Description

@carolinan

What problem does this address?

A few blocks have an option to change the HTML element to <main>, but there must only be one main element on a page.
Having more than one <main> element (that is not hidden) is invalid HTML and an accessibility issue.

What is your proposed solution?

Add a warning if there is more than one main element -perhaps in the document outline "table of content" details popover?
It already warns for incorrect heading levels.

Activity

  1. added
    [Type] Enhancement
    [Feature] Document Outline
    on Oct 5, 2021
  2. aristath commented on Oct 6, 2021

    @aristath
    Member

    Note from the MDN Docs:

    A document mustn't have more than one <main> element that doesn't have the hidden attribute specified.

    It is possible to have more than one main, but only one that's not hidden. Right now I don't believe we add hidden anywhere, but any check we add should take the hidden scenario into account. 👍

  3. carolinan commented on Oct 25, 2021

    @carolinan
    Author
  4. added
    [Focus] Accessibility (a11y)
    on May 19, 2022
  5. changed the title [-]Warn if there is more than one main HTML element[/-] [+]Warn if there is more than one main HTML element, or if the main is missing[/+] on Jul 19, 2024
  6. bph commented on Aug 2, 2024

    @bph
    Contributor

    The accessibility features "Skip to content" + <main/> section could use the attention similar to the color low contrast warning, when selecting text and background colors.

    A user can so easily remove a <main/> section tag by just removing a root group block. This came up in two discussions in the #outreach channel as well,

    Apart from the warning, and update on documentation would also be beneficial to add the HTML Element selector to other blocks, like post content, media+text when used in a template context.

    @WordPress/outreach

  7. added
    Needs Design
    Needs Accessibility Feedback
    on Aug 2, 2024
  8. carolinan commented on Aug 3, 2024

    @carolinan
    ContributorAuthor

    Some thoughts:

    • I think there are performance issues with looping through all blocks on the current page, find blocks that use the tagName attribute, and then check if that tagName is main. The check for the heading level is much more limited because it only checks the heading block.
    • The check should be limited to block themes, because the editor does not know what other tags there are in classic theme templates. (*To do, check if the heading outline is available on classic themes and if it is accurate).
    • The check may need to be limited to the Site Editor, unless it can check both template and block editor contents.
    • A new help text needs to be written, explaining why the main is needed.
    • A link to more information should be added, similar to the link added to the alt text help.
  9. luminuu commented on Aug 5, 2024

    @luminuu
    Member

    I've recently worked on a solution where I've implemented this via a block theme itself and it's working fine. Here's how it looks:
    CleanShot 2024-08-05 at 16 01 40@2x

    It's only available in the site editor and on templates (not template parts, patterns and pages). It recursively checks the content with wp.data.select( 'core/editor' ).getEditorBlocks(). The message changes if there's no main tag found or if there's more than one main tag found and it also displays how many tags were found, however not where specifically.

  10. getdave commented on Dec 2, 2024

    @getdave
    Contributor

    Related to #65400. Soon Zoom Out will not be available if <main> does not exist. Therefore it would be great if there could be a warning about:

    • more than one <main>
    • lack of <main>
  11. getdave commented on Dec 2, 2024

    @getdave
    Contributor

    @luminuu Looks great, would you be open to raising a PR with your code? 🙏

  12. carolinan commented on Dec 2, 2024

    @carolinan
    ContributorAuthor

    I like that the message shows as a notice this way and not as a warning in the Document Outline panel where it would be less visible.

  13. juanfra commented on Dec 2, 2024

    @juanfra
    Member

    I like that the message shows as a notice this way and not as a warning in the Document Outline panel where it would be less visible.

    +1

    This will be especially helpful for theme developers working on implementing zoom-out functionality. While building TT5 and working with templates that include nested groups, we encountered some challenges.

  14. luminuu commented on Dec 5, 2024

    @luminuu
    Member

    @luminuu Looks great, would you be open to raising a PR with your code? 🙏

    @getdave The code is specific to a theme, I assume it has to be rewritten in order to land in core? The code can be found here: https://github.com/Greyd-Suite/greyd-wp/blob/main/assets/js/editor-main-tag-warning.js

  15. self-assigned this
    on Jan 10, 2025
  16. carolinan commented on Jan 14, 2025

    @carolinan
    ContributorAuthor

    I have made some progress on this, but I am wondering how to best explain to the user how to act on the warning:

    1. How to add a main element
    2. How to find duplicate main elements

    Perhaps the notice needs an external link to a support page?

    I think the warning would be easier to act on, if it was clearer in the list view which groups already have the <main> tag:
    #42610

  17. added
    [Status] In Progress
    on Jan 14, 2025
  18. luminuu commented on Jan 24, 2025

    @luminuu
    Member

    We have since improved the warning in the Greyd Theme, the update still has to be rolled out to the Greyd WP theme but I agree, it's confusing when you just have the message without knowing where to look for the main tag.

    Thank you for working on this @carolinan!

  19. Mamaduka commented on Jul 16, 2025

    @Mamaduka
    Member

    Recently, I (re)discovered a proposal from #41747 to allow displaying errors/warnings in the list. Now I think it might be a more suitable place for displaying similar warnings.

  20. carolinan commented on Jul 16, 2025

    @carolinan
    ContributorAuthor

    The problem is that still going to be that if you have more than one block that can use the main element, where in list view do you show the message that there is no main element in the document?
    We can't show it when any or multiple of those elements are selected or expanded. As mentioned in the PR that will just confuse the user about where to add it, and they may add it randomly to any of these elements which would be an accessibility problem.

    We can't tie a document level warning to a single block.

  21. Mamaduka commented on Jul 16, 2025

    @Mamaduka
    Member

    As far as I understand, #41747 proposes a dropdown where errors and warnings can be displayed. So it will be no different from displaying a warning in a Document Overview.

    In any case, this won't be a blocker for #68661, I just remembered an old issue and wanted to connect some dots.

    I'll try to move the current proposal forward. I should have time for it this week.

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

Metadata

Metadata

Assignees

Labels

Needs Accessibility FeedbackNeeds Design[Feature] Document Outline[Focus] Accessibility (a11y)[Status] In Progress[Type] Enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions