{"id":719,"date":"2024-04-05T02:12:11","date_gmt":"2024-04-05T02:12:11","guid":{"rendered":"https:\/\/www.cssportal.com\/blog\/?p=719"},"modified":"2024-04-06T00:06:38","modified_gmt":"2024-04-06T00:06:38","slug":"inert-global-attribute-what-does-it-do","status":"publish","type":"post","link":"https:\/\/www.cssportal.com\/blog\/inert-global-attribute-what-does-it-do\/","title":{"rendered":"Inert Global Attribute &#8211; What Does It Do?"},"content":{"rendered":"<p>Ever wondered how webpages manage to make certain elements interactive while keeping others purely presentational? The magic lies in HTML attributes, special instructions woven into the code that tell a web browser how to handle specific elements.<\/p>\n<p>This post looks into a particularly useful attribute called <code>inert<\/code>. Unlike its name might suggest, <code>inert<\/code> isn&#39;t about making elements invisible. Instead, it focuses on making them inactive, essentially removing their ability to respond to user interactions. <\/p>\n<p>Let&#39;s explore what the <code>inert<\/code> attribute does and how it can be used to create more accessible and user-friendly web experiences.<\/p>\n<h3>What Does <code>inert<\/code> Do?<\/h3>\n<p>Imagine a webpage with a fancy modal window that pops up when you click a button. While the modal is open, you wouldn&#39;t want to accidentally click something behind it, right? That&#39;s where the <code>inert<\/code> attribute comes in. When applied to an element, <code>inert<\/code> essentially throws a virtual &quot;Do Not Disturb&quot; sign on it, making the browser ignore various user interactions. Here&#39;s a breakdown of its effects:<\/p>\n<ul>\n<li><strong>Stops Events in Their Tracks:<\/strong> Clicking, focusing, hovering &#8211; all the usual ways users interact with elements become a no-go zone. When an element is marked <code>inert<\/code>, the browser doesn&#39;t fire off any events associated with these interactions. <\/li>\n<li><strong>Inaccessible to Assistive Technologies:<\/strong> Screen readers, used by people with visual impairments, rely on a structured accessibility tree to navigate webpages. Elements with the <code>inert<\/code> attribute are excluded from this tree, ensuring screen readers don&#39;t announce them as interactive elements.  <\/li>\n<li><strong>Off the Tab Order Highway:<\/strong> When you use the Tab key to navigate a webpage, you&#39;re essentially moving through a defined order of focusable elements. <code>inert<\/code> elements are removed from this tab order, preventing users from accidentally tabbing into them and getting stuck.<\/li>\n<\/ul>\n<p>It&#39;s important to distinguish <code>inert<\/code> from its cousin, the <code>hidden<\/code> attribute. While both can make elements appear visually inactive, <code>hidden<\/code> elements can still be programmatically accessed and might be announced by screen readers. <code>inert<\/code>, on the other hand, provides a more complete disengagement from user interaction.<\/p>\n<h3>Use Cases for the <code>inert<\/code> Attribute<\/h3>\n<p>The <code>inert<\/code> attribute shines in situations where you want to control user interaction and enhance accessibility. Here are some compelling use cases:<\/p>\n<ul>\n<li><strong>Accessibility Champion:<\/strong>\n<ul>\n<li><strong>Non-Interactive Elements:<\/strong> Imagine a form that waits for user input before certain options become available. Marking those inactive options with <code>inert<\/code> prevents users from accidentally triggering errors and keeps screen readers focused on relevant elements.<\/li>\n<li><strong>Custom UI with Flair:<\/strong> Building custom UI components like modals, tooltips, or dropdown menus often involves elements that shouldn&#39;t be directly interacted with. <code>inert<\/code> helps create accessible versions of these components by ensuring screen readers skip over non-essential parts.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Content and Presentation United:<\/strong>\n<ul>\n<li><strong>Decoration, Not Interaction:<\/strong> Sometimes webpages use decorative elements for visual appeal. Marking these elements as <code>inert<\/code> ensures users don&#39;t get confused by trying to interact with something that&#39;s purely presentational. It also helps keep the focus on the actual interactive content.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>By strategically applying the <code>inert<\/code> attribute, you can create web experiences that are not only visually engaging but also inclusive and user-friendly for everyone. <\/p>\n<h3>How to Use the <code>inert<\/code> Attribute<\/h3>\n<p>Using the <code>inert<\/code> attribute is delightfully straightforward! Here&#39;s how to get started:<\/p>\n<ul>\n<li><strong>Basic Syntax:<\/strong> It&#39;s as simple as adding the <code>inert<\/code> attribute directly to the opening tag of the element you want to make inactive. Here&#39;s an example:<\/li>\n<\/ul>\n<div class=\"headerblog\">HTML<\/div>\n<pre><code class=\"language-html\">&lt;div inert&gt;This element is inactive!&lt;\/div&gt;\r\n<\/code><\/pre>\n<p>Now, any clicks, focus attempts, or other user interactions on this <code>&lt;div&gt;<\/code> will be ignored by the browser.<\/p>\n<ul>\n<li><strong>Dynamic <code>inert<\/code> with JavaScript (Optional):<\/strong> While the basic syntax works well for static elements, you might want to control the <code>inert<\/code> state dynamically based on user actions. This can be achieved using JavaScript:<\/li>\n<\/ul>\n<div class=\"headerblog\">HTML<\/div>\n<pre><code class=\"language-html\">&lt;div id=&quot;mainContent&quot;&gt;\r\n    &lt;button id=&quot;showModal&quot;&gt;Open Modal&lt;\/button&gt;\r\n    &lt;!-- Main page content --&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div id=&quot;modal&quot; class=&quot;modal&quot; inert&gt;\r\n    &lt;p&gt;This is a modal dialog. You cannot interact with the main content now.&lt;\/p&gt;\r\n    &lt;button id=&quot;closeModal&quot;&gt;Close&lt;\/button&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div id=&quot;overlay&quot; class=&quot;overlay&quot; hidden&gt;&lt;\/div&gt;<\/code><\/pre>\n<div class=\"headerblog\">CSS<\/div>\n<pre><code class=\"language-css\">.modal {\r\n    position: fixed;\r\n    top: 50%;\r\n    left: 50%;\r\n    transform: translate(-50%, -50%);\r\n    border: 1px solid #000;\r\n    background-color: #fff;\r\n    padding: 20px;\r\n    z-index: 2;\r\n}\r\n.overlay {\r\n    position: fixed;\r\n    top: 0;\r\n    left: 0;\r\n    width: 100%;\r\n    height: 100%;\r\n    background: rgba(0,0,0,0.5);\r\n    z-index: 1;\r\n}\r\n.modal, .overlay {\r\n    display: none;\r\n}<\/code><\/pre>\n<div class=\"headerblog\">JavaScript<\/div>\n<pre><code class=\"language-javascript\">const showModal = document.getElementById(&#x27;showModal&#x27;);\r\nconst closeModal = document.getElementById(&#x27;closeModal&#x27;);\r\nconst modal = document.getElementById(&#x27;modal&#x27;);\r\nconst overlay = document.getElementById(&#x27;overlay&#x27;);\r\nconst mainContent = document.getElementById(&#x27;mainContent&#x27;);\r\n\r\nshowModal.addEventListener(&#x27;click&#x27;, () =&#x3E; {\r\n    modal.style.display = &#x27;block&#x27;; \/\/ Show the modal\r\n    overlay.style.display = &#x27;block&#x27;; \/\/ Show the overlay\r\n    modal.removeAttribute(&#x27;inert&#x27;);\r\n    mainContent.setAttribute(&#x27;inert&#x27;, &#x27;&#x27;);\r\n});\r\n\r\ncloseModal.addEventListener(&#x27;click&#x27;, () =&#x3E; {\r\n    modal.style.display = &#x27;none&#x27;; \/\/ Hide the modal\r\n    overlay.style.display = &#x27;none&#x27;; \/\/ Hide the overlay\r\n    modal.setAttribute(&#x27;inert&#x27;, &#x27;&#x27;);\r\n    mainContent.removeAttribute(&#x27;inert&#x27;);\r\n});<\/code><\/pre>\n<ul>\n<li>When the user clicks the &quot;Open Modal&quot; button, the modal dialog becomes active (not inert), and the main content of the page (<code>mainContent<\/code>) is set to be inert. This means the user cannot interact with the main content while the modal is open.<\/li>\n<li>The modal itself is initially set to be inert to prevent interaction until it&#39;s explicitly opened.<\/li>\n<li>When the &quot;Close&quot; button in the modal is clicked, the modal is set back to inert, and the main content&#39;s <code>inert<\/code> attribute is removed, allowing interaction with the main content again.<\/li>\n<\/ul>\n<p><a class=\"btn btn-primary\" rel=\"nofollow noopener\" target=\"_blank\" href=\"\/blog-examples\/inert.php\">Click to view Example<\/a><\/p>\n<p>Remember, <code>inert<\/code> is a relatively new attribute, so it&#39;s always a good practice to check browser compatibility for your target audience.<\/p>\n<div class=\"text-center mb-20 mt-20\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<!-- CSS Responsive -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-1356719463849900\"\n     data-ad-slot=\"5275015068\"\n     data-ad-format=\"horizontal\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/div>\n<div class=\"adblock-message bg-main-50 rounded p-10 mb-20\">  \n\t<div class=\"d-flex align-items-center justify-content-between\">\n\t  <span class=\"text-gray-400\">If this site has been useful, we\u2019d love your support! Consider buying us a coffee to keep things going strong!<\/span>\n\t  <div class=\"buymeacoffee\">\n\t\t<a target=\"_blank\" href=\"https:\/\/buymeacoffee.com\/rpjs\"><img decoding=\"async\" width=\"180\" src=\"\/assets\/images\/coffee.png\"><\/a>\n\t  <\/div>\n\t<\/div>\n<\/div>\n<h3>Benefits of Using the <code>inert<\/code> Attribute<\/h3>\n<p>Integrating the <code>inert<\/code> attribute into your web development workflow offers a range of benefits for both users and developers:<\/p>\n<ul>\n<li><strong>Accessibility for All:<\/strong> By keeping users with assistive technologies in mind, <code>inert<\/code> ensures a smoother experience. Screen readers avoid announcing non-interactive elements, and the focus stays on relevant content. This creates a more inclusive web environment.<\/li>\n<li><strong>Enhanced User Experience:<\/strong>  Accidental clicks on non-interactive elements can be frustrating. <code>inert<\/code> prevents these mishaps, leading to a more intuitive and user-friendly experience. Users can navigate the page without getting sidetracked by elements they shouldn&#39;t interact with.<\/li>\n<li><strong>Cleaner Code, Clearer Focus:<\/strong> Separating content from presentation becomes easier with <code>inert<\/code>. Developers can mark decorative elements as inactive, keeping the HTML code cleaner and ensuring focus remains on the core functionality of the webpage. This leads to more maintainable and well-structured code.<\/li>\n<\/ul>\n<h3>Considerations and Best Practices for Using <code>inert<\/code><\/h3>\n<p>While the <code>inert<\/code> attribute offers a powerful tool for managing user interaction, there are a few things to keep in mind:<\/p>\n<ul>\n<li><strong>Browser Compatibility:<\/strong> As mentioned earlier, <code>inert<\/code> is a relatively new attribute. While major browsers offer good support, it&#39;s always a good idea to check compatibility for your target audience. You might need to consider alternative approaches for older browsers.<\/li>\n<li><strong><code>inert<\/code> for Sections, Not Controls:<\/strong> The <code>inert<\/code> attribute is best suited for marking entire sections of content as inactive. For individual controls like buttons or form elements, using the <code>disabled<\/code> attribute is a more appropriate choice. <code>disabled<\/code> not only makes the element inactive but also provides visual cues to users about its state.<\/li>\n<li><strong>Complex Scenarios, Consider Alternatives:<\/strong>  For highly complex scenarios with intricate interactions, <code>inert<\/code> might not always be the best fit. Consider exploring other accessibility techniques like <code>aria-hidden<\/code> or <code>aria-disabled<\/code> in conjunction with appropriate focus management strategies.<\/li>\n<\/ul>\n<p>Here are some best practices to keep in mind:<\/p>\n<ul>\n<li><strong>Test Thoroughly:<\/strong>  Always test your webpages with assistive technologies and different browsers to ensure the <code>inert<\/code> attribute functions as intended and doesn&#39;t introduce unintended accessibility issues.<\/li>\n<li><strong>Progressive Enhancement:<\/strong>  If browser compatibility is a concern, consider using progressive enhancement techniques. This involves providing a basic experience for all users and then layering on the benefits of <code>inert<\/code> for browsers that support it.<\/li>\n<li><strong>Stay Updated:<\/strong>  The web development landscape is constantly evolving. Keep yourself updated on the latest accessibility best practices and browser support for the <code>inert<\/code> attribute to ensure your webpages remain accessible and user-friendly.<\/li>\n<\/ul>\n<p>By following these considerations and best practices, you can leverage the <code>inert<\/code> attribute effectively to create a more accessible and user-centric web experience.<\/p>\n<h2 id=\"inert-vs-aria-hidden\"><code>inert<\/code> vs. <code>aria-hidden<\/code><\/h2>\n<p>You might be wondering how <code>inert<\/code> stacks up against another common accessibility attribute, <code>aria-hidden<\/code>. Here&#39;s a quick breakdown:<\/p>\n<ul>\n<li>\n<p><strong>Focus:<\/strong> <code>inert<\/code> completely removes the element from the tab order and prevents it from receiving focus. <code>aria-hidden<\/code> on its own doesn&#39;t affect focus, but it can be used in conjunction with <code>tabIndex=&quot;-1&quot;<\/code> to achieve a similar effect.<\/p>\n<\/li>\n<li>\n<p><strong>Accessibility Tree:<\/strong> <code>inert<\/code> excludes the element and its children from the accessibility tree, ensuring screen readers don&#39;t announce them. <code>aria-hidden=&quot;true&quot;<\/code> hides the element from screen readers as well, but it can still be included in the accessibility tree if necessary.<\/p>\n<\/li>\n<li>\n<p><strong>Interaction:<\/strong> Both attributes prevent user interaction like clicks or hovers. However, <code>inert<\/code> provides a more robust solution, as it completely disregards the element&#39;s existence from the browser&#39;s perspective.<\/p>\n<\/li>\n<\/ul>\n<p><strong>Choosing the Right Tool:<\/strong><\/p>\n<ul>\n<li>Use <code>inert<\/code> when you want to completely remove an element from the accessibility tree and user interaction. This is ideal for non-interactive decorative elements or content that becomes active later based on user input.<\/li>\n<li>Use <code>aria-hidden<\/code> when you want to hide content visually from screen readers but still keep it part of the accessibility tree. This could be useful for temporary content that disappears after a user action.<\/li>\n<\/ul>\n<p>Remember, the best approach depends on your specific needs. Always consider the context and user experience when choosing between <code>inert<\/code> and <code>aria-hidden<\/code>.<\/p>\n<h3>Conclusion<\/h3>\n<p>The <code>inert<\/code> attribute may seem like a small piece of the HTML puzzle, but it packs a powerful punch. By strategically applying <code>inert<\/code>, you can create webpages that are not only visually appealing but also truly inclusive for everyone.<\/p>\n<p>Remember, a well-crafted webpage should not just look good, it should be easy to navigate and interact with for all users, regardless of their abilities. By embracing the power of <code>inert<\/code> and following accessibility best practices, you can contribute to a more user-friendly and equitable web for everyone.<\/p>\n<p>Check out our complete list of <a href=\"\/html-attributes\/\">HTML Attributes<\/a> and view browser compatibility and an example for the <a href=\"\/html-global-attributes\/inert.php\">Inert Attribute<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wondered how webpages manage to make certain elements interactive while keeping others purely presentational? The magic lies in HTML attributes, special instructions woven into the code that tell a web browser how to handle specific elements. This post looks into a particularly useful attribute called inert. Unlike its name might suggest, inert isn&#39;t about [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":720,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html","wpautop"],"_links":{"self":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/comments?post=719"}],"version-history":[{"count":3,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"predecessor-version":[{"id":723,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/719\/revisions\/723"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media\/720"}],"wp:attachment":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}