Customized built-in elements
Customized built-in elements extend a real button or table row with the is attribute. See what they keep, why Safari refuses them, and what to use instead.
- 01Extend a built-in elementWrite the class, the
extendsoption, and theisattribute, and create one from HTML or JavaScript. - 02Know what they're good forKeep a real element's focus, keyboard, forms, and role, and fit into tables, selects, and lists.
- 03Pick a path that works everywhereKnow why Safari refuses them, when a polyfill helps, and what to build instead.
What is a customized built-in element?
Leaf and Kettle's order page has one big button: Order green tea. Ravi wants it to count the cups as people click. He knows how to make a new tag. But a new tag isn't a button. He would have to rebuild focus, the keyboard, and forms by hand.
Then he reads about the is attribute. Can he keep the real button and only add his code? And why does Asha say it does nothing on her iPhone?
A customized built-in element is a real HTML element, like a button, with your own class added through the is attribute. It keeps everything the built-in element does. It works in Chrome and Firefox. Safari doesn't support it, and has no plans to.
Here is Ravi's button:
<button is="tea-order">Order green tea</button> <script src="tea-order.js"></script>- Line 1 is a normal
<button>, withis="tea-order". That name tells the browser which class to add. - Line 3 loads the class from
tea-order.js. Click the second tab to see it.
In the tea-order.js tab:
- Line 1 makes a class that extends
HTMLButtonElement. That's the class every<button>is built from. SoTeaOrderstarts with everything a button has. - Lines 4 to 8 count the clicks. Each click adds one to
cupsand writes the count on the button. - Line 12 registers the class. The third argument,
{ extends: "button" }, says which built-in tag it belongs to. - Line 13 prints
true. The button on the page is now aTeaOrder.
Click the button in the result twice. It says Order green tea (2 so far). Now press Tab to reach the button, and press Enter. That counts too. Ravi wrote no keyboard code. A real button already handles the keyboard.
TeaOrder class to it.You want to carry tea home on a bicycle. You could build a new vehicle from scratch: wheels, brakes, pedals. Or you could clip a basket onto your bike. It still pedals, brakes, and rings its bell. Anyone can ride it. It just carries more.
- In real life: The bicycle
- In JavaScript: The built-in
<button> - In real life: The basket you clip on
- In JavaScript: Your
TeaOrderclass - In real life: It still pedals, brakes, and rings
- In JavaScript: Focus, the keyboard, and forms, for free
- In real life: Building a vehicle from scratch
- In JavaScript: A new
<tea-button>tag that adds everything itself
A customized built-in element is a real built-in element with your class added. Your class extends the element's own class. define() gets extends, and the HTML gets is. Chrome and Firefox support it. Safari doesn't.
Lesson 1.2 listed it among the parts that don't work in every browser, and lesson 3.1 compared the two kinds of custom element. This lesson goes through it in full.
How do you extend a built-in element with is?
You name the built-in element in three places. Each one has its own job:
- The class extends the element's own class. For a
<button>, that'sHTMLButtonElement. It works like extendingArrayin the JavaScript path's Extending built-ins & instanceof lesson. define()gets{ extends: "button" }. This tells the browser which tag your name belongs to.- The HTML puts
is="tea-order"on that tag. The tag stays<button>. Your name goes inis.
Why say button in the class and again in extends? Because some tags share one class. <q> and <blockquote> both use HTMLQuoteElement, the HTML Standard notes. The class alone can't say which tag you mean.
Now try different tags. Pick a choice, and look for the khaki background. It means the class ran:
<p> <button is="tea-mark">Order green tea</button></p> <script> class TeaMark extends HTMLButtonElement { connectedCallback() { this.style.background = "khaki"; } } customElements.define("tea-mark", TeaMark, { extends: "button" }); console.log(document.querySelector("[is]") instanceof TeaMark);</script>A <button> uses the class HTMLButtonElement, so line 6 and line 11 agree. The browser builds a TeaMark, and connectedCallback() colors it.
The first three work. The last two fail, in two different ways:
- With A button with HTMLElement,
define()works. Then the browser builds the button, and the class doesn't fit a button. Chrome reports aTypeError:localName does not match the HTML element interface. The button stays a plain button. - With Your own tag,
define()throws at once:"tea-label" is a valid custom element name. You can't extend another custom element this way.
Which class goes with which tag?
Every HTML tag is built from one class. Read each row across:
| Which tag? | Which class does it extend? | What goes in extends? |
|---|---|---|
<button> | HTMLButtonElement | "button" |
<tr> | HTMLTableRowElement | "tr" |
<li> | HTMLLIElement | "li" |
<q> or <blockquote> | HTMLQuoteElement, which the two tags share | "q" or "blockquote" |
<section>, <article>, or <nav> | Plain HTMLElement, because these tags have no class of their own | The tag's name, like "section" |
The last row surprises people. <section>, <article>, and <nav> have no class of their own. They use plain HTMLElement. So a customized section extends HTMLElement and passes extends: "section".
Tags you can't extend
extends must name an element that HTML defines. Old tags like <blink> are built from HTMLUnknownElement, a class with no behavior. Chrome refuses them: "blink" is an HTMLUnknownElement. A made-up tag like combobox gets the same answer. The HTML Standard explains why. If your code extended combobox today, HTML could never add a real <combobox> later.
Lifecycle callbacks work the same
A customized built-in gets the same callbacks as any custom element. Lesson 3.6 put them in order. Here are two of them:
<button is="tea-order" tea="green">Order</button> <script> class TeaOrder extends HTMLButtonElement { static observedAttributes = ["tea"]; connectedCallback() { console.log("connected"); } attributeChangedCallback(name, oldValue, newValue) { console.log(name, oldValue, newValue); } } customElements.define("tea-order", TeaOrder, { extends: "button" }); document.querySelector("button").setAttribute("tea", "mint");</script>- Line 1 is a button with
is="tea-order"and ateaattribute. - Line 5 says which attribute to watch. Lesson 3.4 taught observedAttributes.
- Lines 6 to 11 print when the button joins the page, and when
teachanges. - Line 14 changes
teatomint.
The console prints tea null green, then connected, then tea green mint. That's the order lesson 3.6 showed for any upgraded element: attributes first, then connectedCallback(). disconnectedCallback() works too.
How do you create a customized built-in element from JavaScript?
There are two ways. Both need the class to be registered first.
<p>Made from JavaScript:</p> <script> class TeaOrder extends HTMLButtonElement {} customElements.define("tea-order", TeaOrder, { extends: "button" }); const made = document.createElement("button", { is: "tea-order" }); made.textContent = "Order mint tea"; document.body.append(made); console.log(made instanceof TeaOrder, made.hasAttribute("is")); console.log(made.outerHTML); const built = new TeaOrder(); console.log(built.localName);</script>- Line 7 calls
document.createElement()with a second argument,{ is: "tea-order" }. You get a<button>, built as aTeaOrder. - Lines 8 and 9 give it text and put it on the page.
- Line 10 prints
true false. It's aTeaOrder. But it has noisattribute! - Line 11 prints
<button is="tea-order">Order mint tea</button>. Written out as HTML, the name is back. - Line 13 calls
new TeaOrder(). That works too. - Line 14 prints
button. The tag is stillbutton, nottea-order.
Why is the attribute missing? The browser keeps the name in a hidden place on the element, called its is value. The HTML Standard says the attribute "will not be present in the DOM" when you create the element this way. The browser adds it only when it writes the element out as HTML. So a copy of that HTML builds the same element again.
One more way doesn't work. document.createElement("tea-order") makes a <tea-order> tag. The definition belongs to buttons, so that tag stays a plain HTMLElement, with no behavior. The same goes for <tea-order> written in HTML.
Why it matters: the name of a customized built-in never becomes a tag. Make it with is in the HTML, createElement("button", { is }), or new.
Why doesn't changing the is attribute do anything?
Because the browser reads is only once, when it makes the element. The HTML Standard says it plainly: "changing the value of the is attribute does not change the element's behavior". After that, the attribute is just text.
Here are four ways to give a button the name. Guess which ones make a TeaOrder, then pick each one:
<button is="tea-order">Order green tea</button> <script> class TeaOrder extends HTMLButtonElement {} customElements.define("tea-order", TeaOrder, { extends: "button" }); const button = document.querySelector("button"); // The is value came from the HTML. console.log(button instanceof TeaOrder, button.getAttribute("is"));</script>The parser read is="tea-order" while it built the button. That set its is value, so define() upgrades it.
The first two count, because the name arrived while the button was being made. The parser read it from the HTML, or createElement() got it. The third doesn't count. The button was already a plain button when line 8 added the attribute. The fourth stays a TeaOrder. Taking the attribute away later changes nothing.
is attribute can say anything afterwards.A bakery bakes a lemon cake and boxes it. Later, someone writes "chocolate" on the box. The cake is still lemon. And if the label falls off, the cake is still lemon. The flavor was decided in the oven.
- In real life: Baking the cake
- In JavaScript: Making the element
- In real life: The flavor, set in the oven
- In JavaScript: The is value
- In real life: The label on the box
- In JavaScript: The
isattribute - In real life: Writing a new label later
- In JavaScript:
setAttribute("is", …)after the element exists
Why it matters: a script can't turn the buttons already on a page into customized ones by adding is. Put is in the HTML, or pass it to createElement().
Styling it before the upgrade
A plain <button> is always defined. A <button is="tea-order"> isn't, until its class is registered. Until then, it's a working plain button. And the :defined selector from lesson 2.4 doesn't match it.
<style> button:not(:defined) { opacity: 0.4; }</style> <button is="tea-order">Order green tea</button><button>Ask about teas</button> <script> setTimeout(() => { customElements.define("tea-order", class extends HTMLButtonElement {}, { extends: "button" }); }, 2000);</script>- Line 2 fades every button that isn't defined yet.
- Line 5 has
is="tea-order". Line 6 doesn't. - Lines 9 to 11 register
tea-ordertwo seconds after the page loads.
Press Edit & run and watch. For two seconds, only Order green tea is faded. Ask about teas never is. Then the class arrives, the button upgrades, and the fade goes. Lesson 4.1 showed the same upgrade for new tags.
What does a customized built-in keep from the real element?
Everything. The HTML Standard lists what a button with is keeps: "their focus behavior, ability to participate in form submission, the disabled attribute, and so on." That's why customized built-ins exist.
Compare a real button with a new tag that only looks like one:
<style> tea-button { display: inline-block; border: 2px solid gray; padding: 2px 8px; }</style> <form> <button is="tea-order" name="tea" value="green">Order green tea</button> <tea-button name="tea" value="mint">Order mint tea</tea-button></form><p>Sent: <output>nothing yet</output></p> <script src="tea-buttons.js"></script>- Line 2 draws a gray border around
tea-button, so it looks like a button. - Line 6 is a real button with
is="tea-order", a name, and a value. - Line 7 is a
<tea-button>with the same kind of name and value. - Line 9 shows what the form sent.
In the tea-buttons.js tab, lines 1 and 4 make two empty classes. Neither adds any behavior. Lines 8 to 12 run when the form is sent. Line 9 stops the page from reloading. Line 10 reads event.submitter, the button that sent the form. Line 11 shows its name and value.
Now try it in the result:
- Click Order green tea. The page says
Sent: tea=green. - Click Order mint tea. Nothing happens. A
<tea-button>doesn't send forms. - Press Tab. Focus lands on Order green tea. Press Tab again, and focus skips Order mint tea. The new tag can't take focus.
- Press Edit & run and add
disabledto line 6. Tab skips the button, and clicking it sends nothing.
Screen readers see the difference too. They read the accessibility tree, the browser's list of what each thing on the page is. Here is Chrome's:
To make <tea-button> act the same, you would add a tabindex, a role, keys that click, rules for disabled, and form support. The HTML Standard's Drawbacks of autonomous custom elements builds each one by hand. A customized built-in gets them all from the real element.
What a customized built-in can't have
Two tools that later stages use are closed to it:
- ElementInternals, which lets a custom element join forms and set its own role.
attachInternals()throws for any element with an is value. Forms uses it to build form controls. - A shadow root, on most tags. A shadow root is a private DOM tree inside an element. A
<button>can't have one. Only a few tags can, like<p>,<div>, and<section>, as MDN lists.
<button is="tea-order">Order green tea</button> <script> class TeaOrder extends HTMLButtonElement { constructor() { super(); this.attachInternals(); } } customElements.define("tea-order", TeaOrder, { extends: "button" });</script>Line 7 asks for ElementInternals. Chrome reports NotSupportedError: Unable to attach ElementInternals to a customized built-in element. The upgrade fails, so the button never matches :defined. It still shows, and it still works as a button.
Why does a custom element end up outside the table?
Some elements only accept certain children. A table takes rows and cells. The HTML parser, the part of the browser that reads HTML and builds elements, checks this. When it meets a tag that doesn't belong in a table, it moves the tag out.
<style> tea-row, tr[is="tea-row"] { background: khaki; }</style> <table> <tea-row><td>Green tea</td><td>$3</td></tea-row> <tr is="tea-row"><td>Mint tea</td><td>$4</td></tr></table> <script> const table = document.querySelector("table"); console.log(table.previousElementSibling.outerHTML); console.log(table.rows.length);</script>- Line 2 colors both kinds of tea row khaki.
- Line 6 is a new tag,
<tea-row>, around two cells. - Line 7 is a real row,
<tr>, withis="tea-row". - Line 12 prints the element just before the table. Line 13 prints how many rows the table has.
The console prints <tea-row></tea-row> and 2. The parser moved <tea-row> out, just before the table, and left it empty. Its two cells stayed in the table, in a plain row the parser made for them. That's why Green tea isn't khaki. Mint tea's row is.
is stays where you put it.A kitchen drawer has a tray with slots for knives, forks, and spoons. You buy a whisk. It fits no slot, so it goes beside the tray. A fork with your name engraved on it still fits the fork slot.
- In real life: The cutlery tray
- In JavaScript: The
<table> - In real life: The fork slot
- In JavaScript: The place for a
<tr> - In real life: A whisk, put beside the tray
- In JavaScript:
<tea-row>, moved before the table - In real life: A fork with your name engraved
- In JavaScript:
<tr is="tea-row">
Even WebKit, which opposes customized built-ins, calls this the main use case it finds compelling: elements with special parser rules, like <td>.
Options in a select and items in a list
<select> <tea-option>Green tea</tea-option> <option is="tea-option">Mint tea</option></select> <ul> <tea-item>Oolong</tea-item> <li is="tea-item">Chai</li></ul> <script> console.log(document.querySelector("select").options.length);</script>- Lines 1 to 4 are a select with a new
<tea-option>and a real<option is>. - Lines 6 to 9 are a list with a new
<tea-item>and a real<li is>. - Line 12 prints how many options the select has.
It prints 1. Chrome keeps <tea-option> inside the select. But a select only counts <option> elements, so Green tea can't be picked. In the list, Oolong gets no bullet. Chrome's accessibility tree has no list item for it, only its text. Chai is a real list item.
Why it matters: a new tag breaks in these places, and a tag with is doesn't. But is only works in Chrome and Firefox. The practical section builds a table that works everywhere.
Why doesn't Safari support customized built-in elements?
Because WebKit thinks they are the wrong design. It has said so for ten years. Here is the story, in its own words.
2016. In a long thread about is, Ryosuke Niwa, a WebKit engineer, wrote that Apple objects to it, and "this feature won't be supported in WebKit". He called one problem "fundamental", and it's about subclasses. Built-in elements keep gaining features. Say you had extended <input> before type="date" existed, or <img> before srcset. Your class would now miss a feature its parent has.
Programmers call that breaking the Liskov substitution principle: a subclass should work everywhere its parent works. He added that built-in elements were never designed to be subclassed.
You buy a snug case for your phone. Next year's model adds a second camera. The old case covers it. The case was made for the phone as it was, not as it became.
- In real life: The phone
- In JavaScript: A built-in element, like
<input> - In real life: The snug case
- In JavaScript: Your class that extends it
- In real life: Next year's second camera
- In JavaScript: A new feature, like
type="date"orsrcset - In real life: The case covering the camera
- In JavaScript: A subclass that misses the new feature
2018. Chrome 67 and Firefox 63 shipped customized built-ins anyway. WebKit's bug for them, 182671, is closed as WONTFIX: it won't be fixed.
2022 and 2023. Anne van Kesteren of Apple wrote WebKit's full position. Its reasons, in short:
- Form-associated custom elements are a better way to build form controls.
- ElementInternals and ARIA give autonomous elements good accessibility.
- Customized built-ins can't use ElementInternals, and most can't have a shadow root. That makes them "a kind of second-class custom element".
- HTML's usual fallback is child elements, as
<canvas>and<picture>use. So the progressive enhancement argument falls short. - The syntax puts browser makers before web developers.
In August 2023, WebKit marked its position "oppose".
2026. Someone asked WebKit to think again (issue 722). Anne van Kesteren closed it the same day: "No and in fact given the numerous recent parser and sanitizer issues surrounding customized built-in I'm more convinced than ever we made the right choice here." Would fixing those issues change the answer? He replied: "No."
What were those issues? Two kinds:
- The parser. When tags are badly nested, the parser sometimes rebuilds tags like
<b>and<i>. A customized<i is>runs your code in the middle of that, and the rules weren't written for it. Two issues in the HTML Standard are about this: 12327 and 12828. - Sanitizers, tools that strip unsafe HTML. They check attributes. But the is value lives on the element, so removing the attribute doesn't remove the class. You saw that in the playground. The problem came up in the Sanitizer API's own tracker.
What Chrome and Firefox say
Chrome's status page says the feature lets authors "create custom elements which inherit from the semantics of native, built-in elements". Chrome 67 shipped it in 2018, and Firefox 63 followed that October. Both still support it.
In 2026, in the parser issue, Anne van Kesteren asked the browsers that ship it to rethink it. David Baron of Google's Chrome team replied that it made more sense to put it "on a deprecation path" after platform-provided behaviors are stable. That's a newer proposal, at the end of the alternatives section. Deprecation means planning a feature's removal. Nothing has been removed.
What Safari does with your code
Safari's define() reads only two arguments: the name and the class. It never looks at the third. So Safari registers tea-order as a new tag of its own. A <button is="tea-order"> isn't that tag, so it stays a plain button. It still works, and the page goes on. Only your class never runs, as the picture at the top showed.
Why it matters: never put anything essential in a customized built-in's class. In Safari, the plain element is all your readers get.
How do you polyfill customized built-ins in Safari?
A polyfill is a script that adds a missing feature to a browser. For is, the best-known one is @ungap/custom-elements. Its README says it adds the custom elements API, "including the ability to extend builtin elements", in about 2 KB. It asks to be loaded before your other scripts.
You only want it where is doesn't work. This page checks first:
<button is="tea-order">Order green tea</button> <script> const works = document.createElement("button", { is: "tea-check" }).outerHTML.includes("tea-check"); console.log(works); const ready = works ? Promise.resolve() : import("https://unpkg.com/@ungap/custom-elements@1.3.0/es.js"); ready.then(() => import("./tea-order.js"));</script>- Line 4 is the check. It makes a button with
{ is: "tea-check" }and looks for that name in its HTML. Chrome and Firefox write the name into the HTML, as you saw earlier. Safari'screateElement()has noisoption, so the name never appears. No class needs to exist for this check. - Line 5 prints the answer. In Chrome, it's
true. - Line 6 loads the polyfill only when the check fails.
import()loads a script while the page runs, as the Dynamic import lesson shows. The address pins version 1.3.0, so a new release can't change your page. - Line 7 loads
tea-order.jsafter that, so the class is registered once the polyfill is ready.
In Chrome, the page prints true twice, and the polyfill is never downloaded. The second true comes from tea-order.js. It's like checking a hotel's sockets before you unpack your travel adapter.
Polyfills have a cost. They copy a feature with script, and they lean on details of the browser that can change. In 2023, a Safari preview release broke older versions of this polyfill (WebKit bug 258282). The polyfill fixed it in version 1.3.0. WebKit also added a special case to its own code that spots the older versions, and it still carries it.
Why it matters: a polyfill makes is work in Safari, but your page then depends on a script that copies a feature Safari's makers oppose. For most components, the paths below are simpler.
What can you use instead of the is attribute?
There are four paths. The first three work in every browser today. The last is still a proposal.
Wrap a real element
Put a custom element around the real one. Lesson 5.6 built HTML web components this way, and lesson 1.2's last exercise rebuilt its order button as a wrapper. The real button inside keeps focus, the keyboard, forms, and its role, in every browser.
A knitted cosy goes over a real teapot. The teapot still pours, and the cosy keeps the tea warm. Take the cosy off, and you still have a teapot that pours.
- In real life: The teapot
- In JavaScript: The real
<button>inside - In real life: The knitted cosy
- In JavaScript: Your custom element around it
- In real life: Keeping the tea warm
- In JavaScript: The extra behavior your class adds
- In real life: A teapot with no cosy
- In JavaScript: The button, before your script loads
Enhance the elements inside
Where a new tag can't go, like inside a table, put your custom element around the whole thing. It finds the rows and adds behavior. The practical section builds one. You could also mark elements with an attribute, like data-confirm, and let a script find them. But a plain script only finds the elements that exist when it runs. A custom element runs again for every copy of its tag added later.
Build a form control with ElementInternals
For a control HTML doesn't have, like a toggle switch, build an autonomous element. ElementInternals lets it join forms. That's a form-associated custom element, and it works in Chrome, Firefox, and Safari. Forms builds them, a switch among them.
Platform-provided behaviors, a proposal
Microsoft's Edge team proposed platform-provided behaviors. An autonomous element would borrow a built-in element's behavior through ElementInternals. The first one is a button's: sending forms, focus, and the button role. Its explainer set customized built-ins aside: they don't work in every browser and can't use ElementInternals.
Chrome has a trial behind its Experimental Web Platform features flag, since Chrome 149. The change to the HTML Standard, pull request 12409, is still open. Firefox and Safari haven't said what they think yet. MDN also points to custom attributes, a proposal to add behavior to any element through an attribute of your own. Neither is ready to ship.
Read each row across: one question, then the answer for each path.
| Question | Customized built-in | Wrapper around a real element | Form-associated custom element |
|---|---|---|---|
| Does it work in Safari? | No. Safari never runs your class. | Yes. It works in every browser. | Yes, from Safari 16.4. |
| Does it keep the real element's behavior? | Yes. It is the real element. | Yes. The real element is inside it. | No. ElementInternals adds form behavior, and you build the rest. |
Can it use ElementInternals? | No. attachInternals() throws. | Yes, on the wrapper. | Yes. That's how it joins a form. |
| Can it have a shadow root? | Only on a few tags, like <p> and <section>. | Yes, on the wrapper. | Yes. |
| Can it be a table row or an option? | Yes, as <tr is> or <option is>. | No. It goes around the whole table or select. | No. It can't be a <tr> or an <option>. |
Now sort some real jobs. Ask: is there a real element to wrap?
- A counter added to the shop's real Order button
- Sold-out rows greyed out in the real menu table
- A show password button beside a real password input
- A toggle switch, drawn from scratch, sent with the form as on or off
- A tea strength dial that the form sends as a number
- A row of tea color swatches whose choice is sent with the order
Sort each job by the path that works in every browser.
Building a tea table that works in every browser
Asha wants an order table: a row per tea, a box for the number of cups, and a total. With is, each row could be a <tr is="tea-row">, and that fails in Safari. A <tea-row> tag fails everywhere, because the parser moves it out. So Ravi wraps the whole table:
<style> tea-table:not(:defined) p { display: none; }</style> <tea-table> <table> <tr><th>Tea</th><th>Price</th><th>Cups</th></tr> <tr data-price="3"> <td>Green tea</td><td>$3</td> <td><input type="number" min="0" value="0" aria-label="Green tea cups"></td> </tr> <tr data-price="4"> <td>Mint tea</td><td>$4</td> <td><input type="number" min="0" value="0" aria-label="Mint tea cups"></td> </tr> </table> <p>Total: $<output>0</output></p></tea-table> <script src="tea-table.js"></script>- Line 2 hides the total until
tea-tableis defined. Without the script, the reader still gets the table and its boxes, with no wrong total. Lesson 5.6 showed what to hide. - Lines 6 to 16 are a plain table. Each tea row has
data-priceand a number box. The parser keeps every row. - Line 17 holds the total, in an
<output>.
In the tea-table.js tab:
- Line 4 listens for
inputon the wrapper. Typing in any box sends aninputevent that bubbles up to it. - Line 6 goes through every row that has a price.
- Line 7 multiplies the price by the cups.
*turns both strings into numbers. - Line 9 writes the total.
Type 2 in the Mint tea box and 1 in the Green tea box. The total says $11. This works in Chrome, Firefox, and Safari, because nothing uses is. Rows added later count too, since line 6 finds the rows again on every change.
Common mix-ups
- "
isworks in every browser." No. Chrome and Firefox support it. Safari doesn't, and WebKit said no again in September 2026. - "Adding
iswithsetAttribute()turns a button into my element." No. The browser readsisonly when it makes the element. - "Removing
isturns it back into a plain button." No. Its class was fixed when it was made. - "Once
tea-orderis defined,<tea-order>works too." No. A definition withextendsbelongs to one built-in tag. A<tea-order>tag stays plain. - "Any class works with
extends." No. It must be the tag's own class:HTMLButtonElementfor a button,HTMLElementfor a section. Otherwise building it fails. - "A customized built-in can do everything a custom element can." No. It can't use ElementInternals, and most tags can't have a shadow root.
- "In Safari, a page with
isbreaks." No. The element stays a working built-in element. Only your class never runs. - "A polyfill makes Safari the same as Chrome." No. It copies the feature with script, and a Safari change broke older versions of it in 2023.
Practice exercises
Answer first, then use the hints. The results run live, so you can check yourself.
What does line 9 print?
<button>Order green tea</button> <script> class TeaOrder extends HTMLButtonElement {} customElements.define("tea-order", TeaOrder, { extends: "button" }); const button = document.querySelector("button"); button.setAttribute("is", "tea-order"); console.log(button instanceof TeaOrder);</script>false. The parser made a plain button on line 1. Line 8 adds the attribute afterwards, and the browser ignores it.
What does line 4 print?
<script> class TeaOrder extends HTMLButtonElement {} customElements.define("tea-order", TeaOrder, { extends: "button" }); console.log(new TeaOrder().outerHTML);</script><button is="tea-order"></button>. new TeaOrder() makes a button. Its is value is written back as an attribute when the browser turns it into HTML.
This row should turn khaki, but it doesn't. Which class should line 6 extend?
<table> <tr is="tea-row"><td>Green tea</td><td>$3</td></tr></table> <script> class TeaRow extends HTMLElement { connectedCallback() { this.style.background = "khaki"; } } customElements.define("tea-row", TeaRow, { extends: "tr" });</script><table> <tr is="tea-row"><td>Green tea</td><td>$3</td></tr></table> <script> class TeaRow extends HTMLTableRowElement { connectedCallback() { this.style.background = "khaki"; } } customElements.define("tea-row", TeaRow, { extends: "tr" });</script>HTMLTableRowElement. Line 11 says the class belongs to <tr>, so line 6 must extend the class every <tr> is built from. Then the row turns khaki.
How many rows does the table have? Then fix lines 2 and 3 so each tea gets its own row.
<table> <tea-row><td>Green tea</td><td>$3</td></tea-row> <tea-row><td>Mint tea</td><td>$4</td></tea-row></table> <script> console.log(document.querySelector("table").rows.length);</script><table> <tr is="tea-row"><td>Green tea</td><td>$3</td></tr> <tr is="tea-row"><td>Mint tea</td><td>$4</td></tr></table> <script> console.log(document.querySelector("table").rows.length);</script>1. Both <tea-row> tags move out, empty. The first cell starts a plain row, and the next three cells join it, so all four cells share one row. With <tr is="tea-row">, the table has two rows.
What does line 12 print?
<p>Today's tea:</p> <script> const button = document.createElement("button", { is: "tea-green" }); button.setAttribute("is", "tea-mint"); document.body.append(button); class TeaGreen extends HTMLButtonElement {} class TeaMint extends HTMLButtonElement {} customElements.define("tea-mint", TeaMint, { extends: "button" }); customElements.define("tea-green", TeaGreen, { extends: "button" }); console.log(button.constructor.name);</script>TeaGreen. Line 4 set the is value to tea-green. Line 5 only changes the attribute. When line 11 registers tea-green, the button on the page upgrades to TeaGreen, though its attribute still says tea-mint.
Replace the comment on line 13 so a click on a row prints that row's tea. Then click the Oolong row. What does the console print?
<tea-table> <table> <tr><td>Green tea</td><td>$3</td></tr> <tr><td>Mint tea</td><td>$4</td></tr> <tr><td>Oolong</td><td>$5</td></tr> </table></tea-table> <script> class TeaTable extends HTMLElement { constructor() { super(); // When a row is clicked, print the tea in its first cell. } } customElements.define("tea-table", TeaTable);</script><tea-table> <table> <tr><td>Green tea</td><td>$3</td></tr> <tr><td>Mint tea</td><td>$4</td></tr> <tr><td>Oolong</td><td>$5</td></tr> </table></tea-table> <script> class TeaTable extends HTMLElement { constructor() { super(); this.addEventListener("click", (event) => { const row = event.target.closest("tr"); console.log(row.cells[0].textContent); }); } } customElements.define("tea-table", TeaTable);</script>Oolong. The click on a cell bubbles up to <tea-table>. closest("tr") climbs to the row, and its first cell holds the tea's name. No is needed, so it works in every browser.
Check your understanding
For each question, ask two things: when was the element made, and which browser runs it?
Question 1 of 7
tea-orderis defined with{ extends: "button" }. What does<tea-order>Order</tea-order>become?Choose an answer to see the explanation.
Question 2 of 7What does this createElement() page print?
Read the code, then predict<script> class TeaOrder extends HTMLButtonElement {} customElements.define("tea-order", TeaOrder, { extends: "button" }); const made = document.createElement("button", { is: "tea-order" }); console.log(made.hasAttribute("is")); </script>Choose an answer to see the explanation.
Question 3 of 7Which class does a customized
<section>extend?Choose an answer to see the explanation.
Question 4 of 7A page has
<button is="tea-order">Order</button>and definesTeaOrderwithextends: "button". What happens in Safari?Choose an answer to see the explanation.
Question 5 of 7Which of these is one of the reasons WebKit gives for refusing customized built-ins?
Choose an answer to see the explanation.
Question 6 of 7You write
<tea-row>inside a<table>. Where does the HTML parser put it?Choose an answer to see the explanation.
Question 7 of 7You need a button with extra behavior that keeps the keyboard and forms, in every browser. What do you build?
Choose an answer to see the explanation.
Key takeaways
- A customized built-in element is a real element with your class added through
is. - Name the element three times: the class extends its class,
define()getsextends, and the HTML usesis. - Tags with no class of their own, like
<section>, extendHTMLElement. - The is value is fixed when the element is made. Adding or removing the attribute later does nothing.
createElement()withisandnewboth work. Neither adds the attribute until the element is written out as HTML.- It keeps the real element's focus, keyboard, forms,
disabled, and role. It fits where only real rows, options, and list items may go. - It can't use ElementInternals, and most tags can't have a shadow root.
- Safari doesn't support it, and WebKit has refused it since 2016. A polyfill exists, at a cost.
- For every browser, wrap or enhance real elements, or build a form-associated custom element.
Remember the one-liner.is adds your class to a real element in Chrome and Firefox. Wrap the real element when every browser counts.
Sources
The facts on this page come from these standards, docs, positions, and source files:
- HTML Standard: Creating a customized built-in element, WHATWG.
- HTML Standard: Drawbacks of autonomous custom elements, WHATWG.
- is, MDN.
- CustomElementRegistry: define(), Defining a customized built-in element, MDN.
- Document: createElement(), MDN.
- HTML Standard: Custom elements, core concepts, WHATWG.
- Element: attachShadow(), Elements you can attach a shadow to, MDN.
- The is="" attribute is confusing?, WICG web components issue 509.
- Implementation (or not) of customized builtins, WebKit bug 182671.
- Customized built-in elements, WebKit standards positions, issue 97.
- Customized built-in elements (Round 2), WebKit standards positions, issue 722.
- Disallow customized builtin elements from being on the list of active formatting elements, WHATWG HTML issue 12327.
- Adoption agency algorithm can throw after a custom element reaction reparents nodes, WHATWG HTML issue 12828.
- The is non-attribute allows custom elements to sneak in, Sanitizer API issue 387.
- Customized built-in elements, Chrome Platform Status.
- Firefox 63 release notes for developers, MDN.
- JSCustomElementRegistryCustom.cpp, define(), WebKit source at WebKit-7625.2.8.
- @ungap/custom-elements README, version 1.3.0, ungap.
- Broke custom element built-in polyfill, WebKit bug 258282.
- CustomElementRegistry.cpp, addElementDefinition(), WebKit source at WebKit-7625.2.8.
- Platform-Provided Behaviors for Custom Elements, Microsoft Edge explainer.
- Platform-provided behaviors for custom elements, Chrome Platform Status.
- Add platform-provided behaviors for custom elements & submit behavior, WHATWG HTML pull request 12409.
- Proposal: Custom attributes for all elements, WICG web components issue 1029.
Coming next: stage 2 ends here. Shadow DOM, templates, and slots starts with What is shadow DOM?, where a component gets a private inside of its own.