惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
S
Secure Thoughts
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
WordPress大学
WordPress大学
SecWiki News
SecWiki News
B
Blog
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
Webroot Blog
Webroot Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
LINUX DO - 热门话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
F
Fortinet All Blogs
W
WeLiveSecurity
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
S
Schneier on Security
爱范儿
爱范儿
V
V2EX - 技术
L
LINUX DO - 最新话题
月光博客
月光博客
博客园 - 【当耐特】
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
U
Unit 42
Schneier on Security
Schneier on Security
E
Exploit-DB.com RSS Feed
J
Java Code Geeks
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
博客园 - 叶小钗
T
Troy Hunt's Blog
大猫的无限游戏
大猫的无限游戏
AI
AI
Security Latest
Security Latest

Matthias Ott

Hello Again, World This, Still Not for Everyone The Shape of Friction WeissKlang L1 – Punching Above Its Weight Continvoucly Morged Value Webspace Invaders To Affinity and Beyond The Mystery of Storytelling Amateurs! Echoes of Connection Linear() Is Not (That) Linear View Transitions: The Smooth Parts Adding AVIF and WebP Support to My Craft CMS Site Challenge Acoustic Room Treatment and Building Sound Panels, Part 1: Planning Play On Overshoot The HTML Output Element Listening Closely Compressed Fluid Typography The Lifeblood of the Web What Could Go Wrong? That’s My Rank Making Space CSS :is() :where() the Magic Happens Visual Regression Testing for External URLs With Playwright Jane Goodall’s Famous Last Words European Tech Alternatives 🇪🇺 Independent Type Foundry Advent Calendar – Day 24: NaN Independent Type Foundry Advent Calendar – Day 23: Typotheque Independent Type Foundry Advent Calendar – Day 22: 205TF Independent Type Foundry Advent Calendar – Day 21: HvD Fonts Independent Type Foundry Advent Calendar – Day 20: Frere-Jones Type Independent Type Foundry Advent Calendar – Day 19: Fontwerk Independent Type Foundry Advent Calendar – Day 18: Vectro Independent Type Foundry Advent Calendar – Day 17: Studio René Bieder Independent Type Foundry Advent Calendar – Day 16: R-Typography Independent Type Foundry Advent Calendar – Day 15: David Jonathan Ross Independent Type Foundry Advent Calendar – Day 14: Interval Type Independent Type Foundry Advent Calendar – Day 13: Newglyph Independent Type Foundry Advent Calendar – Day 12: Swiss Typefaces Independent Type Foundry Advent Calendar – Day 11: Sharp Type Independent Type Foundry Advent Calendar – Day 10: Colophon Foundry Independent Type Foundry Advent Calendar – Day 9: Commercial Type Independent Type Foundry Advent Calendar – Day 8: Letters from Sweden Independent Type Foundry Advent Calendar – Day 7: Lineto Independent Type Foundry Advent Calendar – Day 6: Ohno Type Company Independent Type Foundry Advent Calendar – Day 5: Milieu Grotesque Independent Type Foundry Advent Calendar – Day 4: TypeMates Independent Type Foundry Advent Calendar – Day 3: Klim Type Foundry Independent Type Foundry Advent Calendar – Day 2: Dinamo Independent Type Foundry Advent Calendar – Day 1: Grilli Type The Independent Type Foundry Advent Calendar 2022 A Conversation With ChatGPT ChatGPT, please explain websites in the words of William Shakespeare Transient Frameworks Leaving Twitter Behind Converting Your Twitter Archive to Markdown The Wrong Question It Wasn’t Written Syndicating Posts from Your Personal Website to Twitter and Mastodon Suspension None of Your Business Doing Our Part Patch That Package Brain Dump Generating Accessibility Test Results for a Whole Website With Evaluatory The CSS Cascade, a Deep Dive Updates About Updates How to Delete Your Commit History in Git Unblocking Your Writing Blocks, Part 2: I’m Not an Expert nor a “Thought Leader” Connections No Wrong Notes Better Options Design Debt Finite and Infinite Games Don’t Assume, Validate. Necessity Is the Ultimate Teacher One Egg Go Deep There Is No Secret Code Balancing Risk Blue Eyes, Brown Eyes The Shortcut Boomerang My RSS Feed Collection of Personal Websites Frequency The Illusion of Control The Decisions Journey Write It Down Nownownow Into the Personal-Website-Verse Considering the Opposite What is it for? Unlimited Bowling. Never done. We Are Team Internet. We Need to Save #NetNeutrality. Progressive Search Data loss (also) by JavaScript Books I Will Definitely Maybe Read in 2017 Starting to Write Notes
Debugging HTML With Advanced CSS Selectors
Matthias Ott · 2020-06-23 · via Matthias Ott

Writing HTML is hard. At least writing semantically sound, valid HTML is. This might come as a surprise to those who only scratch the surface of what HTML really can do. What can be so hard about a few elements, right? At least it isn’t an object-oriented, multi-paradigm programming language. But as soon as you start to dig deeper you realize that there is a lot to get wrong in HTML.

For one, HTML now contains hundreds of elements. This alone can make it hard to find the right element for the job. But also if you know all the vocabulary, there are many more things to consider. For example, which elements are allowed to be nested within other elements or which attributes a certain element can have. Not to forget accessibility: If we use semantically correct elements, we make sure that our content will be understood by assistive technologies like screenreaders and that our interfaces are usable for everyone. Using a native button element instead of a div when you need a button will not only impress your friends but also add standard styles and accessibility features like focus and additional keyboard events.

So there are a lot of reasons why you should write semantic, valid, accessible HTML. But how do you make sure that your code is error-free? If you are working on a larger team or with a team of content managers, it can be a challenge to maintain a high standard, especially if different team members have a different experience level with HTML.

You can, and this would definitely be the best solution, set up a thorough testing environment where each HTML or accessibility error is flagged immediately. Truth be told, we are far from testing being a priority – or even a sphere of knowledge – for many developers and clients. Also, when you are in an earlier phase of a project, and you are, for example, still prototyping in HTML or building basic HTML templates, you might not yet have an automated testing solution in place.

So how can you still make sure that your HTML is valid? Testing your HTML manually with the W3C Markup Validation Service would be another option. But this can be tedious, too. Wouldn’t it be nice to be able to see HTML errors directly in the browser? This is where CSS comes in. By the very nature of CSS, you are able to filter for elements in the most remote corners of the DOM. And, as it turns out, you can also use it to, at least visually, validate your code by highlighting elements that contain errors.

Adam Argyle posted an example of how this can look like on Twitter the other day:

/* CSS */
:is(ul, ol) > *:not(li) {
  outline: 2px dotted red;
}

In this case, the selector matches all elements that are inside of lists but not a list item li and displays them with a red outline. An outline makes sense because, other than using border, it does not change the size of the box of the element. Adam uses the new :is() pseudo-class, which still has limited browser support. But you could easily write it without :is():

/* CSS */
ul > *:not(li), ol > *:not(li) {
    outline: 2px dotted red;
}

This technique can be used for many more useful things, for example, detecting images without an alt attribute:

/* CSS */
img:not([alt]) { ... }

As a general rule, images should always have an alt attribute to be accessible. When the attribute is set, a screen reader will read the alternative text. And when it is empty, the screen reader will ignore the image. If there is no alt attribute at all, though, the screen reader will read the src attribute instead. Not very useful in most cases.

Ire Aderinokun has written a great post about many more use cases for listing HTML with CSS, like checking for empty interactive elements, unlabelled form elements, or faulty or missing link targets:

/* CSS */
a:not([href]),
a[href="#"],
a[href=""],
a[href*="javascript:void(0)"] { … }

She even created a Chrome extension that combines many of those selectors into a style sheet that you can apply to any web page to check the HTML for accessibility issues.

And there are many more interesting things you could do. For example, checking for the previously mentioned div buttons from hell:

/* CSS */
div[role="button"] { 
  text-decoration: blink; 
  /* Don't actually use blink here, it's a joke. */
}

Or, if you don’t shy away from using gigantonormous selectors, you could check if a span only contains the allowed “phrasing content”, or if some div lover put a lovely div in your span:

/* CSS */
span > :not(abbr):not(audio):not(b):not(bdo):not(br):not(button):not(canvas):not(cite):not(code):not(command):not(data):not(datalist):not(dfn):not(em):not(embed):not(i):not(iframe):not(img):not(input):not(kbd):not(keygen):not(label):not(mark):not(math):not(meter):not(noscript):not(object):not(output):not(picture):not(progress):not(q):not(ruby):not(samp):not(script):not(select):not(small):not(span):not(strong):not(sub):not(sup):not(svg):not(textarea):not(time):not(var):not(video):not(wbr) {
  outline: 3px dashed red;
}

Using CSS to debug all of your code covering all possible ways HTML in which elements could be misused would certainly be overkill. But depending on your project, this technique could still be quite useful to work against the most common and most severe mistakes like missing links or alt attributes. One could even think of having a default debug.css file that contains a standard set of the most useful rules. I think I’ll try that in one of the next projects.

-

This is the 24th post of my 100 days of writing series. You can find a list of all posts here.

~

10 Webmentions

5 Likes

ⓘ Webmentions are a way to notify other websites when you link to them, and to receive notifications when others link to you. Learn more about Webmentions.