HTML Tutorial

HTML Classes

class names let CSS and JavaScript target groups of elements. One element can have many classes.

Names for groups

The class attribute puts an element into one or more named groups. CSS uses those names to style every member the same way. JavaScript can find the group and change it. The name is not unique: as many elements as you like can share a class.

Write the name in the HTML. Select it in CSS with a dot in front: .note means “every element whose class list includes note”.

A single class

The most common pattern is one descriptive name. Here every tip uses class="note".

Example

<p class="note">Oven is hot. Use the cloth.</p>
<p class="note">Soup of the day: tomato.</p>
<p>Regular paragraph, no extra class.</p>

Two paragraphs belong to the note group. The third does not. Style or script the group without touching every other p on the page.

One element, many classes

Separate class names with spaces. The element is a member of each group at once. Order in the attribute does not matter to HTML. CSS source order and specificity decide which rules win when they conflict.

Example

<div class="card featured">Featured lunch</div>
<div class="card">Regular lunch</div>
<p class="note warning">Allergen: nuts in the pesto.</p>

The first card is both a card and featured. Shared styles live on .card. Extra styles for the highlight live on .featured. The warning note can look like other notes and still use a stronger color from .warning.

Select a class in CSS

In CSS, a class selector starts with a dot. That is how you tell a class name from a tag name:p is every paragraph; .card is every element with that class.

Example

<style>
  .card {
    border: 1px solid #cbd5e1;
    padding: 1rem;
    border-radius: 0.5rem;
    max-width: 18rem;
  }
  .card h2 {
    margin-top: 0;
  }
  .price {
    font-weight: 700;
  }
</style>

<div class="card">
  <h2>Espresso</h2>
  <p>Short and hot.</p>
  <p class="price">2.50</p>
</div>
<div class="card">
  <h2>Still water</h2>
  <p class="price">1.00</p>
</div>

Both cards share the same box. Only the price line uses .price. Try this in/html/try and add a third card — you do not copy the CSS again.

Class is not unique

That is the point. Reuse a class on every similar element. If you need a name that must appear only once on the page, that is an id — the next chapter.

classid
How many on a pageAs many as you needOne of each name
CSS selector.card#intro
Several names on one tagYes, space-separatedOne id value
Typical useStyle a groupJump link, label, one-off target

Classes and JavaScript

Scripts find groups with document.querySelectorAll('.card') ordocument.getElementsByClassName('card'). The same name you used for CSS is the name you use in the script. You will see a tiny click example in the JavaScript chapter. For now, remember: class names are hooks for both style and behavior.

Class names are case-sensitive. card and Card are different groups. Stick to lowercase and hyphens: menu-item, note, is-active.

Name them for people

Pick names that say what the group is, not how it looks today. card andnote still make sense if you change the color. blue-box does not.

  • Letters, digits, hyphens, and underscores. Start with a letter.
  • Do not put a dot in the HTML. The dot belongs only in the CSS selector.
  • Do not use spaces inside one name. Spaces start a second class.

Next: id for names that must be unique — jump links, form labels, and one-off styles.

Worked examples

The short listings above show the tag in isolation. These pages use the same markup on documents you would actually publish: a lab report, a clinic form, a timetable, a weather card.

HTML does not calculate. It names the pieces so a browser, a screen reader, and a search engine can tell a heading from a paragraph. The numbers below are classroom values — the same Ohm, pH, and pulse figures as the C track — now sitting in real page structure.

Preview them in the HTML editor at /html/try. Change a heading or a number and watch the page, not a print log.

Chemistry

Reuse a warning class

class names a group. CSS can target .acid on every bottle card. One element may have several classes, separated by spaces.

Pick names from meaning (warning, acid) not from looks (red-box). When the colour changes, the meaning should not.

Example

<style>
  .acid { background: #fee2e2; border-left: 4px solid #b91c1c; padding: 0.5rem; }
</style>
<p class="acid">HCl — corrosive</p>
<p class="acid">H2SO4 — corrosive</p>

Statistics

Pass and fail chips

Two classes, two looks. The markup stays a paragraph. The class is the switch. That is how a results board lights green or red without new tags.

Example

<style>
  .pass { color: #166534; }
  .fail { color: #9f1239; }
</style>
<p class="pass">Ada 81 — pass</p>
<p class="fail">Cara 38 — resit</p>

FAQ: HTML Classes

Common questions about this page.

What is the StudyGrid HTML tutorial?

The StudyGrid HTML tutorial is a full beginner track: tags, headings, links, images, tables, layout, forms, and media. Each chapter has copy-and-run examples.

Should I run html classes examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn html classes in this HTML HTML lesson (HTML Classes).

Is the HTML editor the same as Try Python?

No. Try HTML is a live page preview at /html/try. Try Python stays at /try and runs Python. HTML lessons never open the Python editor.

Do I need to install anything to learn HTML?

No. Open a chapter, click Try it in HTML, and the page preview updates in the browser. You can also download a .html file and open it locally.

Where should I start the HTML tutorial?

Start at HTML Intro, then Basic, Elements, and Attributes. After the first document, continue to headings, links, and forms. Use Next at the bottom of each chapter.

Is the HTML tutorial free?

Yes. The HTML studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live preview editor.