Advanced HTML

A presentation at DDD Perth in October 2023 in Perth WA, Australia by Mandy Michael

Slide 1

Slide 1

Advanced HTML for Good Developers

Slide 2

Slide 2

HTML IS SIMPLE & VERY FRIENDLY. BATMANDY

Slide 3

Slide 3

WE MAKE THE MISTAKE OF ASSUMING BECAUSE IT’S SIMPLE IT’S NOT VALUABLE BATMANDY

Slide 4

Slide 4

THERE ARE ~115 USABLE* HTML ELEMENTS BATMANDY

Slide 5

Slide 5

<p> <a> <button> <li> <script> <main> <link> <form> <img> <ul> <div> <section> <input> <h2> <label> <span> <h1> <article> BATMANDY

Slide 6

Slide 6

<p> <a> <button> <li> <script> <main> <link> <form> <img> <ul> <div> <section> <input> <h2> <label> <span> <h1> <article> BATMANDY

Slide 7

Slide 7

BATMANDY

Slide 8

Slide 8

BATMANDY

Slide 9

Slide 9

<html> … <body> <header> <h1>Some text</h1> </header> <p>A paragraph…</p> <ul> <li>A list item</li> <li>Another item</li> </ul> <p>More content…</p> <hr> …. BATMANDY

Slide 10

Slide 10

THE DOM TREE IS CONSTRUCTED OFF WHAT WE PROVIDE. BATMANDY

Slide 11

Slide 11

TYPE SCRIPT BATMANDY

Slide 12

Slide 12

interface dog { name: string age: number isFluffy: boolean } BATMANDY

Slide 13

Slide 13

WE DETERMINE WHAT WE EXPECT THE CONTENT TO BE BATMANDY

Slide 14

Slide 14

<html> <body> <header> <h1>Dogs: They are good</h1> </header> <main> <h2>Why are dogs good?</h2> <p>All dogs are good, they are the goodest doggies.</p> <figure> <img href=”dog.png” alt=”A white golden retriever, with his head dropping to the side and tongue dangling out of his mouth” /> <figcaption>Michaelangelo AKA “Jello”</figcaption> </figure> </main> </body> </html> BATMANDY

Slide 15

Slide 15

interface dog { name: any age: any isFluffy: any } BATMANDY

Slide 16

Slide 16

<html> <body> <div> <div>Dogs: They are good</div> </div> <div> <div>Why are dogs good?</div> <div>All dogs are good, they are the goodest doggies.</div> <div> <img href=”dog.png” alt=”A golden retriever” /> <div>Michaelangelo AKA “Jello”</div> </div> </div> </body> </html> BATMANDY

Slide 17

Slide 17

IF YOU CHOOSE A GENERIC ELEMENT YOU GET A GENERIC OUTPUT BATMANDY

Slide 18

Slide 18

JUST <DIV> BATMANDY

Slide 19

Slide 19

SEMANTICS BATMANDY

Slide 20

Slide 20

NOT EVERYONE INTERACTS THE SAME BATMANDY

Slide 21

Slide 21

ACCESSIBILITY TREE BATMANDY

Slide 22

Slide 22

BATMANDY

Slide 23

Slide 23

BATMANDY

Slide 24

Slide 24

BATMANDY

Slide 25

Slide 25

BATMANDY

Slide 26

Slide 26

BATMANDY

Slide 27

Slide 27

BATMANDY

Slide 28

Slide 28

Using HTML correctly is just UX for assistive tech & other technologies. @BATMANDY BATMANDY

Slide 29

Slide 29

SEGWAY BATMANDY

Slide 30

Slide 30

Eric Bailey PERFORMANCE & THE ACCESSIBILITY TREE Check out Eric’s presentation on Notist! noti.st/ericwbailey BATMANDY

Slide 31

Slide 31

Advanced HTML for Good Developers

Slide 32

Slide 32

GOOGLE RECOMMENDS Less than 1,500 nodes Max depth of 32 nodes No parent node with more than 60 child nodes BATMANDY

Slide 33

Slide 33

EVERYTHING ADDS UP

Slide 34

Slide 34

DON’T JUST USE DIVS BATMANDY

Slide 35

Slide 35

<p> <a> <button> <li> <script> <main> <link> <form> <img> <ul> <div> <section> <input> <h2> <label> <span> <h1> <article> BATMANDY

Slide 36

Slide 36

USE THE NAMED ELEMENT FOR WHAT YOU ARE BUILDING BATMANDY

Slide 37

Slide 37

If you have a header, use the <header> element <header>This is a header</header> BATMANDY

Slide 38

Slide 38

HTML is intentionally simple, elements are named to help you out. BATMANDY

Slide 39

Slide 39

Use heading elements in the correct order <h1> <h2> <h3> <h4> <h5> <h6> BATMANDY

Slide 40

Slide 40

BATMANDY

Slide 41

Slide 41

Good Developers HTML and it’s Bene ts H1 fi BATMANDY

Slide 42

Slide 42

<h3>Good Developers</h3> <h1>HTML and it’s Benefits</h1> <div>Good Developers</div> <h1>HTML and it’s Benefits</h1> <span>Good Developers</span> <h1>HTML and it’s Benefits</h1> BATMANDY

Slide 43

Slide 43

<h1> <span>Good Developers</span> HTML and it’s Benefits </h1> BATMANDY

Slide 44

Slide 44

<h1>Good Developers</h1> <h2>HTML and it’s Benefits</h2> BATMANDY

Slide 45

Slide 45

JUST BECAUSE IT’S IN A DESIGN DOES NOT MEAN YOU HAVE TO DO IT. BATMANDY

Slide 46

Slide 46

ALWAYS CONSIDER THE CONTEXT BATMANDY

Slide 47

Slide 47

BATMANDY

Slide 48

Slide 48

Inputs & Dates <label for=“myDate“>Date:</label> <input type=”date” id=”myDate” name=“awesome-date” value=”2023-10-07” /> BATMANDY

Slide 49

Slide 49

USE THE NAMED ELEMENT FOR WHAT YOU ARE BUILDING BATMANDY

Slide 50

Slide 50

USE THE NAMED O M O T S T F T E H E IM YOU ARE BUILDING BATMANDY

Slide 51

Slide 51

DISCOVERABLE vs KNOWN DATES BATMANDY

Slide 52

Slide 52

MAYBE YOU DON’T NEED A DATEPICKER https://adrianroselli.com/2019/07/maybe-you-dont-need-a-date-picker.html BATMANDY

Slide 53

Slide 53

MAKE THE MOST OF BUILT-IN FUNCTIONALITY BATMANDY

Slide 54

Slide 54

<div onclick=”doSomething();”> I am a button </div> BATMANDY

Slide 55

Slide 55

<div tabindex=”0” role=”button” aria-pressed=“button” onclick=”doSomething();”> I am a button </div> const ENTER = 13; const SPACE = 32; myButton.addEventListener(‘keydown’, function(event) { if (event.keyCode === ENTER || event.keyCode === SPACE) { event.preventDefault(); doSomething(event); } }); function toggleButton(button) { button.setAttribute(“aria-pressed”, button.getAttribute(“aria-pressed”) === “true” ? “false” : “true” )}; } function doSomething() { // Your actual code to do what you want )}; BATMANDY

Slide 56

Slide 56

If you need a button, use the <button> element <button>This is a button</button> BATMANDY

Slide 57

Slide 57

FEATURES

<div> <button> Clickable / Tapable Focusable Keyboard Interaction Announce via assistive tech Submit / Reset Forms Disabled attribute Reported to accessibility tree :active, :hover, :focus & :disabled states BATMANDY

Slide 58

Slide 58

A Button CSS Reset By Andy Bell button { display: inline-block; border: none; margin: 0; padding: 0; font-family: sans-serif; font-size: 1rem; line-height: 1; background: transparent; -webkit-appearance: none; } BATMANDY

Slide 59

Slide 59

<div tabindex=”0” role=”button” aria-pressed=“button” onclick=”doSomething();”> I am a button </div> const ENTER = 13; const SPACE = 32; myButton.addEventListener(‘keydown’, function(event) { if (event.keyCode === ENTER || event.keyCode === SPACE) { event.preventDefault(); doSomething(event); } }); function toggleButton(button) { button.setAttribute(“aria-pressed”, button.getAttribute(“aria-pressed”) === “true” ? “false” : “true” )}; } function doSomething() { // Your actual code to do what you want )}; BATMANDY

Slide 60

Slide 60

A Button CSS Reset By Andy Bell button { display: inline-block; border: none; margin: 0; padding: 0; font-family: sans-serif; font-size: 1rem; line-height: 1; background: transparent; -webkit-appearance: none; } BATMANDY

Slide 61

Slide 61

If you need a link, use the <a> element FEATURES

<div> <a> Navigate to a page or view Change url Browser redraw/refresh Focusable Keyboard Interaction Open in new window Reported to accessibility tree :active, :hover, :focus, :link & :visited states BATMANDY

Slide 62

Slide 62

A <a> navigates to a new url, whereas a <button> triggers or toggles something in an interface? BATMANDY

Slide 63

Slide 63

IT’S LIKE USING A PRE-INSTALLED JS LIBRARY. BATMANDY

Slide 64

Slide 64

DON’T FORGET ABOUT THE ATTRIBUTES BATMANDY

Slide 65

Slide 65

ARIA ATTRIBUTES FILL THE GAPS IN STANDARD HTML TO GIVE MORE CONTEXT TO THE ACCESSIBILITY TREE BATMANDY

Slide 66

Slide 66

aria-pressed aria-selected aria-expanded aria-grabbed aria-live aria-hidden aria-labelledby https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes BATMANDY

Slide 67

Slide 67

“ If you’re writing CSS that conveys information. Ask yourself is that information also in the accessibility tree? If not, a bit of ARIA might help. JULIE GRUNDY ” BATMANDY

Slide 68

Slide 68

accept ATTRIBUTE rel=“” accept pattern spellcheck contenteditable BATMANDY

Slide 69

Slide 69

pattern ATTRIBUTE rel=“” accept pattern <input pattern=”\d{4,4}” title=”4 digits”> spellcheck contenteditable BATMANDY

Slide 70

Slide 70

contenteditable ATTRIBUTE rel=“” accept pattern

<p contenteditable>content</p> spellcheck contenteditable BATMANDY

Slide 71

Slide 71

spellcheck ATTRIBUTE rel=“” accept pattern

<p contenteditable spellcheck=”true”> spellcheck contenteditable BATMANDY

Slide 72

Slide 72

rel ATTRIBUTE rel=“” accept pattern spellcheck contenteditable BATMANDY

Slide 73

Slide 73

LOADING & PERFORMANCE BATMANDY

Slide 74

Slide 74

PRELOADING OFF SCREEN IMAGES G N DI AK A O L Y Z A L A BATMANDY

Slide 75

Slide 75

BATMANDY

Slide 76

Slide 76

Native lazy-loading for the web <img src=”img.png” loading=”lazy” /> <picture> <source media=”(min-width: 800px)” … /> <img src=“img.png” loading=“lazy” /> </picture> <iframe src=“img.png” loading=“lazy” />

  • BATMANDY

Slide 77

Slide 77

Only lazy load images that are not in viewport BATMANDY

Slide 78

Slide 78

LARGEST CONTENTFUL PAINT (LCP) : the render time of the largest image or text block visible within the viewport when the page rst starts loading. fi BATMANDY

Slide 79

Slide 79

Only lazy load images that are not in viewport BATMANDY

Slide 80

Slide 80

Lazy load distance threshold is based on the type of resource and the connection type BATMANDY

Slide 81

Slide 81

Optimising loading <img src=”img.png” loading=”lazy” width=”200” height=“200” fetchpriority=“high” /> BATMANDY

Slide 82

Slide 82

Fetch Priority high: You want the browser to prioritize it low: You want the browser to deprioritize it auto: You want the browser to decide (Default) BATMANDY

Slide 83

Slide 83

fetchpriority sets the relative priority BATMANDY

Slide 84

Slide 84

Fetch Priority <ul class=“carousel”> <li><img src=”img/1.jpg” <li><img src=”img/2.jpg” <li><img src=”img/3.jpg” <li><img src=”img/4.jpg” </ul> fetchpriority=”high”></li> fetchpriority=”low”></li> fetchpriority=”low”></li> fetchpriority=”low”></li> BATMANDY

Slide 85

Slide 85

Fetch Priority <ul class=“my-unecessary-carousel“> <li><img src=”img/1.jpg” fetchpriority=”high”></li> <li><img src=”img/2.jpg” fetchpriority=”low” loading=”lazy”></li> <li><img src=”img/3.jpg” fetchpriority=”low” loading=”lazy”></li> <li><img src=”img/4.jpg” fetchpriority=”low” loading=”lazy”></li> </ul> BATMANDY

Slide 86

Slide 86

Fetch Priority Assign fetchpriority to <iframe> elements Used in JavaScript fetch to prioritize API calls Prioritise resource tags like <script> & <link> BATMANDY

Slide 87

Slide 87

Be more explicit with Resource Hints BATMANDY

Slide 88

Slide 88

RESOURCE HINTS

<link rel=”preconnect” href=“https://gooddogs.com/img”> <link rel=”dns-prefetch” href=“https://gooddogs.com/img”> BATMANDY

Slide 89

Slide 89

RESOURCE HINTS

<link rel=”preconnect” href=”….”> Starts the connection process as soon as possible BATMANDY

Slide 90

Slide 90

RESOURCE HINTS

<link rel=”preconnect” href=”….”> Starts the connection process as soon as possible Only for critical connections BATMANDY

Slide 91

Slide 91

RESOURCE HINTS

<link rel=”dns-prefetch” href=”….”> Starts the DNS lookup https://caniuse.com/?search=dns-prefetch BATMANDY

Slide 92

Slide 92

EVERYTHING ADDS UP

Slide 93

Slide 93

DON’T JUST USE DIVS USE THE NAMED ELEMENTS LEVERAGE BUILT-IN FUNCTIONALITY EXPLORE THE ATTRIBUTES CONVEY INFORMATION & CONTEXT BATMANDY

Slide 94

Slide 94

MAKE IT USEFUL MAKE IT USABLE BATMANDY

Slide 95

Slide 95

HTML IS NOT JUST THE FOUNDATION WE BUILD ON. IT’S VITAL IN MAKING OUR WEBSITES ACCESSIBLE . USABLE & PERFORMANT BATMANDY

Slide 96

Slide 96

HTML IS A HACK FOR MAKING GOOD WEBSITES & APPS BATMANDY

Slide 97

Slide 97

THANKS BURNT TOAST CREATIVE @burnttoastcre8v BATMANDY

Slide 98

Slide 98

THANKS & RESOURCES Julie Grundy - @stringyland.bsky.social Eric Bailey - @social.ericwbailey.website/@eric ‣developer.mozilla.org/docs/Web/HTML ‣https://web.dev/iframe-lazy-loading/ ‣noti.st/ericwbailey/Yfyaxa/the-intersection-of-performance-and-accessibility ‣developers.google.com/web/tools/lighthouse/audits/dom-size ‣medium.com/@mandy.michael/understanding-why-semantic-html-is-important-as-told-by-typescriptbd71ad41e6c4 ‣https://web.dev/fetch-priority/ ‣https://www.smashingmagazine.com/2022/04/boost-resource-loading-new-priority-hint-fetchpriority/ ‣https://adrianroselli.com/2019/07/maybe-you-dont-need-a-date-picker.html ‣https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/frame/ settings.json5;l=963-995 ‣https://web.dev/lcp/ ‣https://addyosmani.com/blog/lazy-loading/ ‣https://www.debugbear.com/blog/resource-hints-rel-preload-prefetch-preconnect BATMANDY