<?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[ jabo Landry - 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[ jabo Landry - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 18 Jul 2026 19:36:44 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/Arnold-Jabo/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>
        
            <item>
                <title>
                    <![CDATA[ How to Build Responsive Designs and Scroll Effects with CSS Container Queries ]]>
                </title>
                <description>
                    <![CDATA[ Container queries let you target specific sections of your webpage and apply styles to create customizable, responsive designs based on the container's size rather than that of the viewport. This guid ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-responsive-designs-and-scroll-effects-with-css-container-queries/</link>
                <guid isPermaLink="false">6a1f667b3090fee41f05a0d3</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Container queries ]]>
                    </category>
                
                    <category>
                        <![CDATA[ scroll ]]>
                    </category>
                
                    <category>
                        <![CDATA[ media queries ]]>
                    </category>
                
                    <category>
                        <![CDATA[ scroll animation ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ jabo Landry ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2026 23:25:47 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/dc70f649-2837-4768-be69-e4c1e85dcb03.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Container queries let you target specific sections of your webpage and apply styles to create customizable, responsive designs based on the container's size rather than that of the viewport.</p>
<p>This guide will teach you this useful alternative for responsive designs. You'll also learn about on-scroll effects in CSS.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before diving into scrollable queries and container-based scroll effects, make sure you have:</p>
<ul>
<li><p><strong>Basic familiarity with CSS features and key terms</strong> — understanding properties like <code>height</code>, <code>overflow</code>, and how they affect layout.</p>
</li>
<li><p><strong>Experience writing vanilla CSS</strong> — being comfortable with selectors, rulesets, and applying styles without relying on frameworks.</p>
</li>
<li><p><strong>A working knowledge of HTML structure</strong> — since scroll behavior depends on parent elements, knowing how containers and child elements interact is essential.</p>
</li>
<li><p><em>(Optional but helpful)</em> Some exposure to responsive design concepts, so you can see how scrollable parents fit into broader layout strategies.</p>
</li>
</ul>
<h2 id="heading-what-well-cover">What We'll Cover:</h2>
<ul>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-how-to-use-container-queries">How to Use Container Queries</a></p>
</li>
<li><p><a href="#heading-difference-between-container-queries-and-media-queries">Difference Between Container Queries and Media queries</a></p>
</li>
<li><p><a href="#heading-container-scroll-state">Container Scroll-state</a></p>
</li>
<li><p><a href="#heading-wrapping-up">Wrapping Up</a></p>
</li>
</ul>
<h2 id="heading-how-to-use-container-queries">How to Use Container Queries</h2>
<p>Usually, when making responsive designs, you use a media query that adds style to the page based on the page's width viewport, but container queries shift the narrative by adding styles based on the specific elements container or parent element size.</p>
<p>Take a navbar as an example. Let's say you want to have its links wrapped on top of each other when the size of a <code>nav</code> element size is less than <code>450px</code> and also change the background color when the screen size is less than <code>450px</code>.</p>
<p>To make <code>nav</code> element links wrap on top of each other when the <code>nav</code> element size is less than <code>450px</code>, we start by wrapping the links inside a single parent <code>nav</code>:</p>
<pre><code class="language-html">   &lt;nav&gt;
      &lt;img src="" alt="logo" /&gt;
      &lt;ul&gt;
        &lt;a href=""&gt;home&lt;/a&gt;
        &lt;a href=""&gt;about&lt;/a&gt;
        &lt;a href=""&gt;services&lt;/a&gt;
        &lt;a href=""&gt;contact&lt;/a&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
</code></pre>
<p>Then use <code>container-type</code> to define a container query on the wrapping element or parent (<code>nav</code>) element. It can have one of the following values:</p>
<ul>
<li><p><code>inline-size</code>: Used when you want to track the x-axis of the parent element on the page (width)</p>
</li>
<li><p><code>block-size</code>: Used when you want to track the y-axis of the parent element on the page (height)</p>
</li>
<li><p>Container Scroll-state: Used when you want to track the parent element when scrolled and when elements with position <code>sticky</code> are stuck on the screen.</p>
</li>
</ul>
<p>For most cases, you will need to track the size of an element against the x-axis size, and you must use the <code>container-type</code> on a parent element because you want to apply styles based on the parent element size. Here, we'll use the <code>nav</code> because it is the parent element.</p>
<pre><code class="language-css">nav { 
  container-type: inline-size;
}
</code></pre>
<p>You then define a container query with an at symbol (<code>@</code>) followed by the <code>container</code> keyword.</p>
<p>Then check if the <code>width</code> of the parent container (<code>nav</code>) gets narrower than <code>450px</code>. If the condition is true, add a <code>flex-wrap</code> to links element:</p>
<pre><code class="language-css">@container (width &gt; 450px) {
  ul {
    flex-wrap: wrap;
  }
}
</code></pre>
<h2 id="heading-difference-between-container-queries-and-media-queries">Difference Between Container Queries and Media queries</h2>
<p>Take a look at the code below for media queries:</p>
<pre><code class="language-css">
@media (width &lt; 450px) {
  nav {
    background: green;
  }
}
</code></pre>
<p>In the media query above, the <code>nav</code> element has a <code>green</code> background-color when the screen size is less than <code>450px</code>.</p>
<p>For container queries, the elements are getting stacked on top of each other when the <code>nav</code> element size is less than <code>450px</code>.</p>
<p>So, the difference is that media-queries considers <strong>the whole screen size</strong> while container-queries consider the <strong>parent element size</strong>.</p>
<p><strong>Example:</strong></p>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/dPprjqM" 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-container-scroll-state">Container Scroll-state</h2>
<p>You can also use container queries to style an element on scroll or to style an element with a position sticky when it's stuck on the page.</p>
<p><strong>Note</strong>: Container <strong>scroll-state</strong> browser support isn't very good. It's supported in about 71% of all browsers currently, with the notable exceptions of Safari and Firefox. Be cautious when using it in production.</p>
<h3 id="heading-scrollable-queries">Scrollable Queries</h3>
<p>A scrollable query is used to check for parent element scrollbar behavior. If there is a scroll behavior on the parent element, then the container query for scrolling will be applied.</p>
<p>To use the scrollable queries, you need to have a parent that has a scroll behavior. HTML is the best fit for such an example because by default HTML has a scroll behavior on it.</p>
<p>To make an element scrollable, set a fixed height and apply <code>overflow</code>. This ensures a scroll bar appears whenever content exceeds that defined space.</p>
<h4 id="heading-example">Example:</h4>
<p>We’ll design a simple page that displays a sidebar table of contents when the page is scrolled down and hides it when the user returns to the top.</p>
<p>First, make HTML a <code>container-type</code> of <code>scroll-state</code>:</p>
<pre><code class="language-css">html {
  container-type: scroll-state;
}
</code></pre>
<p>Style the table of contents section and position it at the top right of the page.</p>
<pre><code class="language-css">.toc {
  position: fixed;
  top: 5rem;
  left:90%;
  align-self: start;
  opacity: 0;
}
</code></pre>
<p>You positioned the table of contents to the right of the page and fixed it to the top of the screen by <code>5rem</code>. You also hid it by default by adding an <code>opacity</code> of <code>0</code>.</p>
<p>Now you need to check if the container scrollbar can be scrolled to the top of its container. If true, unhide the table of contents:</p>
<pre><code class="language-css">@container scroll-state(scrollable: top) {
  .toc {
    transition: opacity 0.4s linear;
    opacity: 1;  
  }
}
</code></pre>
<p>You defined a container query, then followed it with the scroll-state keyword. This checks whether the scrollbar on a parent element can be scrolled.</p>
<p>Use <code>scrollable</code> to define the scroll direction. Setting it to <code>top</code> tests whether the container can scroll upward, while setting it to <code>bottom</code> tests whether it can scroll downward.</p>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/GgjewRp" 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 that as long there's a space for scrolling to the top, the table-of- contents will be visible, but when you can't scroll to the top, the table of contents will be hidden.</p>
<p>You can also check whether you can scroll in both top and bottom directions using the <code>y</code> specifier to specify the container's top and bottom scroll directions.</p>
<h4 id="heading-example">Example:</h4>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/ogYgvJb?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>You can see from the example that the table of contents will always be visible on the screen because the scroll bar can scroll both ways (top and bottom)</p>
<h3 id="heading-stuck-queries">Stuck Queries</h3>
<p>Stuck queries are used to inspect the element that have a <code>position</code> of <code>sticky</code> and applies styles when the element is stuck on the page.</p>
<p>These are great if you want to style elements that have a sticky position and you want them to have a certain style(s) when they get stuck on the page.</p>
<h4 id="heading-example">Example:</h4>
<p>We'll design a simple navbar that will change its <code>box-shadow</code> when its stuck at the top of the screen.</p>
<p>Here, you'll set a wrapper element that wraps <code>nav</code> and makes it a container query, and also gives it a <code>position</code> of <code>sticky</code> to be able to track if it is stuck at the top of the screen.</p>
<pre><code class="language-css">header {
  position: sticky;
  top: 0rem;
  container-type: scroll-state;
}
</code></pre>
<p>You can then define <code>@container</code> to execute when the element gets stuck on the screen:</p>
<pre><code class="language-css">@container scroll-state(stuck:top) {
  nav {
    box-shadow:
      0 14px 28px rgba(0, 0, 0, 0.55),
      0 1px 0 rgba(255, 255, 255, 0.06) inset;
    background-color: #0a0a0a;
  }
}
</code></pre>
<p>You checked for <code>scroll-state</code> behavior and then, inside the parenthesis, checked if the element with <code>position: sticky</code> is <strong>stuck</strong> at top of the page. If true, the background color of the <code>nav</code> changes and a <code>box-shadow</code> is added.</p>
<p>Final results should look like this:</p>
<div class="embed-wrapper"><iframe width="100%" height="350" src="https://codepen.io/jabo-arnold/embed/gbwEQZR" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>

<p>By default, the <code>nav</code> doesn't have a <code>box-shadow</code>, but when you scroll and the <code>nav</code> element gets stuck at the top of the page, <code>box-shadow</code> and other rules defined within the stuck <code>@container</code> query definition will be applied to the <code>nav</code> element.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this article, you learned how to set up and work with container queries to make more customizable and responsive designs, and some cool scroll effects.</p>
<p>The examples provided in this guide are not the only things you can do with container queries. The examples were designed to help you think of other alternatives and contexts where you can apply container queries to match your exact needs and design.</p>
<p>Container queries have high browser support, which is currently at 95% and supported in all major browsers.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
