Step 1 / 119%

Step 1

HTML Structure

10 min

Lay out the page skeleton: display, keys and data-* attributes.

Definition

HTML (HyperText Markup Language) is the language that describes the structure of a web page: it states what each element represents (a heading, a paragraph, a button...) using tags. It doesn't deal with appearance, which is CSS's job, nor with behavior, which is JavaScript's job.

Attributes (such as id, aria-label or data-digit) complete a tag by adding information to it: an identifier, a description for screen readers, or a piece of data that JavaScript can read later.

In this project, you will build a real calculator from scratch: it performs the four operations, chains calculations, handles division by zero and can also be used with the keyboard. All you need is a code editor and a browser.

The project unfolds in several steps, always in the same order: first the structure with HTML, then the style, and finally the behavior with JavaScript. This separation of roles is a good habit: it makes each file simpler to write, to reread and to fix.

Finished calculator

This is what you will have built by the end of the project.

1. Prepare the folder

Create a calculator folder with two files inside: calculator.html and calculator.js. Leave the JavaScript file empty for now; it will be filled in starting from step 4.

Keeping all the project's files in one folder simplifies the links between them: the HTML page can load the script simply by its name, with no complicated path. Choose names without spaces or accents, to avoid compatibility problems.

2. Write the page skeleton

In calculator.html, write only the structure: no classes, no styles. The meaning of each element first, the appearance afterwards.

calculator.html (new file)
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Calculator</title>
    <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
  </head>

  <body>
    <main>
      <!-- Display -->
      <div aria-live="polite">
        <p id="previous"></p>
        <p id="current">0</p>
      </div>

      <!-- Keys -->
      <div id="keys">
        <div>
          <button data-action="clear" aria-label="Clear all">C</button>
          <button data-action="delete" aria-label="Delete last digit">⌫</button>
          <button data-action="percent" aria-label="Percent">%</button>
          <button data-operator="/" aria-label="Divide">÷</button>
        </div>
        <div>
          <button data-digit="7">7</button>
          <button data-digit="8">8</button>
          <button data-digit="9">9</button>
          <button data-operator="*" aria-label="Multiply">×</button>
        </div>
        <div>
          <button data-digit="4">4</button>
          <button data-digit="5">5</button>
          <button data-digit="6">6</button>
          <button data-operator="-" aria-label="Subtract">−</button>
        </div>
        <div>
          <button data-digit="1">1</button>
          <button data-digit="2">2</button>
          <button data-digit="3">3</button>
          <button data-operator="+" aria-label="Add">+</button>
        </div>
        <div>
          <button data-action="sign" aria-label="Change sign">±</button>
          <button data-digit="0">0</button>
          <button data-action="decimal" aria-label="Decimal point">.</button>
          <button data-action="equals" aria-label="Equals">=</button>
        </div>
      </div>
    </main>

    <script src="calculator.js"></script>
  </body>
</html>

3. Reading the skeleton from top to bottom

Take the time to reread the code to pick out its main parts:

  • The <head> contains the invisible information: the character encoding, the mobile configuration, the tab title and the scripts to load.
  • The <main> groups the main content of the page. It is a semantic tag: it indicates the role of the content, which helps screen readers and search engines.
  • The first <div> is the calculator's display, with two lines: the calculation in progress and the number being entered.
  • The second <div> is the keypad: twenty buttons arranged in the order of a real calculator, row by row.

Note that each button is a real <button> and not a simple <div>: a button is keyboard-accessible and recognized by screen readers without any extra work.

Key takeaways

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"> loads Tailwind CSS from a CDN: a single line, nothing to install. It is ideal for learning; on a real production site, the compiled version is used instead.
  • <script src="calculator.js"> is placed at the very bottom of the <body>: by the time the JavaScript runs, all the elements of the page already exist.
  • id="previous" and id="current" are the two lines of the display: the top one shows the calculation in progress, the bottom one the number being entered.
  • id="keys" wraps all the keys. A single click listener will be enough (step 6).
  • The data-* attributes give each key a role: data-digit for a digit, data-operator for an operator, data-action for everything else. JavaScript will read them with button.dataset.
  • The data-operator values (/, *, -, +) are the ones used for calculating; the symbols ÷, ×, − are only for display.
  • aria-label describes the symbol keys (⌫, ÷, ×…) to screen readers, and aria-live="polite" announces every change of the display to them.

Why no style for now?

You may have noticed that the code contains no Tailwind classes, even though the script is already loaded. This is intentional: separating structure from style lets you first check that the page is correct, without being distracted by the appearance. In the next step, you will add the classes one by one and watch the calculator take shape.

Unstyled calculator structure

It isn't pretty, and that's normal: structure first, style afterwards.

Common mistakes to avoid

  • Forgetting the exclamation mark in <!DOCTYPE html> or closing a tag in the wrong place: the browser tries to fix things on its own, but the display becomes unpredictable.
  • Getting the script name wrong: calculator.js must have exactly the same name as in the <script> tag, including capital letters.
  • Forgetting a data attribute: a key without data-digit, data-operator or data-action will not react to clicks later.

Checkpoint

Open calculator.html in your browser (double-click, or use the Live Server extension in VS Code). You should see a 0 followed by 20 stacked buttons. None of them reacts yet.

If the page is blank or if tags are displayed as text, check the opening and closing of every tag, and that the file was saved with the .html extension.