DEV Community

Cover image for Using a Custom Element
Nikki Massaro Kauffman
Nikki Massaro Kauffman

Posted on • Edited on

4 2

Using a Custom Element

Recently I gave a lightning talk for the HighEdWeb Associations's annual conference called "Old School HTML with Modern Web Components" making the case for web components.

Since then I've been getting lots of follow-up questions about how to get started. I think what scares most people off of them is that we often start with how to make your own, which is honestly doesn't give you the immediate return on your learning investment. So what I've decided to do a series that starts at a much easier and more immediately rewarding point.

For newcomers dependencies, bundling, and polyfills are a bit intimidating, so for now, let's use a script where all of that has been taken care of for you. To that end, let's with the simplest most useful task: using an existing web component with a single script.

  1. Add the Javascript. We have a CDN for the web components we use. Add the following script to any HTML page:

    <script> 
    window.__appCDN="https://cdn.webcomponents.psu.edu/cdn/"; </script>
    <script src="https://cdn.webcomponents.psu.edu/cdn/build.js"></script>
    
  2. Use the custom element HTML. For example, try accent-card:

    <accent-card accent-color="red" dark horizontal image-src="https://webcomponents.psu.edu/styleguide/elements/accent-card/demo/images/image1.jpg">
    <h1 slot="heading">Look at this card</h1>
    <div slot="content">This is the card content.</div>
    </accent-card>
    
  3. Make some changes. Check out the accent-card demo to learn how to change the card's accent-color, orientation, heading, content, and more. Accent Card demo with an arrow pointing to the panel on the right and the demo/HTML on the left

  4. Try more custom elements. Browse our CDN to try more:

Next you'll learn how to make your own magic script for dynamically importing web components.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay