Tagged: til

  • Inline rendering with document.currentScript

    Today I learned about document.currentScript thanks to Lean Rada.

    Using document.currentScript with the replaceWith() method, the footer can update itself and fix one of the most common problems on the web: a stale copyright year.

    <footer>
    	©
    	<noscript>2026</noscript>
    	<script>
    		document.currentScript.replaceWith(new Date().getFullYear());
    	</script>
    </footer>

    As mentioned on MDN, document.currentScript returns the <script> element whose script is currently being processed and isn’t a JavaScript module.

    So when the above script runs, the document.currentScript returns the script element. However, a script can only change the page when it explicitly touches the DOM, so we use the replaceWith() method. It swaps the returned script element for the year.

    Before I learned about document.currentScript, I would have used a templating language and set up a cron job to rebuild the site every 1st Jan. Or I would have used the textContent property, which needs an id or a selector to find the element. document.currentScript is much more convenient since it already knows where it is.

  • Note - Posted on

    Today I learned that modal <dialog>s opened with showModal() can have their own <h1>.

    I discovered this while reading Modern Web Guidance’s HTML guide. I was skeptical at first because it’s bad practice to have multiple <h1>s on a single web page.

    But this exception for modal dialogs made sense when I learned what showModal() does. It makes the rest of the page inert and as mentioned on MDN, inert HTML elements and their flat tree descendants are hidden from assistive technologies as they are excluded from the accessibility tree. So while the modal dialog is open there is no other <h1> to compete with.

    I’m not sure if there is consensus on this because I have seen many examples where an <h2> was used instead.

  • Note - Posted on

    Today I learned, through Sara Soueidan’s course, Practical Accessibility, that spectacles/eyeglasses are a form of assistive technology.

    I’ve worn glasses for most of my life, but I’ve never really thought of myself as someone who uses assistive technology.

    It made me realize how easily we can think of assistive technology as something that exists for “people with disabilities,” rather than something many of us may rely on ourselves.

  • </> htmx ~ Vendoring

    Today I learned, that copying and pasting other people’s code in your project is called vendoring. Carson Gross goes on to explain how vendoring can get pretty difficult to deal with and how that is actually its strength:

    Because dealing with large numbers of dependencies is difficult, vendoring encourages a culture of independence.

    You get more of what you make easy, and if you make dependencies easy, you get more of them.

  • Note - Posted on

    Today I learned via Paweł Grzybek’s post on Mastodon, where Temani Afif pointed out that text decorations propagate to their descendants, they aren’t inherited.

    The difference between propagation and inheritance is that the descendants don’t get their own text-decoration property. So, the line, color, style and thickness are all determined by the ancestor where text-decoration was declared.

    A descendant can still declare its own text-decoration, but that creates an additional decoration on top, it doesn’t modify or remove the ancestor’s.

  • Note - Posted on

    Today I learned, adding tabindex="-1" to skip link targets is likely no longer necessary in modern browsers.

    Manuel Matuzović tested skip links without tabindex="-1" on targets across major browsers and screen readers on macOS, Windows and Android, concluding it is safe to remove in most setups, thanks to sequential focus navigation starting point.

    Furthermore, adding tabindex="-1" to skip link targets also introduces a regression in usability. With tabindex="-1" on main, clicking on any non-interactive area inside main makes main itself the focus starting point. So, the next tab press sends focus to the first focusable element in main, not the next one after where the user clicked. The gov.uk team removed tabindex from main for this reason.

  • Inherited properties leak through the donut scope

    Today I learned about a potential pitfall with @scope.

    Via MDN [emphasis mine]:

    It is important to understand that, while @scope allows you to isolate the application of selectors to specific DOM subtrees, it does not completely isolate the applied styles to within those subtrees. This is most noticeable with inheritance — properties that are inherited by children (for example color or font-family) will still be inherited, beyond any set scope limit.

    In other words, @scope isolates your selectors, not your styles. Here’s what that looks like in practice.

    <article class="card">
    	<p>In scope</p>
    	<div class="content">
    		<p>Outside the scope limit</p>
    	</div>
    </article>
    @scope (.card) to (.content) {
    	:scope {
    		color: red;
    		font-family: Georgia, serif;
    	}
    
    	p {
    		padding: 1lh;
    		outline: 1px solid;
    	}
    }

    The second <p>, the one inside .content, won’t get the padding or outline because those are non-inherited properties in the scope limit. That part works exactly as you’d expect.

    But it will still turn red and render in Georgia because color and font-family are inherited properties set on .card (via :scope). And as we just learned from MDN, inheritance flows through the DOM regardless of where @scope sets the scope limit.

    As Miriam mentioned in the Winging It episode on CSS Scope & Mixins, it has to work this way because if @scope blocked inheritance at the scope limit, every element beyond the scope limit would receive the initial value for each inherited property.

    initial applies the value as defined in the CSS spec. And that would be far more destructive than letting inheritance flow through.

    If you’re coming from JavaScript, you might expect custom properties to work like scoped variables work in JavaScript. But in CSS, custom properties are inherited properties just like color or font-family.

    @scope (.card) to (.content) {
    	:scope {
    		--text-color: red;
    		font-family: Georgia, serif;
    	}
    
    	p {
    		color: var(--text-color);
    		padding: 1lh;
    		outline: 1px solid;
    	}
    }
    
    /* This rule exists to show that the inherited value is available beyond the scope limit */
    .content p {
    	color: var(--text-color);
    }

    With the same HTML, the second <p> again loses the padding and outline. But --text-color inherits down from .card into .content p. So the second <p> also turns red because its color property references var(--text-color) which resolves to red.

    For custom properties, if inheritance were blocked at the scope limit and no ancestor outside the scope had also defined it, any var(--text-color) beyond the scope limit would be undefined and that would just trigger the fallback in the var() function, or if there’s no fallback, the property using it would behave as unset.

    There’s more to what happens when a custom property is undefined, involving the guaranteed-invalid value and invalid at computed-value time (IACVT). I wrote a note on what happens when the CSS function var() references an undefined custom property.

  • Note - Posted on

    Today I learned that setting type="reset" on a <button> element in HTML creates a reset button that, when activated, immediately clears all form data, resetting it to its initial state.

    This could have been useful in the context of a search form. But <input type="search"> already provides a native clear button for that single field.

  • Note - Posted on

    Today I learned that on macOS, with the default scrollbar setting enabled, classic scrollbars are shown automatically when a mouse is connected. I used to believe that, mouse or not, users needed to explicitly change the scrollbar appearance to get the classic scrollbars.

    This is relevant for everyone who builds websites because when classic scrollbars are shown, the value of 100vw includes the scrollbar width. This can cause an unexpected horizontal overflow if the layout relies on 100vw for full-width elements. Additionally, they affect media queries, which assume scrollbars don’t exist when evaluating viewport width.

    Having said that, these aren’t new issues. Classic scrollbars are shown by default on Windows, where the same behaviours apply.

    Further reading:

  • Note - Posted on

    Today I learned, :root (0-1-0) has a higher specificity than html (0-0-1).

    In hindsight, it’s obvious. :root is a CSS pseudo-class selector and, like most pseudo-class selectors, it has the same specificity as a class selector or an attribute selector.

  • Note - Posted on

    Today I learned, grid-auto-columns and grid-auto-rows size implicit tracks as well as any explicit tracks that are not explicitly sized by by grid-template-rows or grid-template-columns.

    Until now, I was under the impression that grid-auto-rows and grid-auto-columns size only implicit grid tracks.

  • Note - Posted on

    Thanks to Kevin Powell, today I learned that the text-underline-offset property is named so because it only applies to underlines and not other text-decoration-line values like overline and line-through.

    <a href="https://example.com">Example</a>
    a {
    	text-decoration-line: underline overline; /* We can set multiple line-decoration properties at once */
    	text-underline-offset: 16px; /* Only impacts underline */
    }