Highlight text with HTML <mark> tag

Highlight text with HTML <mark> tag

Highlight text with HTML <mark> tag

If you ever need to mark text within a paragraph, better use the <mark> tag. It's HTML version of a yellow highlighter. I've always used a <span> tag with some CSS styling sprinkled on it, not realizing this more semantic option existed. HTML5 is filled with all kind of goodies, am I right 😆

<p>
  <mark>Highlight</mark>
  text with HTML &lt;mark&gt; tag
</p>
Default <mark> styling

The default background color of <mark> is yellow.

<p>
  <mark>Default Yellow Highlight</mark>
</p>

Output

Default Yellow Highlight
Custom Styling <mark> with CSS

Of course, like any text HTML tag, you can apply custom styling with CSS. You can think of it similarly to how you would style a <p> tag.

mark {
  background: red;
  color: white;
}

Output

Custom Highlight
<mark> vs Other Text HTML Tags


strong

<strong> is used to indicate text that has strong importance than the surrounding text, such as a warning or error. Semantically, its importance. It appears as bold

b

<b> is very similar to <strong> as it also appears as bold. However, unlike it, it doesn't really convey any importance and it's more a stylistic thing than semantics.

em

<em> is used to stress emphasis on a particular word. It appears as italics

mark

<mark> merely highlights the relevance of a certain piece of text. Prior to the existence of this tag, many have used em or strong to give the highlighted content some semantic meaning. Well no more! If you need to highlight, use this tag 🌟

Why semantic HTML tag is important

The reason you don't just use <div> tags everywhere is because they are not semantic. You might be like me when I first started learning programming - who cares about being semantically-correct 🙄. Wrong ❌. In fact, search engines like Google do! Because semantics convey meaning about your site. When search robots crawl through your site, they'll know what's up. In other words, ding ding ding on your SEO or search engine optimization 🏆

Another reason to be semantically-correct is for accessibility. A lot of the accessibility tools rely on the tags' semantics to convert your site into meaning to the human user using it (ie. screen readers). Here's an analogy. Remember back in the days, when we got the computer reading text from a site. It sounded super robotic and odd 🤖. Without the proper semantics, it's just like that. DDoes it work, sure - but the listening experience is terrible 😱. However, when you use proper semantics, it's like listening to Siri. It sounds way more human-y because it has all the different inflection, changes in pitch, and even know when to pause. And this is the similar type of better experience you can achieve when using semantically-correct HTML tags 👍


HTML5 tag and SEO

I do want to point out one thing though.

Google's John Mueller mentioned this in a Twitter response:

It certainly makes sense to use HTML5 properly if you can, there's no SEO downside to doing so :).

Here's what I gather from this. Whether or not you use HTML5 tags, it won't hurt your Google search result ranking. However, does that mean you should use HTML5 tags. Not at all! The accessibility benefits are still there. And some HTML5 tags have really interesting browser behavior and it opens up your user to more advanced feature that it didn't before 🤩

Accessibility Concerns

Alright, hopefully, I have successfully conveyed to you the importance of semantic HTML tags. And you can now understand how <mark> is not simply to style texts, but it's semantically a good thing.

But! there is some concern with its accessibility. Unfortunately, the use of the <mark>tag is not announced by most screen readers in its default settings. But good news, there's is a way around it. You can use the CSS content property and pseudo-element to make an announcement.

mark::before {
  content: " [highlight start] ";
}

mark::after {
content: " [highlight end] ";
}

/* Hide the text created in the CSS content property */
mark::before,
mark::after {
clip-path: inset(100%);
clip: rect(1px, 1px, 1px, 1px);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}

So what the screen reader will do here. When it encounters text that's wrapped in the <mark> tag, it will announce "highlight start" followed by the text within the tag. and then announce "highlight end" once the mark tag ends.

BUT...

Please note: if you have a lot of these "announcement", it can be very verbose and add in sometimes annoying unnecessary information. Which can cause some screen reader users to turn off this feature. So, the lesson here is. "With great power comes great responsibility 🕷". Don't abuse this technique and ONLY apply it in instances where NOT knowing the highlighted content can adversely affect understanding for the user.

Use Case for <mark>

The fun part now! Let's take at some use cases for <mark>:

Use Case: Search Result

Here's a popular one. You can use it to highlight the term a user has searched for.

<p>Search Result for "Vue"</p>

<hr>

<p><mark>Vue</mark> is a awesome JavaScript framework. <mark>Vue</mark> is awesome. Can you tell I really like <mark>Vue</mark> 😆</p>


Output

Search Result for "Vue"
Vue is a awesome JavaScript framework. Vue is awesome. Can you tell I really like Vue 😆

Use Case: Quotes

It's great to provide highlights for quotation (<q>) and block quote (<blockquote>).

<p>According to Samantha, <q>Vue is <mark>AWESOME</mark></q></p>


Output

According to Samantha, Vue is AWESOME


Originally published on https://www.samanthaming.com


Understanding of Meter Tag in HTML 5

Understanding of Meter Tag in HTML 5

This post explains the HTML 5 meter tag and how to create it

What is the meter tag?

I this article I am explaining the meter tag. The output of both (progress and meter) tags is the same as we will see but there is the difference that actually the meter tag is used to represent a scalar measurement within a known range. The value can be fractional.Examples

Disk uses, the relevance of a query result, the fraction of a voting population to have selected a specific candidate.

What is the difference between the progress tag and the meter tag? Progress bar is used to display the progress of a specific task. Or a progress element represents the completion progress of a task. Whereas the meter tag is used to represent guages.  We can think that a progress tag represents dynamic data whereas a meter tag represents static data.

Note:

  1. According to the W3C, the meter element should not be used to indicate progress, because to indicate the progress we have the progress tag.
  2. The meter element also does not represent a scalar value of an arbitrary range; for example, it would be wrong to use this to report a weight, or height, unless there is a known maximum value.

Syntax

The Meter tag is an inline element, the same as a header, progress and so on.

Attributes

<meter></meter>   

Apart from the Global Attributes and Event Attributes, the meter tag have 6 more attributes as shown in the following table:

The following inequalities must hold, as applicable:

  • min <= value <= max
  • min <= low <= max (if low is specified)
  • min <= high <= max (if high is specified)
  • min <= optimum <= max (if optimum is specified)
  • low <= high (if both low and high are specified)

Note: if you do not specify min or max then the range will be between 0.0 to 1.0 and the value must be in that range.

HTML DOM Meter Object

In the DOM the meter element is defined by METER that represents for HTML 5.

  • How the meter object can be accessed: using the getElementById() method you can access the element.
var x = document.getElementById("[Give id of the meter tag here]");  
  • Create a meter object: You can also create the element dynamically using the createElement() method:
var x = document.createElement("METER");    
  • Meter object properties: The properties of a meter object are given in the following table:

Example 1: This example will show you how can you use a meter element in HTML 5.

<!DOCTYPE html>        
<html lang="en">        
<head>        
    <meta charset="utf-8" />        
</head>        
<body>        
   <b>Meter without value</b>    
   <meter></meter>    
    
   <br/><br/>    
   <b>Meter with value but without min and max attribute</b>    
   <meter value="0.8"></meter>    
    
   <br/><br/>    
   <b>Meter with value, min and max attribute</b>    
   <meter min="0" max="100" value="17"></meter>    
    
   <br/><br/>    
   <b>Meter (when "min <= value < low")</b>    
   <meter  min="0" max="100" value="17" low="25" high="75"></meter>    
    
   <br/><br/>    
   <b>Meter (when "high < value <= max")</b>    
   <meter  min="0" max="100" value="80" low="25" high="75"></meter>    
    
   <br/><br/>    
   <b>Meter (when "low <= value <= high")</b>    
   <meter  min="0" max="100" value="50" low="25" high="75"></meter>    
    
   <br/><br/>    
   <b>Meter with optimum attribute</b>    
   <meter  min="0" max="100" value="24" low="25" high="75" optimum="80"></meter>    
    
   <br/><br/>    
   <b>Meter with optimum attribute</b>    
   <meter  min="0" max="100" value="80" low="25" high="75" optimum="20"></meter>    
    
</body>        
</html>     

The output of example 1:

Styling meter element: we can define various types of styles for the meter element using a progress selector.Example 2: This example will explain how to increase the width and height of a meter element.

Example 2: This example will explain how to increase the width and height of a meter element.

<!DOCTYPE html>        
<html lang="en">        
<head>        
    <meta charset="utf-8" />     
   <style>    
   meter {    
     width: 400px;    
     height: 25px;    
   }    
   </style>       
</head>        
<body>        
   <b>Meter without value</b>    
   <meter></meter>    
    
   <br/><br/>    
   <b>Meter with value but without min and max attribute</b>    
   <meter value="0.8"></meter>    
    
   <br/><br/>    
   <b>Meter with value, min and max attribute</b>    
   <meter min="0" max="100" value="17"></meter>    
       
   <br/><br/>    
   <b>Meter (when "min <= value < low")</b>    
   <meter  min="0" max="100" value="17" low="25" high="75"></meter>    
    
   <br/><br/>    
   <b>Meter (when "high < value <= max")</b>    
   <meter  min="0" max="100" value="80" low="25" high="75"></meter>    
    
   <br/><br/>    
   <b>Meter (when "low <= value <= high")</b>    
   <meter  min="0" max="100" value="50" low="25" high="75"></meter>    
    
   <br/><br/>    
   <b>Meter with optimum attribute</b>    
   <meter  min="0" max="100" value="24" low="25" high="75" optimum="80"></meter>    
    
   <br/><br/>    
   <b>Meter with optimum attribute</b>    
   <meter  min="0" max="100" value="80" low="25" high="75" optimum="20"></meter>    
    
</body>        
</html>     

Output of the Example 2:

More in styling:

  • WebKit/Blink Browser: like Opera, Googe Chrome and Safari
  • FireFox
  • Internet Explorer

1. WebKit/Blink Browser: According to webkit.org, we can have 5 different pseudo classes that are given in the following table.

Example 3:  This example explains how to reset the appearance of a meter element. To reset the appearance we use -webkit-appearance:none;

<!DOCTYPE html>          
<html lang="en">          
<head>          
    <meta charset="utf-8" />       
<style>      
meter {      
  width: 300px;      
  height: 25px;      
  -webkit-appearance: none; /* Reset appearance */      
  border: 1px solid #ccc;      
  border-radius: 5px;      
}      
</style>         
</head>          
<body>          
<b>Meter without value</b>      
<meter></meter>      
      
<br/><br/>      
<b>Meter with value but without min and max attribute</b>      
<meter value="0.8"></meter>      
      
<br/><br/>      
<b>Meter with value, min and max attribute</b>      
<meter min="0" max="100" value="17"></meter>      
      
<br/><br/>      
<b>Meter (when "min <= value < low")</b>      
<meter  min="0" max="100" value="17" low="25" high="75"></meter>      
      
<br/><br/>      
<b>Meter (when "high < value <= max")</b>      
<meter  min="0" max="100" value="80" low="25" high="75"></meter>      
      
<br/><br/>      
<b>Meter (when "low <= value <= high")</b>      
<meter  min="0" max="100" value="50" low="25" high="75"></meter>      
      
<br/><br/>      
<b>Meter with optimum attribute</b>      
<meter  min="0" max="100" value="24" low="25" high="75" optimum="80"></meter>      
      
<br/><br/>      
<b>Meter with optimum attribute</b>      
<meter  min="0" max="100" value="80" low="25" high="75" optimum="20"></meter>      
      
</body>          
</html>      

Output of the Example 3:

Example 4: How to render the background container?

According to the preceding table I have described that we can render the container using the -webkit-meter-bar pseudo-class, so to apply the rendering I am using the following code:

meter::-webkit-meter-bar {    
  background: none;     
  background-color: whiteSmoke;    
  box-shadow: 0 5px 5px -5px #00F inset;    
  border: 1px solid #0ff;    
  border-radius: 5px;    
}  

Complete Code

<!DOCTYPE html>        
<html lang="en">        
<head>        
    <meta charset="utf-8" />     
<style>    
meter {    
  width: 300px;    
  height: 25px;    
  -webkit-appearance: none; /* Reset appearance */    
  border: 1px solid #F0f;    
  border-radius: 5px;    
}    
meter::-webkit-meter-bar {    
  background: none;     
  background-color: whiteSmoke;    
  box-shadow: 0 5px 5px -5px #00F inset;    
  border: 1px solid #0ff;    
  border-radius: 5px;    
}    
    
</style>       
</head>        
<body>        
<b>Meter without value</b>    
<meter></meter>    
    
<br/><br/>    
<b>Meter with value but without min and max attribute</b>    
<meter value="0.8"></meter>    
    
<br/><br/>    
<b>Meter with value, min and max attribute</b>    
<meter min="0" max="100" value="17"></meter>    
    
<br/><br/>    
<b>Meter (when "min <= value < low")</b>    
<meter  min="0" max="100" value="17" low="25" high="75"></meter>    
    
<br/><br/>    
<b>Meter (when "high < value <= max")</b>    
<meter  min="0" max="100" value="80" low="25" high="75"></meter>    
    
<br/><br/>    
<b>Meter (when "low <= value <= high")</b>    
<meter  min="0" max="100" value="50" low="25" high="75"></meter>    
    
<br/><br/>    
<b>Meter with optimum attribute</b>    
<meter  min="0" max="100" value="24" low="25" high="75" optimum="80"></meter>    
    
<br/><br/>    
<b>Meter with optimum attribute</b>    
<meter  min="0" max="100" value="80" low="25" high="75" optimum="20"></meter>    
    
</body>        
</html>     

Output of the Example 4:

Example 5: This example explains how to render the value of the meter element, it means inside the container how to apply the rendering.

According to the table we can render the meter elements using the following pseudo classes:

-webkit-meter-optimum-value  
-webkit-meter-suboptimum-value  
-webkit-meter-even-less-good-value  

Code: I am using the following code:

  1. For -webkit-meter-optimum-value:
meter::-webkit-meter-optimum-value {    
  box-shadow: 0 5px 5px -5px #999 inset;    
  background-image: linear-gradient(    
    25deg,     
    #002900 5%,     
    #003D00 5%,    
    #005200 25%,    
    #007A00 25%,    
    #00A300 55%,    
    #00CC00 55%,    
    #33D633 95%,    
    #66E066 95%,    
    #99EB99 100%    
  );    
  background-size: 100% 100%;    
}   
  1. For -webkit-meter-suboptimum-value:
meter::-webkit-meter-suboptimum-value {    
  box-shadow: 0 5px 5px -5px #999 inset;    
  background-image: linear-gradient(    
    25deg,     
    #333300 5%,     
    #666600 5%,    
    #999900 25%,    
    #CCCC00 25%,    
    #FFFF00 55%,    
    #FFFF33 55%,    
    #FFFFCC 95%,    
    #FF3300 95%,    
    #B22400 100%    
  );    
  background-size: 100% 100%;    
}   
  1. For -webkit-meter-even-less-good-value:
meter::-webkit-meter-even-less-good-value  {    
  box-shadow: 0 5px 5px -5px #999 inset;    
  background-image: linear-gradient(    
    25deg,     
    #000000 5%,     
    #330000 5%,    
    #660000 25%,    
    #990000 25%,    
    #CC0000 55%,    
    #FF0000 55%,    
    #FF3333 95%,    
    #FF6666 95%,    
    #FF9999 100%    
  );    
  background-size: 100% 100%;    
}   

Complete Code:

<!DOCTYPE html>        
<html lang="en">  
   <head>  
      <meta charset="utf-8" />  
      <style>    
         meter {    
         width: 300px;    
         height: 25px;    
         -webkit-appearance: none; /* Reset appearance */    
         border: 1px solid #F0f;    
         border-radius: 5px;    
         }    
         meter::-webkit-meter-bar {    
         background: none;     
         background-color: whiteSmoke;    
         box-shadow: 0 5px 5px -5px #00F inset;    
         border: 1px solid #0ff;    
         border-radius: 5px;    
         }    
         meter::-webkit-meter-optimum-value {    
         box-shadow: 0 5px 5px -5px #999 inset;    
         background-image: linear-gradient(    
         25deg,     
         #002900 5%,     
         #003D00 5%,    
         #005200 25%,    
         #007A00 25%,    
         #00A300 55%,    
         #00CC00 55%,    
         #33D633 95%,    
         #66E066 95%,    
         #99EB99 100%    
         );    
         background-size: 100% 100%;    
         }    
         meter::-webkit-meter-suboptimum-value {    
         box-shadow: 0 5px 5px -5px #999 inset;    
         background-image: linear-gradient(    
         25deg,     
         #333300 5%,     
         #666600 5%,    
         #999900 25%,    
         #CCCC00 25%,    
         #FFFF00 55%,    
         #FFFF33 55%,    
         #FFFFCC 95%,    
         #FF3300 95%,    
         #B22400 100%    
         );    
         background-size: 100% 100%;    
         }    
         meter::-webkit-meter-even-less-good-value  {    
         box-shadow: 0 5px 5px -5px #999 inset;    
         background-image: linear-gradient(    
         25deg,     
         #000000 5%,     
         #330000 5%,    
         #660000 25%,    
         #990000 25%,    
         #CC0000 55%,    
         #FF0000 55%,    
         #FF3333 95%,    
         #FF6666 95%,    
         #FF9999 100%    
         );    
         background-size: 100% 100%;    
         }    
      </style>  
   </head>  
   <body>        
      <b>Meter without value</b>    
      <meter></meter>    
      <br/><br/>    
      <b>Meter with value but without min and max attribute</b>    
      <meter value="0.8"></meter>    
      <br/><br/>    
      <b>Meter with value, min and max attribute</b>    
      <meter min="0" max="100" value="17"></meter>    
      <br/><br/>    
      <b>Meter (when "min <= value < low")</b>    
      <meter  min="0" max="100" value="17" low="25" high="75"></meter>    
      <br/><br/>    
      <b>Meter (when "high < value <= max")</b>    
      <meter  min="0" max="100" value="80" low="25" high="75"></meter>    
      <br/><br/>    
      <b>Meter (when "low <= value <= high")</b>    
      <meter  min="0" max="100" value="50" low="25" high="75"></meter>    
      <br/><br/>    
      <b>Meter with optimum attribute</b>    
      <meter  min="0" max="100" value="24" low="25" high="75" optimum="80"></meter>    
      <br/><br/>    
      <b>Meter with optimum attribute</b>    
      <meter  min="0" max="100" value="80" low="25" high="75" optimum="20"></meter>    
   </body>  
</html> 

Output of the Example 5:

2. FireFox Browser: For the FireFox browser use "-moz" instead of "-webkit" and everything else is the same.

3. Internet Explorer: In IE you can use all these properties directly, like appearance.

Thank you for reading ! I hope this tutorial will surely help!

The Ultimate Guide to Web Development with HTML and CSS

The Ultimate Guide to Web Development with HTML and CSS

The Ultimate Guide to Web Development with HTML and CSS. Learn Web Development by creating a Blog with HTML and CSS. Make a web applications using HTML5 and CSS3. Create a blog from Scratch.

The Ultimate Guide to Web Development with HTML and CSS

The Ultimate Guide to Web Development with HTML and CSS. Learn Web Development by creating a Blog with HTML and CSS. Make a web applications using HTML5 and CSS3. Create a blog from Scratch.

If you want to create your own website or want to become a web developer, you are in the right place. This course, you are going to learn about html language which is the most important skill you must know in web developer career.

In addition, html is used to create website skeleton, to make them look nice, you must apply CSS style to them. With the latest technology, you will be able to make a web-page layout and structure according to what you want or what your client want.

Honestly, There are many courses available on the internet include free and fee. how ever, most of them are really hard to follow along. That is the reason that I create this web development course on Udemy which is easier to understand. Do you want to spend a month to learn or just spend less time but get the concept and have ability to develop a website? When you know its concept, you will no longer need any teacher. I mean you can learn and research more by yourself.

What you'll learn

  • Make a web applications using HTML5 and CSS3
  • Create a blog from stretch
  • Understand and have skill how website work
  • Be a comfortable front-end developer

Render HTML with Vanilla JavaScript and lit-html

Render HTML with Vanilla JavaScript and lit-html

Sometimes you need to render HTML elements on a web page. And like Goldilocks' search for "just right", you have to try a few techniques before you find the right one. Using a framework may be too hard. Using pure HTML and the DOM API may be too soft. What you need is something in the middle that is just right. Is lit-html "just right"? Let's find out.

Sometimes you need to render HTML elements on a web page. And like Goldilocks' search for "just right", you have to try a few techniques before you find the right one. Using a framework may be too hard. Using pure HTML and the DOM API may be too soft. What you need is something in the middle that is just right. Is lit-html "just right"? Let's find out.

First, I'll show how this all works. Then at the end of this article, I'll explain everything you need to get started with lit-html to try this for yourself.

When you're done, you can push your HTML app with lit-html to the cloud to see it in all of its glory! I included a link to a free Azure trial, so you can try it yourself.

Resources:

The Sample App

Here is the app I'll demonstrate in this article. It fetches a list of heroes and renders them when you click the button. It also renders a progress indicator while it is fetching.

What's the Value of lit-html

When you focus on rendering content, and nothing else, lit-html is a good fit. It works closely with the DOM to render content, and refresh it in an optimal manner. The docs can provide you with more details, but the basic code for lit-html looks like this.

// Credit: As seen in official docs https://lit-html.polymer-project.org/guide/getting-started

// Import lit-html
import { html, render } from 'lit-html';

// Define a template
const myTemplate = name =>
  html`
    <p>Hello ${name}</p>
  `;

// Render the template to the document
render(myTemplate('World'), document.body);

You import lit-html, define a template, then render it to the DOM. That's it!

Rendering HTML

A progress bar is fairly basic. There is some HTML, and we show it when needed and hide it when it is not required. While we could use a template, or innerHTML, or the DOM API for this, let's see what this would look like with lit-html.

First, we get a reference to the element in the DOM where the progress bar will appear.

Then we define the template. This code looks and feels like JSX (or TSX). The advantage here is that you can write the HTML. You wrap the HTML in a template string (notice the back-tick character is used and not a single quote). Template strings allow you to span lines and insert variables where needed (we'll see this soon). The magic that makes this work is the html tag that precedes the template string. The html tag is what tells lit-html that you are about to define a template.

Next, we compile the template and pass those results to lit-html's render function, which places the results in the DOM. Finally, we hide or show the progress bar as needed.

function showProgress(show = true) {
  const container = document.getElementById('progress-placeholder');

  const template: () => TemplateResult = () => html`
    <progress class="progress is-medium is-info" max="100"></progress>
  `;
  const result = template();
  render(result, container);

  container.style.display = show ? 'block' : 'none';
}

Now you can run this showProgress function any time you want to show the progress bar.

Note that when a template is re-rendered, the only part that is updated is the data that changed. If no data changed, nothing is updated.

Rendering HTML with Dynamic Values

The progress bar does not change each time it is rendered. You will have situations where you want your HTML to change. For example, you may have a message area on your web app that shows a styled message box with a title and a message. The title and message will change every time you show the message area. Now you have dynamic values.

The HTML is defined with a template string, so it is trivial to add a variable into it. Notice the code below adds a title and text into the template, using the ${data.title} and ${data.text} syntax, respectively.

Then the template is compiled and rendered were needed.

When this template is re-rendered, the only part that is updated is the data that changed. In this case, that's the title and text.

function showMessage(text: string, title = 'Info') {
  const template: (data: any) => TemplateResult = (data: Message) => html`
    <div id="message-box" class="message is-info">
      <h3 class="message-header">${data.title}</h3>
      <p class="message-body">${data.text}</p>
    </div>
  `;

  const el = document.getElementById('message-placeholder');
  const result = template({ title, text });
  render(result, el);

  el.style.visibility = !!text ? 'visible' : 'hidden';
}

Rendering a List

Things get a little more real when we render a list. Let's think about that for a moment. A list requires that we have a plan if there is data and a backup plan if there is no data. A list requires that we render the same thing for each row, and we don't know how many rows we have. A list requires that we pass different values for each row, too. Then we have to take the rows and wrap them in a container such as a <ul> or a <table>.

So there is a little more logic here, regardless of whether we use lit-html or any other technique. Let's explore how the replaceHeroList function renders the rows using lit-html.

function replaceHeroList(heroes?: Hero[]) {
 const heroPlaceholder = document.querySelector('.hero-list');

 // Define the template
 let template: () => TemplateResult;

 if (heroes && heroes.length) {
   // Create the template for every hero row
   template = createList();
 } else {
   // Create the template with a simple "not found" message
   template = () =>
     html`
       <p>heroes not found</p>
     `;
 }

 // Compile the template
 const result = template();

 // Render the template
 render(result, heroPlaceholder);

Notice that when there are heroes, we call the createList function. This function begins by creating an array of TemplateResult. So for every hero in the heroes array, we define a template that represents the <li> containing the HTML that displays that respective hero.

Then we create another template that contains the <ul> and embeds the array of hero templates. It's pretty cool that we can embed templates like this! Finally, we return it all and let the logic compile the templates and render them.

function createList() {
  // Create an array of the templates for each hero
  const templates: TemplateResult[] = heroes.map(hero => {
    return html`
      <li>
        <div class="card">
          <div class="card-content">
            <div class="content">
              <div class="name">${hero.name}</div>
              <div class="description">${hero.description}</div>
            </div>
          </div>
        </div>
      </li>
    `;
  });

  // Create a template that includes the hero templates
  const ulTemplate: () => TemplateResult = () =>
    html`
      <ul>
        ${templates}
      </ul>
    `;
  return ulTemplate;
}

Summary

When you want to render HTML, lit-html is a fast and light-weight option. Is it better than using templates and the DOM API? You'll have to decide what is best for you. But the real story here is that you have another great option to consider when determining the right tool for your job.

Prologue

You can also get editor help with your lit-html templates. Notice the image below shows the syntax highlighting for the HTML template!

Setup

You can install the lit-html package with npm.

npm install lit-html

Alternately you can load it directly from the unpkg.com CDN

import { html, render } from 'https://unpkg.com/lit-html?module';

You have a choice here. npm is my preference, but feel 100% free to use the CDN if that suits you.

TypeScript and lit-html

You only need to include the library for lit-html and you're done. But I like to use TypeScript, and I absolutely recommend enabling your tooling to work great with typeScript and lit-html.

Let me be very clear here - you do not need TypeScript. I choose to use it because it helps identify mistakes while I write code. If you don't want TypeScript, you can opt to use plain JavaScript.

Here are the steps to make TypeScript and lit-html light up together:

  1. Install TypeScript support for lit-html
  2. Configure your tsconfig.json file
  3. Install the VS Code extension for lit-html

Run this command to install the plugin and typescript, as development dependencies to your project.

npm install --save-dev typescript-lit-html-plugin typescript

Edit your tsconfig.json by adding the following to your compilerOptions section.

"compilerOptions": {
  "plugins": [
    {
      "name": "typescript-lit-html-plugin"
    }
  ]
}

Finally, install the VS Code extension for lit-html.

Now you get syntax highlighting for all of your lit-html templates!