Or; how to use plain HTML and CSS to impress your friends and vanquish your enemies!
Eddie Antonio Santos
Thanks, Amelia!Web standards nerds!
— Amelia Bellamy-Royds (@AmeliasBrain) April 21, 2021
What are some examples of web content / user interface patterns that you've seen implemented with JavaScript, when they could have been done more simply & accessibly with HTML + CSS?
Asking for an @_EddieAntonio, who's working on a talk.
We will be creating this app:
with zero JavaScript
Basic markup:
How to submit without JavaScript?
Use <form>
and <button>
elements!
Will do a GET
request to /search?q=user+input
<button type="submit">
is all that's need to submit a form on click on enter!
Do not submit if empty
<form class="search-bar" action="/search">
<input name="search" type="search" required>
<button type="submit">Search!</button>
</form>
add the required
attribute to the input!
Check MDN for a list of validation!
Always sanitize+validate on the backend!
Basic markup
<form class="search-bar" action="/search">
<input id="search" name="search" type="search" required>
<button id="button" type="submit">Search!</button>
</form>
$("#search").on("mouseenter", () => {
$("#button").css("background-color", "blue")
})
$("#search").on("mouseleave", () => {
$("#button").css("background-color", "white")
})
(there are many ways to do this)
CSS pseudo-classes to the rescue!
(examples over simplified for demonstration)
<form class="search-bar" action="/search">
<input name="search" type="search" required>
<button type="submit">Search!</button>
</form>
.search-bar:focus-within button[type=submit] {
background-color: grey;
}
.search-bar button:hover,
.search-bar button:active,
.search-bar button:focus {
background-color: blue;
}
Combine with sibling selectors to style invalid inputs!
(examples over simplified for demonstration)
<form class="search-bar" action="/search">
<input name="search" type="search" required>
<button type="submit">Search!</button>
</form>
input[type=search]:invalid + button[type=submit] {
cursor: not-allowed;
}
input[type=search]:invalid + button[type=submit]:hover,
input[type=search]:invalid + button[type=submit]:focus {
background-color: red;
}
Basic markup
Advanced Options
…
$("#open-advanced-options").click((event) => {
event.preventDefault();
$("#advanced-options").show();
})
(there are many ways to do this)
Builtin HTML tags to do this!
<details>
and
<summary>
Advanced options
(advanced options go here…)
<details>
has open
attribute that you can use in CSS and
JavaScript!
details > .content {
opacity: 0;
/* fade in when open: */
transition: opacity .25s;
}
details[open] > .content {
opacity: 1;
}
Source code available on my GitHub!
with zero JavaScript
registering a click
event handler
for <a href="#">
<form action="/search">
<button type="submit">
Search!
</button>
</form>
registering a mouseenter
, mouseleave
, etc. event handlers
use CSS pseudo-classes
:hover
:focus
:focus-within
Using JavaScript to show/hide content