How do you display text code in HTML?

How to display HTML tags as plain text in HTML ?

Improve Article

Save Article

Like Article

  • Difficulty Level : Hard
  • Last Updated : 20 Sep, 2021

  • Read
  • Discuss
  • Courses
  • Practice
  • Video
  • Improve Article

    Save Article

    Basically, there are two methods for displaying HTML tags as plain text.

    1. Using <plaintext> element: Plaintext element is deprecated which means this feature is no longer supported. Though some browsers might still support it, it is not recommended to use.

    2. HTML entities: The second and only option available is using html entities. < ,> are reserved characters in HTML, In order to display this reserved characters  you have to replace them with html entities. You can learn more about entities here. You can either use entity name or entity number initializing them with  & and ending them with ;

     

    Refer to below table for required html entities:

    Sign

    DescriptionEntity nameEntity number<Less than(start of html element)&lt;&#60;>Greater than(end of html element)&gt;&#62;“Double quotation&quot;&#34;&Ampersand ( beginning of html entity)&amp;&#38;

    Example 1: In the first example, we are using html entity names to display body element and paragraph element on web page.

    HTML




    <!DOCTYPE html>

    <html>

      

    <head>

        <<0<1<0>

    <4head>

      

    <<9>

        <html3>

    html5html6html7html8html7>

    html5>2<9 >4<9 >

        <4html3>

    <4<9>

      

    <4html>

    Output:

    How do you display text code in HTML?

    Output

    Explanation: In above code, “<” and “>” is simply replaced by their respective html entities.<pre></pre> is html element which defines preformatted text.

    Example 2: In below example, we are trying to display html entity name for “<” using entity name for “&” sign.

    One of HTML's main jobs is to give text structure so that a browser can display an HTML document the way its developer intends. This article explains the way HTML can be used to structure a page of text by adding headings and paragraphs, emphasizing words, creating lists, and more.

    Prerequisites:Basic HTML familiarity, as covered in Getting started with HTML.Objective:Learn how to mark up a basic page of text to give it structure and meaning — including paragraphs, headings, lists, emphasis, and quotations.

    Most structured text consists of headings and paragraphs, whether you are reading a story, a newspaper, a college textbook, a magazine, etc.

    How do you display text code in HTML?

    Structured content makes the reading experience easier and more enjoyable.

    In HTML, each paragraph has to be wrapped in a

    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    1 element, like so:

    <p>I am a paragraph, oh yes I am.</p>
    

    Each heading has to be wrapped in a heading element:

    <h1>I am the title of the story.</h1>
    

    There are six heading elements:

    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    2,
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    3,
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    4,
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    5,
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    6, and
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    7. Each element represents a different level of content in the document;
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    2 represents the main heading,
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    3 represents subheadings,
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    4 represents sub-subheadings, and so on.

    For example, in this story, the

    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    2 element represents the title of the story, the
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    3 elements represent the title of each chapter, and the
    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    4 elements represent sub-sections of each chapter:

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    

    It's really up to you what the elements involved represent, as long as the hierarchy makes sense. You just need to bear in mind a few best practices as you create such structures:

    • Preferably, you should use a single
      <h2>Live output</h2>
      
      <div class="output" style="min-height: 50px;"></div>
      
      <h2>Editable code</h2>
      <p class="a11y-label">
        Press Esc to move focus away from the code area (Tab inserts a tab character).
      </p>
      
      <textarea id="code" class="input" style="min-height: 100px; width: 95%">
      My short story I am a statistician and my name is Trish.
      
      My legs are made of cardboard and I am married to a fish.
      </textarea>
      
      <div class="playable-buttons">
        <input id="reset" type="button" value="Reset" />
        <input id="solution" type="button" value="Show solution" />
      </div>
      
      2 per page—this is the top level heading, and all others sit below this in the hierarchy.
    • Make sure you use the headings in the correct order in the hierarchy. Don't use
      <h2>Live output</h2>
      
      <div class="output" style="min-height: 50px;"></div>
      
      <h2>Editable code</h2>
      <p class="a11y-label">
        Press Esc to move focus away from the code area (Tab inserts a tab character).
      </p>
      
      <textarea id="code" class="input" style="min-height: 100px; width: 95%">
      My short story I am a statistician and my name is Trish.
      
      My legs are made of cardboard and I am married to a fish.
      </textarea>
      
      <div class="playable-buttons">
        <input id="reset" type="button" value="Reset" />
        <input id="solution" type="button" value="Show solution" />
      </div>
      
      4 elements to represent subheadings, followed by
      <h2>Live output</h2>
      
      <div class="output" style="min-height: 50px;"></div>
      
      <h2>Editable code</h2>
      <p class="a11y-label">
        Press Esc to move focus away from the code area (Tab inserts a tab character).
      </p>
      
      <textarea id="code" class="input" style="min-height: 100px; width: 95%">
      My short story I am a statistician and my name is Trish.
      
      My legs are made of cardboard and I am married to a fish.
      </textarea>
      
      <div class="playable-buttons">
        <input id="reset" type="button" value="Reset" />
        <input id="solution" type="button" value="Show solution" />
      </div>
      
      3 elements to represent sub-subheadings—that doesn't make sense and will lead to weird results.
    • Of the six heading levels available, you should aim to use no more than three per page, unless you feel it is necessary. Documents with many levels (for example, a deep heading hierarchy) become unwieldy and difficult to navigate. On such occasions, it is advisable to spread the content over multiple pages if possible.

    To answer this question, let's take a look at text-start.html—the starting point of our running example for this article (a nice hummus recipe). You should save a copy of this file on your local machine, as you'll need it for the exercises later on. This document's body currently contains multiple pieces of content. They aren't marked up in any way, but they are separated with line breaks (Enter/Return pressed to go onto the next line).

    However, when you open the document in your browser, you'll see that the text appears as a big chunk!

    How do you display text code in HTML?

    This is because there are no elements to give the content structure, so the browser does not know what is a heading and what is a paragraph. Furthermore:

    • Users looking at a web page tend to scan quickly to find relevant content, often just reading the headings, to begin with. (We usually spend a very short time on a web page.) If they can't see anything useful within a few seconds, they'll likely get frustrated and go somewhere else.
    • Search engines indexing your page consider the contents of headings as important keywords for influencing the page's search rankings. Without headings, your page will perform poorly in terms of SEO (Search Engine Optimization).
    • Severely visually impaired people often don't read web pages; they listen to them instead. This is done with software called a screen reader. This software provides ways to get fast access to given text content. Among the various techniques used, they provide an outline of the document by reading out the headings, allowing their users to find the information they need quickly. If headings are not available, they will be forced to listen to the whole document read out loud.
    • To style content with CSS, or make it do interesting things with JavaScript, you need to have elements wrapping the relevant content, so CSS/JavaScript can effectively target it.

    Therefore, we need to give our content structural markup.

    Let's jump straight in with a live example. In the example below, add elements to the raw text in the Input field so that it appears as a heading and two paragraphs in the Output field.

    If you make a mistake, you can always reset it using the Reset button. If you get stuck, press the Show solution button to see the answer.

    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    

    Semantics are relied on everywhere around us—we rely on previous experience to tell us what the function of an everyday object is; when we see something, we know what its function will be. So, for example, we expect a red traffic light to mean "stop," and a green traffic light to mean "go." Things can get tricky very quickly if the wrong semantics are applied. (Do any countries use red to mean "go"? We hope not.)

    In a similar way, we need to make sure we are using the correct elements, giving our content the correct meaning, function, or appearance. In this context, the

    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    2 element is also a semantic element, which gives the text it wraps around the role (or meaning) of "a top level heading on your page."

    <h1>This is a top level heading</h1>
    

    By default, the browser will give it a large font size to make it look like a heading (although you could style it to look like anything you wanted using CSS). More importantly, its semantic value will be used in multiple ways, for example by search engines and screen readers (as mentioned above).

    On the other hand, you could make any element look like a top level heading. Consider the following:

    <span style="font-size: 32px; margin: 21px 0; display: block;">Is this a top level heading?</span>
    

    This is a

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    
    8 element. It has no semantics. You use it to wrap content when you want to apply CSS to it (or do something to it with JavaScript) without giving it any extra meaning. (You'll find out more about these later on in the course.) We've applied some CSS to it to make it look like a top level heading, but since it has no semantic value, it will not get any of the extra benefits described above. It is a good idea to use the relevant HTML element for the job.

    Now let's turn our attention to lists. Lists are everywhere in life—from your shopping list to the list of directions you subconsciously follow to get to your house every day, to the lists of instructions you are following in these tutorials! Lists are everywhere on the web, too, and we've got three different types to worry about.

    Unordered lists are used to mark up lists of items for which the order of the items doesn't matter. Let's take a shopping list as an example:

    milk
    eggs
    bread
    hummus
    

    Every unordered list starts off with a

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    
    9 element—this wraps around all the list items:

    <ul>
    milk
    eggs
    bread
    hummus
    </ul>
    

    The last step is to wrap each list item in a

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    0 (list item) element:

    <h1>I am the title of the story.</h1>
    
    0

    Active learning: Marking up an unordered list

    Try editing the live sample below to create your very own HTML unordered list.

    <h1>I am the title of the story.</h1>
    
    1

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    

    <h1>I am the title of the story.</h1>
    
    3

    Ordered lists are lists in which the order of the items does matter. Let's take a set of directions as an example:

    <h1>I am the title of the story.</h1>
    
    4

    The markup structure is the same as for unordered lists, except that you have to wrap the list items in an

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    1 element, rather than
    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    
    9:

    <h1>I am the title of the story.</h1>
    
    5

    Active learning: Marking up an ordered list

    Try editing the live sample below to create your very own HTML ordered list.

    <h1>I am the title of the story.</h1>
    
    6

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    

    <h1>I am the title of the story.</h1>
    
    8

    So at this point in the article, you have all the information you need to mark up our recipe page example. You can choose to either save a local copy of our text-start.html starting file and do the work there or do it in the editable example below. Doing it locally will probably be better, as then you'll get to save the work you are doing, whereas if you fill it in to the editable example, it will be lost the next time you open the page. Both have pros and cons.

    <h1>I am the title of the story.</h1>
    
    9

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    1

    If you get stuck, you can always press the Show solution button, or check out our text-complete.html example on our GitHub repo.

    It is perfectly OK to nest one list inside another one. You might want to have some sub-bullets sitting below a top-level bullet. Let's take the second list from our recipe example:

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    2

    Since the last two bullets are very closely related to the one before them (they read like sub-instructions or choices that fit below that bullet), it might make sense to nest them inside their own unordered list and put that list inside the current fourth bullet. This would look like so:

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    3

    Try going back to the previous active learning example and updating the second list like this.

    In human language, we often emphasize certain words to alter the meaning of a sentence, and we often want to mark certain words as important or different in some way. HTML provides various semantic elements to allow us to mark up textual content with such effects, and in this section, we'll look at a few of the most common ones.

    When we want to add emphasis in spoken language, we stress certain words, subtly altering the meaning of what we are saying. Similarly, in written language we tend to stress words by putting them in italics. For example, the following two sentences have different meanings.

    I am glad you weren't late.

    I am glad you weren't late.

    The first sentence sounds genuinely relieved that the person wasn't late. In contrast, the second one, with both the words "glad" and "late" in italics, sounds sarcastic or passive-aggressive, expressing annoyance that the person arrived a bit late.

    In HTML we use the

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    3 (emphasis) element to mark up such instances. As well as making the document more interesting to read, these are recognized by screen readers, which can be configured to speak them in a different tone of voice. Browsers style this as italic by default, but you shouldn't use this tag purely to get italic styling. To do that, you'd use a
    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    
    8 element and some CSS, or perhaps an
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    5 element (see below).

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    4

    To emphasize important words, we tend to stress them in spoken language and bold them in written language. For example:

    This liquid is highly toxic.

    I am counting on you. Do not be late!

    In HTML we use the

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    6 (strong importance) element to mark up such instances. As well as making the document more useful, again these are recognized by screen readers, which can be configured to speak them in a different tone of voice. Browsers style this as bold text by default, but you shouldn't use this tag purely to get bold styling. To do that, you'd use a
    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    
    8 element and some CSS, or perhaps a
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    8 element (see below).

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    5

    You can nest strong and emphasis inside one another if desired:

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    6

    In this active learning section, we've provided an editable example. Inside it, we'd like you to try adding emphasis and strong importance to the words you think need them, just to have some practice.

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    7

    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    

    <h1>The Crushing Bore</h1>
    
    <p>By Chris Mills</p>
    
    <h2>Chapter 1: The dark night</h2>
    
    <p>
      It was a dark night. Somewhere, an owl hooted. The rain lashed down on the…
    </p>
    
    <h2>Chapter 2: The eternal silence</h2>
    
    <p>Our protagonist could not so much as a whisper out of the shadowy figure…</p>
    
    <h3>The specter speaks</h3>
    
    <p>
      Several more hours had passed, when all of a sudden the specter sat bolt
      upright and exclaimed, "Please have mercy on my soul!"
    </p>
    
    9

    The elements we've discussed so far have clearcut associated semantics. The situation with

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    8,
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    5, and
    <h1>This is a top level heading</h1>
    
    1 is somewhat more complicated. They came about so people could write bold, italics, or underlined text in an era when CSS was still supported poorly or not at all. Elements like this, which only affect presentation and not semantics, are known as presentational elements and should no longer be used because, as we've seen before, semantics is so important to accessibility, SEO, etc.

    HTML5 redefined

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    8,
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    5, and
    <h1>This is a top level heading</h1>
    
    1 with new, somewhat confusing, semantic roles.

    Here's the best rule you can remember: It's only appropriate to use

    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    8,
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    5, or
    <h1>This is a top level heading</h1>
    
    1 to convey a meaning traditionally conveyed with bold, italics, or underline when there isn't a more suitable element; and there usually is. Consider whether
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    6,
    const textarea = document.getElementById('code');
    const reset = document.getElementById('reset');
    const solution = document.getElementById('solution');
    const output = document.querySelector('.output');
    const code = textarea.value;
    let userEntry = textarea.value;
    
    function updateCode() {
      output.innerHTML = textarea.value;
    }
    
    const htmlSolution = `<h1>My short story</h1>
    <p>
      I am a statistician and my name is Trish.
    </p>
    <p>
      My legs are made of cardboard and I am married to a fish.
    </p>`;
    
    let solutionEntry = htmlSolution;
    
    reset.addEventListener('click', () => {
      textarea.value = code;
      userEntry = textarea.value;
      solutionEntry = htmlSolution;
      solution.value = 'Show solution';
      updateCode();
    });
    
    solution.addEventListener('click', () => {
      if (solution.value === 'Show solution') {
        textarea.value = solutionEntry;
        solution.value = 'Hide solution';
      } else {
        textarea.value = userEntry;
        solution.value = 'Show solution';
      }
      updateCode();
    });
    
    textarea.addEventListener('input', updateCode);
    window.addEventListener('load', updateCode);
    
    // Stop tab key tabbing out of textarea and
    // make it write a tab at the caret position instead
    textarea.onkeydown = (e) => {
      if (e.keyCode === 9) {
        e.preventDefault();
        insertAtCaret('\t');
      }
    
      if (e.keyCode === 27) {
        textarea.blur();
      }
    };
    
    function insertAtCaret(text) {
      const scrollPos = textarea.scrollTop;
      let caretPos = textarea.selectionStart;
    
      const front = textarea.value.substring(0, caretPos);
      const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
      textarea.value = front + text + back;
      caretPos += text.length;
      textarea.selectionStart = caretPos;
      textarea.selectionEnd = caretPos;
      textarea.focus();
      textarea.scrollTop = scrollPos;
    }
    
    // Update the saved userCode every time the user updates the text area code
    textarea.onkeyup = function(){
      // We only want to save the state when the user code is being shown,
      // not the solution, so that solution is not saved over the user code
      if (solution.value === 'Show solution') {
        userEntry = textarea.value;
      } else {
        solutionEntry = textarea.value;
      }
    
      updateCode();
    };
    
    3,
    <span style="font-size: 32px; margin: 21px 0; display: block;">Is this a top level heading?</span>
    
    0, or
    html {
      font-family: sans-serif;
    }
    
    h2 {
      font-size: 16px;
    }
    
    .a11y-label {
      margin: 0;
      text-align: right;
      font-size: 0.7rem;
      width: 98%;
    }
    
    body {
      margin: 10px;
      background: #f5f9fa;
    }
    
    8 might be more appropriate.

    Always keep an accessibility mindset. The concept of italics isn't very helpful to people using screen readers, or to people using a writing system other than the Latin alphabet.

    • const textarea = document.getElementById('code');
      const reset = document.getElementById('reset');
      const solution = document.getElementById('solution');
      const output = document.querySelector('.output');
      const code = textarea.value;
      let userEntry = textarea.value;
      
      function updateCode() {
        output.innerHTML = textarea.value;
      }
      
      const htmlSolution = `<h1>My short story</h1>
      <p>
        I am a statistician and my name is Trish.
      </p>
      <p>
        My legs are made of cardboard and I am married to a fish.
      </p>`;
      
      let solutionEntry = htmlSolution;
      
      reset.addEventListener('click', () => {
        textarea.value = code;
        userEntry = textarea.value;
        solutionEntry = htmlSolution;
        solution.value = 'Show solution';
        updateCode();
      });
      
      solution.addEventListener('click', () => {
        if (solution.value === 'Show solution') {
          textarea.value = solutionEntry;
          solution.value = 'Hide solution';
        } else {
          textarea.value = userEntry;
          solution.value = 'Show solution';
        }
        updateCode();
      });
      
      textarea.addEventListener('input', updateCode);
      window.addEventListener('load', updateCode);
      
      // Stop tab key tabbing out of textarea and
      // make it write a tab at the caret position instead
      textarea.onkeydown = (e) => {
        if (e.keyCode === 9) {
          e.preventDefault();
          insertAtCaret('\t');
        }
      
        if (e.keyCode === 27) {
          textarea.blur();
        }
      };
      
      function insertAtCaret(text) {
        const scrollPos = textarea.scrollTop;
        let caretPos = textarea.selectionStart;
      
        const front = textarea.value.substring(0, caretPos);
        const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
        textarea.value = front + text + back;
        caretPos += text.length;
        textarea.selectionStart = caretPos;
        textarea.selectionEnd = caretPos;
        textarea.focus();
        textarea.scrollTop = scrollPos;
      }
      
      // Update the saved userCode every time the user updates the text area code
      textarea.onkeyup = function(){
        // We only want to save the state when the user code is being shown,
        // not the solution, so that solution is not saved over the user code
        if (solution.value === 'Show solution') {
          userEntry = textarea.value;
        } else {
          solutionEntry = textarea.value;
        }
      
        updateCode();
      };
      
      5 is used to convey a meaning traditionally conveyed by italic: foreign words, taxonomic designation, technical terms, a thought…
    • const textarea = document.getElementById('code');
      const reset = document.getElementById('reset');
      const solution = document.getElementById('solution');
      const output = document.querySelector('.output');
      const code = textarea.value;
      let userEntry = textarea.value;
      
      function updateCode() {
        output.innerHTML = textarea.value;
      }
      
      const htmlSolution = `<h1>My short story</h1>
      <p>
        I am a statistician and my name is Trish.
      </p>
      <p>
        My legs are made of cardboard and I am married to a fish.
      </p>`;
      
      let solutionEntry = htmlSolution;
      
      reset.addEventListener('click', () => {
        textarea.value = code;
        userEntry = textarea.value;
        solutionEntry = htmlSolution;
        solution.value = 'Show solution';
        updateCode();
      });
      
      solution.addEventListener('click', () => {
        if (solution.value === 'Show solution') {
          textarea.value = solutionEntry;
          solution.value = 'Hide solution';
        } else {
          textarea.value = userEntry;
          solution.value = 'Show solution';
        }
        updateCode();
      });
      
      textarea.addEventListener('input', updateCode);
      window.addEventListener('load', updateCode);
      
      // Stop tab key tabbing out of textarea and
      // make it write a tab at the caret position instead
      textarea.onkeydown = (e) => {
        if (e.keyCode === 9) {
          e.preventDefault();
          insertAtCaret('\t');
        }
      
        if (e.keyCode === 27) {
          textarea.blur();
        }
      };
      
      function insertAtCaret(text) {
        const scrollPos = textarea.scrollTop;
        let caretPos = textarea.selectionStart;
      
        const front = textarea.value.substring(0, caretPos);
        const back = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
        textarea.value = front + text + back;
        caretPos += text.length;
        textarea.selectionStart = caretPos;
        textarea.selectionEnd = caretPos;
        textarea.focus();
        textarea.scrollTop = scrollPos;
      }
      
      // Update the saved userCode every time the user updates the text area code
      textarea.onkeyup = function(){
        // We only want to save the state when the user code is being shown,
        // not the solution, so that solution is not saved over the user code
        if (solution.value === 'Show solution') {
          userEntry = textarea.value;
        } else {
          solutionEntry = textarea.value;
        }
      
        updateCode();
      };
      
      8 is used to convey a meaning traditionally conveyed by bold: keywords, product names, lead sentence…
    • <h1>This is a top level heading</h1>
      
      1 is used to convey a meaning traditionally conveyed by underline: proper name, misspelling…

    Note: People strongly associate underlining with hyperlinks. Therefore, on the web, it's best to only underline links. Use the

    <h1>This is a top level heading</h1>
    
    1 element when it's semantically appropriate, but consider using CSS to change the default underline to something more appropriate on the web. The example below illustrates how it can be done.

    <h2>Live output</h2>
    
    <div class="output" style="min-height: 50px;"></div>
    
    <h2>Editable code</h2>
    <p class="a11y-label">
      Press Esc to move focus away from the code area (Tab inserts a tab character).
    </p>
    
    <textarea id="code" class="input" style="min-height: 100px; width: 95%">
    My short story I am a statistician and my name is Trish.
    
    My legs are made of cardboard and I am married to a fish.
    </textarea>
    
    <div class="playable-buttons">
      <input id="reset" type="button" value="Reset" />
      <input id="solution" type="button" value="Show solution" />
    </div>
    
    0

    You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: HTML text basics.

    That's it for now! This article should have given you a good idea of how to start marking up text in HTML and introduced you to some of the most important elements in this area. There are a lot more semantic elements to cover in this area, and we'll look at a lot more in our Advanced text formatting article later on in the course. In the next article, we'll be looking in detail at how to create hyperlinks, possibly the most important element on the web.

    How do I display text as code in HTML?

    The HTML. We use the <code> tag to indicate that our text is actually code. We use the <pre> tag because in this case, we actually want to browser to display the white space that we show. This allows the code to be spaced properly.

    How do I display HTML code on a website?

    Open your browser and navigate to the page for which you wish to view the HTML..
    Right-click on the page to open the right-click menu after the page finishes loading..
    Click the menu item that allows you to view the source. ... .
    When the source page opens, you'll see the HTML code for the full page..

    How do I display text in a box in HTML?

    How to Make a Text Box in HTML.
    Step 1: Create a label element. To start, create a <label> element. ... .
    Step 2: Create an input element. ... .
    Step 3: Define any other attributes needed. ... .
    Text Box with Maxlength Attribute. ... .
    Text Box with Minlength Attribute. ... .
    Text Box with Placeholder Attribute. ... .
    Text Box with Size Attribute..

    How do I display HTML code on a website without executing it?

    Use HTML Special Character Codes var myCode = "<b>This is not bold</b>"; $('span#code-span'). text(myCode); Using text instead of html will cause tags to be rendered exposed instead of being executed.