<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ anchor css - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ anchor css - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 17 Jul 2026 22:27:24 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/anchor-css/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Create Accessible Modals and Pop-ups Using HTML, CSS, and Minimal JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Creating pop-ups and modals on your site can be a complicated process. And it often requires a lot of boilerplate code to get started. But the real challenge comes when you want to make that modal or  ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-accessible-modals-and-pop-ups-using-html-css-and-minimal-javascript/</link>
                <guid isPermaLink="false">6a5a91c20842ed39785bc7c0</guid>
                
                    <category>
                        <![CDATA[ anchor css ]]>
                    </category>
                
                    <category>
                        <![CDATA[ popup ]]>
                    </category>
                
                    <category>
                        <![CDATA[ modal ]]>
                    </category>
                
                    <category>
                        <![CDATA[ html modal ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Accessibility ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Dialog ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ jabo Landry ]]>
                </dc:creator>
                <pubDate>Fri, 17 Jul 2026 20:34:10 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/e929b316-4b85-459b-9c40-1b7928518077.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Creating pop-ups and modals on your site can be a complicated process. And it often requires a lot of boilerplate code to get started.</p>
<p>But the real challenge comes when you want to make that modal or pop-up accessible.</p>
<p>Today, I'll show you how you can use <code>&lt;dialog&gt;</code> to create an accessible modal/pop-up with minimal setup using HTML, CSS and JavaScript.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before diving in, it helps if you’re comfortable with a few basics:</p>
<ul>
<li><p><strong>CSS fundamentals</strong>: You should be familiar with common CSS terminology and concepts (selectors, properties, positioning, and so on).</p>
</li>
<li><p><strong>HTML structure</strong>: A working knowledge of how elements are organized in the DOM will make the examples easier to follow.</p>
</li>
<li><p><strong>JavaScript DOM basics</strong>: While this guide uses only minimal JavaScript, understanding how to query and manipulate DOM elements will give you more confidence as you experiment.</p>
</li>
</ul>
<p>That’s all you need. No frameworks, no heavy boilerplate. Just a foundation in the core web technologies.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-how-to-create-pop-ups-with-popover">How to Create Pop-ups with <code>popover</code></a></p>
</li>
<li><p><a href="#heading-how-to-create-a-modal-using-the-dialog-tag">How to Create a Modal Using the <code>dialog</code> Tag</a></p>
</li>
<li><p><a href="#heading-gotchas-to-pay-attention-to">Gotchas to Pay Attention to</a></p>
</li>
<li><p><a href="#heading-the-backdrop-pseudo-class">The <code>::backdrop pseudo</code> class</a></p>
</li>
<li><p><a href="#heading-final-thoughts">Final Thoughts</a></p>
</li>
</ul>
<h2 id="heading-how-to-create-pop-ups-with-popover">How to Create Pop-ups with <code>popover</code></h2>
<p>Creating a pop-up from scratch using HTML, CSS and JavaScript can be quite challenging. A pop-up is a small dialog box that appears on the screen to show extra information or ask for input. Because it’s temporary by design, users expect it to disappear once they’re done interacting with it, like when they press Escape or click outside of it.</p>
<p>Using the HTML <code>popover</code> and <code>popovertarget</code> attributes, you can have several built-in accessibility and interaction behaviors, such as Escape-to-close and light dismiss, but you still need to choose the right semantic element and test keyboard and screen reader behavior.</p>
<h3 id="heading-setting-up-the-pop-up-with-html">Setting Up the Pop-up with HTML</h3>
<p>To be practical, let's create a common example and use case for pop-ups on a webpage. We'll create a nav element that displays when a user clicks a hamburger menu or the menu list.</p>
<p>First, you'll set the <code>popover</code> attribute on the pop-up container. This tells HTML to treat the containing block as a pop-up and hide it from the screen by default.</p>
<p>Then you set the <code>popovertarget</code> attribute on the element that will trigger the pop-up (like a button element or something else) to unhide the hidden element with an attribute of <code>popover</code>.</p>
<h4 id="heading-example">Example:</h4>
<pre><code class="language-html">&lt;button popovertarget="navbar-menu" id='nav-btn'&gt;open&lt;/button&gt;

&lt;nav id="navbar-menu" popover&gt;
  &lt;a href="#"&gt;Home&lt;/a&gt;
  &lt;a href="#"&gt;About&lt;/a&gt;
  &lt;a href="#"&gt;Address&lt;/a&gt;
&lt;/nav&gt;
</code></pre>
<p>With the above setup, you have a pop-up with useful built-in interaction behaviors, including Escape-to-close and light dismiss. You can hide it from the screen by pressing the <code>ESC</code> key on the keyboard or when you click anywhere else on the page (as long as it's not inside the pop-up section).</p>
<p>Remember that the <code>popover</code> attribute alone doesn't automatically make a pop-up accessible. You still need to use the appropriate semantic elements, provide accessible labels where needed, and test keyboard and screen reader behavior.</p>
<h3 id="heading-how-to-align-the-pop-up">How to Align the Pop-up</h3>
<p>Now you'll want to align the pop-up and place it where you want it to be. By default, the pop-up (or modal) that's created using either the <code>popover</code> attribute or the dialog tag will be centered on the page.</p>
<p>This is because by default elements with <code>popover</code> have a position of <code>fixed</code> and the <code>inset</code> of 0, which centers the pop-up box and a margin of <code>auto</code>.</p>
<p><strong>Note:</strong> <code>inset</code> is the shorthand for top, bottom, left and right of an element's position on the page. If you want to have the same size on all of sides of an element, use inset.</p>
<p>If you don't want your pop-up in the center, you can start by setting the element with <code>popover</code> position to absolute to isolate it from the page's flow:</p>
<pre><code class="language-css">#navbar-menu {
  position: absolute;
}
</code></pre>
<p>You can then disable the margin to 0 and positions (inset) to <code>unset</code>:</p>
<pre><code class="language-css">#navbar-menu {
  position: absolute;
  margin: 0;
  inset: unset;
}
</code></pre>
<p>After this, you can then place the popover element on the side of the page you want.</p>
<h3 id="heading-how-to-use-the-position-anchor-property">How to Use the <code>position-anchor</code> Property</h3>
<p><strong>Note:</strong> CSS Anchor Positioning is a newer feature. Check browser support before relying on it in production and provide a fallback for browsers that don't support it yet.</p>
<p>The next step is to position the element close to the element that triggers it. For it we can use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/position-anchor"><code>position-anchor</code> property of CSS</a>.</p>
<p>The <code>position-anchor</code> property in CSS specifies a default anchor element that an absolutely or fixed-positioned element will "tether" or snap to. It lets you link a floating target (like our pop-up here) to another element on the page using only CSS.</p>
<p>In our example, we have a menu list icon or a hamburger menu that will open and close the nav element as a pop-up. We want the nav bar menu to be attached to/near the menu list that opens it.</p>
<p>So, you can add the <code>anchor-name</code> property to a menu icon. The name must be prefixed with double-dashes (and the name can be anything you want).</p>
<pre><code class="language-css">#nav-btn {
  anchor-name: --nav;
}
</code></pre>
<p>The <code>position-anchor</code> property lets you attach an element to another element identified by an anchor name. Once anchored, you can use the <code>anchor()</code> function to position the element relative to that anchor, like aligning it to the anchor’s top, bottom, or center.</p>
<pre><code class="language-css">#navbar-menu {
  position: absolute;
  margin: 0;
  inset: auto;
  position-anchor: --nav;
  top: anchor(bottom);
  right: anchor(right);
}
</code></pre>
<p>Pass the anchor name you give your anchor as value of <code>position-anchor</code> to align the nav element (<code>#navbar-menu</code> in our example) on the page around the <code>anchor-name</code> of <code>nav</code> which is <code>#nav-btn</code> in our example.</p>
<p>Then the <code>anchor()</code>positions the top of nav element on the bottom side and right side to the menu's right side.</p>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/ogBBNNa" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>

<h2 id="heading-how-to-create-a-modal-using-the-dialog-tag">How to Create a Modal Using the <code>dialog</code> Tag</h2>
<p>Using the <code>command</code> and <code>commandfor</code> attributes on a button, you can declaratively control a <code>&lt;dialog&gt;</code>. For example, <code>command="show-modal"</code> opens it as a modal, while <code>command="close"</code> closes it.</p>
<p>Pop-up modals and pop-ups are different: with a pop-up, you can still interact with the page when the pop-up is active. But with modals or pop-up modals you can't interact with the page – modals lock the screen until they're ignored or confirmed.</p>
<p>When a <code>&lt;dialog&gt;</code> is opened as a modal, it's placed in the browser's top layer so that it appears above the rest of the page content. The rest of the document becomes inert, meaning users can't interact with it while the modal is open. The browser also creates a <code>::backdrop</code> behind the modal, which you can style to provide a visual overlay.</p>
<p>Here's an example:</p>
<pre><code class="language-html">&lt;button command="show-modal" commandfor="contact-dialog"&gt;
    open modal
&lt;/button&gt;

 &lt;dialog id="contact-dialog"&gt;
    &lt;button command="close" commandfor="contact-dialog" aria-label="close modal"&gt;
      close modal
    &lt;/button&gt;
    &lt;!--modal contents goes here--&gt;
&lt;/dialog&gt;
</code></pre>
<p>You can use the <code>command</code> attribute to close and show the modal and the <code>commandfor</code> attribute to reference the modal that's being targeted using the modal's id.</p>
<p>The <code>command</code> attribute can receive one of the following options:</p>
<ul>
<li><p><code>show-modal</code>: This option is used on the element that will trigger the modal or the dialog box to open it.</p>
</li>
<li><p><code>close</code>: This option is passed to an element and will close the modal when it's open.</p>
</li>
</ul>
<p>In the code snippet example, you can see that the button with label <code>open modal</code> has a <code>command</code> attribute with the option to <strong>show-modal</strong> and the <code>commandfor</code> attribute that's targeting the dialog element by its id.</p>
<p>The close modal button is using the <strong>close</strong> option on the <code>command</code> attribute to close the modal when clicked. It also uses <code>commandfor</code> to indicate which modal should be closed when the close button is clicked.</p>
<p>Below you'll find a demo of a modal that's created using the <code>command</code> and <code>commandfor</code> attributes:</p>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/NPdRqWK?editors=1100" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>

<p>Try clicking the "click to pop a modal!" button on the Code Pen above to see the modal you can create by just using the <code>command</code> and <code>commandfor</code> attributes on a <code>dialog</code> tag.</p>
<h3 id="heading-create-modal-using-showmodal">Create modal using <code>showModal()</code></h3>
<p>The command API may not be supported in older browsers. Alternatively you can use JavaScript's <code>showModal()</code> method, as it's widely available in many browsers compared to <code>command</code> and <code>commandfor</code>.</p>
<pre><code class="language-javascript">const btn = document.querySelector("button");
const dialog = document.querySelector("dialog");

btn.addEventListener("click", () =&gt; {
  dialog.showModal();
});
</code></pre>
<h3 id="heading-how-to-open-a-non-modal-dialog-with-show">How to Open a Non-Modal Dialog with <code>show()</code></h3>
<p>Sometimes you may want to have an element like modal that stays interactive and doesn't block the other pages content like the <code>command</code> and <code>commandfor</code> attributes do. For this, you can use <code>show()</code> on dialog using JavaScript.</p>
<pre><code class="language-javascript">const btn = document.querySelector("button");
const dialog = document.querySelector("dialog");

btn.addEventListener("click", () =&gt; {
  dialog.show();
});
</code></pre>
<p>The above snippet keeps the rest of the page interactive while the dialog is open. This differs from opening a dialog with <code>showModal()</code> or using <code>command="show-modal"</code>, which opens the dialog as a modal and makes the rest of the document inert.</p>
<p><strong>Note</strong>: Keep in mind that <code>show()</code> isn't considered a modal but more of a dialog-like element that doesn't block interaction with the rest of the page.</p>
<h2 id="heading-gotchas-to-pay-attention-to">Gotchas to Pay Attention to</h2>
<p>When you're using element <code>popover</code>, the <code>command</code> and <code>commandfor</code> attributes, or the <code>show()</code> method, there are some "gotchas" to watch out for. Paying attention to these will help you stick to best practices.</p>
<h3 id="heading-dont-use-flex-or-grid">Don't Use Flex or Grid</h3>
<p>Directly applying layout-related styles like Flex or Grid is highly discouraged on both elements with <code>popover</code> and modal elements.</p>
<p>By default, elements with <code>popover</code> attribute, <code>command</code> and <code>commandfor</code> button attributes, and <code>show()</code> have a display of <code>none</code>. This basically means the pop-up or modal is hidden from the screen.</p>
<p>When you add <code>flex</code> or <code>grid</code> directly on element with the <code>popover</code> attribute or on a modal element, you're rewriting the modal or <code>popover</code> element's default behavior and they will be always visible on the screen. This means that you won't be able to hide the modal from the screen.</p>
<p>Check out the below example in the CodePen demo:</p>
<pre><code class="language-css">#navbar-menu {
  display: grid;
/* other styles definition*/
}
</code></pre>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/wBgrJEv" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>

<p>You can see from the example that the nav element is always visible even if we click on the menu button to hide it again.</p>
<h4 id="heading-suggested-approach">Suggested Approach</h4>
<p>In this situation, you can either use the <code>popover-open</code> pseudo-class on an element on a property that has <code>popover</code> attributes, or you can use <code>dialog[open]</code> on the dialog element.</p>
<p>The pseudo-class styles the element based on its state when interacting with the page. So, in this case we want to give the pop-up or a modal a different layout when it's in the open state.</p>
<p>Example:</p>
<pre><code class="language-css">/* element with a popover*/
#navbar-menu:popover-open {
  display: grid;
  gap: 2rem;
}

/* using a dialog element*/
dialog[open] {
  display: grid;
  gap: 2rem;
}
</code></pre>
<h3 id="heading-background-scrolling">Background Scrolling</h3>
<p>Another thing to consider when using the <code>&lt;dialog&gt;</code> element is background scrolling. Depending on the browser and platform, the underlying page may still be scrollable while a modal dialog is open. If you want to prevent this behavior, you can explicitly disable scrolling while the dialog is open.</p>
<p>Take a look at this CodePen example:</p>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/NPdRqWK?editors=1100" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>

<p>When you click on the button to show the modal, you can still see the scrollbar on the page, and you can scroll around the page.</p>
<h4 id="heading-suggested-approach">Suggested Approach</h4>
<p>To deal with this issue we can use the <code>has</code> pseudo class. <code>has</code> helps select the parent element based on its children's state. On the root element or HTML element, you can check if it has an open modal. If so, you can set the root element or HTML element overflow to hidden.</p>
<p>For example:</p>
<pre><code class="language-css">html:has(dialog[open]) {
  overflow: hidden;
}
</code></pre>
<p>This will hide the scrollbar on the page when the modal is open. Keep in mind that you can also use any parent element to wrap the <code>dialog</code> tag with the <code>has</code> pseudo class. It doesn't always have to be the root element or HTML element.</p>
<h2 id="heading-the-backdrop-pseudo-class">The <code>::backdrop</code> Pseudo Class</h2>
<p>If you want to use a customized overlay color on the <code>popover</code> element or modal element, you can use the <code>::backdrop</code> pseudo class to customize the appearance for the modal overlay color.</p>
<p><strong>Example:</strong></p>
<pre><code class="language-css">dialog::backdrop {
  background: rgba(43, 50, 200, 0.4);
}
</code></pre>
<p>This will apply the overlay with defined <code>RGB</code> colors and the <code>opacity</code> of 0.4 on the overlay to have a little transparent on the overlay.</p>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>I hope you've gained something new from this article, and that it will help you start using these techniques in your projects.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
