• Resolved mohamedaminesamet

    (@mohamedaminesamet)


    hello, I was wondering how can i implement < main > html tag, in the correct way as for the html structure. because < main > tag is important.according to html docs I have read read, it’s better to be like :
    <header>
    -<nav>
    <main>
    -<main section>
    --<section 1>
    ---<article>
    ----<header>
    ----<div>
    --<section 2>
    ---<section 2-1>
    ----<article>
    -<aside>
    <footer>

    But such action is not available to do in elementor! because elementor is not allowing too much section nesting ! and another issue is that you must only have 1 < main > on your entire page.

    I tried integrating this JS code:

    document.addEventListener('DOMContentLoaded', function() {
    const elements = document.querySelectorAll('div[data-elementor-id]');
    elements.forEach(element => {
    const mainElement = document.createElement('main');

    // Copier le contenu
    mainElement.innerHTML = element.innerHTML;

    // Copier les styles en ligne
    mainElement.style.cssText = element.style.cssText;

    // Copier les classes si nécessaire
    mainElement.className = element.className;

    // Remplacer la div par main
    element.parentNode.replaceChild(mainElement, element);
    });
    });

    Also I tried to add this code to functions.php file :

    function replace_first_elementor_div_with_main($content) {
    $header_pos = strpos($content, '<header');
    $footer_pos = strpos($content, '<footer');
    if (is_singular() && !is_admin()) {
    $pattern = '/<div([^>]+)>/i';
    if (preg_match($pattern, $content, $matches, PREG_OFFSET_CAPTURE, $header_pos)) {
    $replacement = '<main' . $matches[1][0] . '>';
    $content .= '</main>';
    $content = preg_replace($pattern, $replacement, $content, 1);
    }
    }
    return $content;
    }
    add_filter('the_content', 'replace_first_elementor_div_with_main');

    These two codes were added to replace the <div> tag that contains all of the page’s content with the <main> tag; however, this changes how Elementor functions and does not work with all versions of Elementor.

    So what should I do? how can I achieve this? 

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Support Rica

    (@ricav)

    Hi @mohamedaminesamet,

    Thank you for the detailed explanation and for sharing your approach.

    You’re absolutely right that the <main> tag plays an important role in accessibility and proper HTML structure. However, Elementor’s layout system is built on nested <div>s, which can limit direct control over semantic tags like <main> in the visual editor.

    That said, if you’re looking to ensure there’s a single, correctly placed <main> element wrapping your primary content, we recommend doing this at the theme level rather than through JavaScript or complex functions.php modifications.

    I suggest you join the Elementor Facebook Community​ where you can find a supportive and helpful community of experienced users who will be happy to provide valuable tips and tricks. You might also find useful resources in our developer documentation here: https://developers.elementor.com/.

    If you have more advanced, developer-specific questions, feel free to connect directly with our developer community on Discord: https://elemn.to/discord

    As this thread has been inactive for over two months, we’ll go ahead and close it now. Thank you for your understanding and cooperation.

    Kind regards,

Viewing 1 replies (of 1 total)

The topic ‘elementor main html tag – html structure – seo’ is closed to new replies.