Home › HTML Masterclass › Part 3

HTML Tutorial Part 3: Page Layout, Semantic HTML and Forms

Lessons 21 to 30 of 38. Understand block and inline elements, div and span, class and id, semantic tags, and every important form input and validation attribute.

Part 1Part 2Part 3Part 4

In this part

6. Layout and Semantics

7. Forms & Inputs

6. Layout and Semantics

Module 6: Display BehaviourIntermediate (Core Fundamental)

21. Block vs Inline (Block vs Inline Deep Dive)

The nature of elements on screen: taking up the whole line or sitting side by side

Real-life analogy

A block element is like a big brick that takes up the full width of the wall, while an inline element is like a thread that flows along with the words on the same line.

Explanation

Every element in the browser has a default display behaviour:

1. Block-level Elements:
  • Always start on a new line.
  • Take up the full 100% width of the screen.
  • CSS Width, Height, Margin and Padding work on them.
  • E.g.: <div>, <p>, <h1>-<h6>, <ul>, <form>
2. Inline Elements:
  • Do not start on a new line; they sit side by side on the same line.
  • Take up only as much width as the content needs.
  • Width and Height do not work directly on them.
  • E.g.: <span>, <a>, <strong>, <img>

Golden rules

  • Never put a large block element inside an inline element.

Common mistake

Trying to apply width to an inline element with CSS (it doesn't work unless you set display: inline-block).

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px;">
  <div style="background-color: #fed7aa; border: 2px solid #f97316; padding: 8px; margin-bottom: 8px;">
    This is a <strong>&lt;div&gt;</strong> (Block): it takes up the full width.
  </div>
  <p>
    This is a normal sentence in which 
    <span style="background-color: #bbf7d0; border: 1px solid #22c55e; padding: 2px 6px;">
      &lt;span&gt; (Inline)
    </span>
    and a 
    <a href="#" style="background-color: #bfdbfe; padding: 2px 6px; color: #1d4ed8;">
      &lt;a&gt; link
    </a>
    are sitting side by side on the same line!
  </p>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which of these is a block-level element?

  1. <span>
  2. <a>
  3. <div>
  4. <strong>
Show answer

Answer: <div>

<div> is a block-level element that takes up the whole line.

Module 6: Generic Containers0 Level Beginner

22. <div> vs <span> (Generic Containers)

The 2 most widely used boxes for CSS styling and layout

Real-life analogy

Div is a big empty carton box that can be filled with many things, and Span is a small transparent plastic bag that fits in the middle of a line!

Explanation

These two are used when you don't have any specific semantic tag:

  • <div> (Division): The most prominent block-level container. It is used to build sections, cards and grid layouts of a web page.
  • <span>: The most prominent inline container. It is used to change the colour or style of just 1 or 2 words inside a paragraph.

Golden rules

  • Use div only when necessary; if possible, use meaningful tags (header, main, section).

Common mistake

Filling the whole page with nothing but 100 divs (Div Soup).

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px;">
  <!-- div: block container -->
  <div style="background: #f8fafc; border: 1px solid #cbd5e1; padding: 12px; border-radius: 8px;">
    <h4 style="margin: 0 0 6px 0;">This is inside the div</h4>
    <p style="margin: 0; font-size: 13px;">
      In this sentence <span style="color: #dc2626; font-weight: bold;">only this word is red</span> (via &lt;span&gt;).
    </p>
  </div>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which tag is most suitable for changing the colour of just one word in the middle of a paragraph?

  1. <div>
  2. <p>
  3. <span>
  4. <section>
Show answer

Answer: <span>

<span> is an inline tag that lets you apply styling without breaking the flow of the sentence.

Module 6: Identification & SelectionIntermediate

23. class vs id

Defining the identity of elements for CSS and JavaScript

Real-life analogy

Class is like a 'school uniform' that many students can wear at the same time, while ID is like a student's 'Aadhaar card number' or roll number, which can belong to only one person in the entire school!

Explanation

1. The class attribute (class):

  • You can give the same class name to many different elements on the page.
  • In CSS it is selected with a dot .classname.
  • An element can have more than one class (e.g. class="btn").

2. The id attribute (id):

  • On an entire HTML page, an ID name can be given to one and only one element (Unique).
  • In CSS it is selected with a hash #idname.
  • It is essential for document.getElementById() in JavaScript.

Golden rules

  • An ID name must not start with a number (Number).
  • Never give two different tags the same ID within one page.

Common mistake

Writing the same id="header" three times on one page. This makes the JavaScript code stop working.

Example code

<!DOCTYPE html>
<html>
<head>
  <style>
    .card { background: #f8fafc; border: 1px solid #cbd5e1; padding: 10px; border-radius: 6px; margin-bottom: 8px; }
    .highlight { border-color: #10b981; background: #ecfdf5; font-weight: bold; }
    #special-box { background: #eff6ff; border: 2px solid #3b82f6; }
  </style>
</head>
<body style="font-family: sans-serif; padding: 20px;">
  <div class="card">Card 1 (plain class)</div>
  <div class="card highlight">Card 2 (two classes together)</div>
  <div class="card" id="special-box">Card 3 (this one has a unique ID)</div>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which symbol is placed before the name to style a 'Class' in CSS?

  1. Hash (#)
  2. Dot (.)
  3. At (@)
  4. Slash (/)
Show answer

Answer: Dot (.)

In CSS, a class is preceded by a dot (.) and an ID by a hash (#).

Module 6: Modern SemanticsIntermediate

24. Semantic HTML in Full (Semantic Elements)

<header>, <nav>, <main>, <section>, <article>, <aside>, <footer>, <details>

Real-life analogy

If you fill a book with only pages, with no index and no chapter names, it will be hard to understand. Semantic tags, like a book's chapters, header and footer, clearly tell you what each piece of content is.

Explanation

Semantic means 'meaningful'. HTML5 gave us meaningful tags:

<header>: The top part of the website (logo, title)
<nav>: The main navigation links and menu
<main>: The main unique content of the page
<section>: A separate part of a topic (Section)
<article>: A standalone blog post or news story
<aside>: A sidebar or related links
<footer>: Copyright and contact information
<details>: An FAQ accordion that opens on click without JS

Golden rules

  • Search engines (Google) look at semantic tags and give the website a higher rank on Google.
  • There should be only one <main> tag on a page.

Common mistake

Using just a <div> instead of <nav> for a navigation menu. Because of this, screen readers cannot tell blind users about the menu.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; margin: 0; padding: 15px; background: #f1f5f9;">
  <header style="background: #0f172a; color: white; padding: 10px; border-radius: 6px 6px 0 0;">
    <h3 style="margin: 0;">My Website (&lt;header&gt;)</h3>
    <nav style="margin-top: 4px;"><a href="#" style="color: #38bdf8;">Home</a> | <a href="#" style="color: #38bdf8;">Contact</a></nav>
  </header>
  <main style="background: white; padding: 12px; border-left: 1px solid #cbd5e1; border-right: 1px solid #cbd5e1;">
    <article>
      <h4 style="color: #047857; margin: 0;">Blog Title (&lt;article&gt;)</h4>
      <p style="font-size: 13px;">Semantic tags improve SEO.</p>
    </article>
    <details style="background: #f8fafc; padding: 6px; border-radius: 4px; font-size: 12px;">
      <summary style="font-weight: bold; cursor: pointer; color: #0284c7;">FAQ: Is HTML easy?</summary>
      Yes, it is very easy!
    </details>
  </main>
  <footer style="background: #e2e8f0; text-align: center; padding: 6px; font-size: 11px; border-radius: 0 0 6px 6px;">
    © 2026 All rights reserved (&lt;footer&gt;)
  </footer>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which semantic tag is best for holding a website's main navigation menu and links?

  1. <menu>
  2. <links>
  3. <nav>
  4. <header>
Show answer

Answer: <nav>

The <nav> tag is made specifically for a group of navigation links.

7. Forms & Inputs

Module 7: Form StructureIntermediate to Advanced

25. HTML Forms: Methods, Enctype and How They Work

<form>, action, GET vs POST, enctype='multipart/form-data'

Real-life analogy

Think of a post office envelope: GET is a transparent postcard whose message everyone can see, while POST is a sealed envelope that stays safely confidential.

Explanation

How the <form> tag works for collecting data from a user (login, signup, payment):

  • action="server.php": Which server file the data will go to be processed when the form is submitted.
  • method="GET": Sends the data by appending it to the browser's URL (for example ?name=rahul&age=20). This is fine for a search bar, but completely unsafe for passwords!
  • method="POST": Sends the data hidden inside the HTTP body, safely. Only POST is used for passwords and large forms.
  • enctype="multipart/form-data": Extremely important! When the form has to let the user upload a photo, PDF or file, adding this attribute is 100% mandatory.

Golden rules

  • Always use method="POST" for sending passwords and sensitive data.
  • Always add enctype="multipart/form-data" to a form with file uploads.

Common mistake

Using the GET method in a form with a password, which makes the user's password visible to everyone in the browser history and URL.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px; background: #f8fafc;">
  <form action="/save" method="POST" enctype="multipart/form-data" style="max-width: 300px; background: white; padding: 15px; border-radius: 8px; border: 1px solid #cbd5e1;">
    <h4 style="margin-top: 0;">Secure Form (POST)</h4>
    <label style="font-size: 12px; font-weight: bold;">Password:</label><br>
    <input type="password" required style="width: 100%; padding: 6px; margin: 4px 0 8px 0; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;"><br>
    <label style="font-size: 12px; font-weight: bold;">File upload:</label><br>
    <input type="file" style="font-size: 12px; margin: 4px 0 10px 0;"><br>
    <button type="submit" style="background: #059669; color: white; border: none; padding: 8px 14px; border-radius: 4px; font-weight: bold; cursor: pointer;">Submit</button>
  </form>
</body>
</html>

Run & edit this in the live editor

Quick quiz

When a file or image has to be uploaded through a form, which enctype must be set on the form?

  1. text/plain
  2. multipart/form-data
  3. application/json
  4. file/upload
Show answer

Answer: multipart/form-data

For file upload, setting enctype='multipart/form-data' is mandatory.

Module 7: Text InputsIntermediate

26. Text, Password, Email and Number Inputs

type='text', 'password', 'email', 'number', 'tel', 'url'

Real-life analogy

Just as when you dial a number on a phone only a 0-9 keypad opens, and when you type an email the keypad with @ opens, the right input type gives the user the right keyboard!

Explanation

The main text-based input types:

  • type="text": For writing a plain name or address.
  • type="password": Shows secret dots (••••) in place of characters.
  • type="email": Automatically checks for @ and a domain name on submit.
  • type="number": Accepts only numbers (0-9). A range can be set with min and max.
  • type="tel": For phone numbers. Opens the phone keypad on a mobile screen.
  • type="url": For website links (checks for https://).

Golden rules

  • Always add <label for="id"> with every input so that the user can also select the box by clicking the label.

Common mistake

Using type='number' for a phone number. You should always use type='tel' for phone numbers.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px;">
  <form style="max-width: 280px; font-size: 13px;">
    <label>Full name:</label>
    <input type="text" placeholder="e.g. Rahul" style="width: 100%; padding: 6px; margin: 3px 0 8px 0; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;">
    
    <label>Email:</label>
    <input type="email" placeholder="rahul@test.com" style="width: 100%; padding: 6px; margin: 3px 0 8px 0; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;">

    <label>Password:</label>
    <input type="password" placeholder="••••••••" style="width: 100%; padding: 6px; margin: 3px 0 8px 0; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;">
  </form>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which input type is used to hide a password and show it as dots (••••)?

  1. type="secret"
  2. type="hidden"
  3. type="password"
  4. type="dots"
Show answer

Answer: type="password"

type='password' hides the characters and shows them as bullets.

Module 7: Choice SelectionIntermediate

27. Radio Buttons vs Checkboxes (Radio vs Checkbox)

Choosing only one option vs choosing many options (the secret of the name attribute)

Real-life analogy

A Radio Button is like an exam Multiple Choice Question where only 1 of the 4 answers is correct. A Checkbox is like pizza toppings where you can pick onion, paneer and corn all together!

Explanation

1. Radio button (type="radio"):

  • Used when you must choose only one from several options (for example Gender: Male/Female).
  • Golden rule: All the radio buttons in a group must have the same name attribute! If the name is different, both will be selected together, which is a big bug.

2. Checkbox (type="checkbox"):

  • The user can tick multiple options (Multiple Choices) at the same time (for example Hobbies: Cricket, Music, Coding).

Golden rules

  • Always keep the same name="gender" in radio buttons.

Common mistake

Writing different names in radio buttons.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px; font-size: 13px;">
  <!-- Radio Button (only 1 can be chosen) -->
  <label style="font-weight: bold;">Select gender (Radio - only one):</label><br>
  <label><input type="radio" name="gender" value="m" checked> Male</label> &nbsp;
  <label><input type="radio" name="gender" value="f"> Female</label><br><br>

  <!-- Checkbox (many can be chosen) -->
  <label style="font-weight: bold;">Hobbies (Checkbox - more than one):</label><br>
  <label><input type="checkbox" name="hobbies" value="cricket" checked> Cricket</label> &nbsp;
  <label><input type="checkbox" name="hobbies" value="music"> Music</label> &nbsp;
  <label><input type="checkbox" name="hobbies" value="coding" checked> Coding</label>
</body>
</html>

Run & edit this in the live editor

Quick quiz

In a group of radio buttons, how do you make sure the user can choose only one option?

  1. By giving all of them the same id
  2. By giving all of them the same name
  3. By giving all of them the same value
  4. By adding an only-one attribute
Show answer

Answer: By giving all of them the same name

When all the radio buttons have the same name, the browser treats them as one group and lets only one be ticked.

Module 7: Dropdown & TextareaIntermediate

28. Dropdowns and Long Messages (<select>, <textarea>, <datalist>)

A dropdown list of states, a long feedback box and auto-search suggestions

Real-life analogy

Just as in online shopping a long list opens downward for choosing your State, select and option do the same thing.

Explanation

Other important form elements:

  • <select> and <option>: For creating a dropdown menu (Drop-down list).
  • <textarea>: For writing a long multi-line message or address. Its size is set with rows and cols.
  • <datalist>: Magical auto-search! Shows suggestions while you type in an input box.

Golden rules

  • The closing tag </textarea> is mandatory for <textarea>.

Common mistake

Writing textarea as a self-closing <textarea />.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px; font-size: 13px;">
  <!-- Dropdown -->
  <label style="font-weight: bold;">Select city (&lt;select&gt;):</label><br>
  <select style="width: 100%; max-width: 260px; padding: 6px; margin: 4px 0 12px 0; border: 1px solid #cbd5e1; border-radius: 4px;">
    <option value="delhi">Delhi</option>
    <option value="mumbai">Mumbai</option>
    <option value="lucknow">Lucknow</option>
  </select><br>

  <!-- Textarea -->
  <label style="font-weight: bold;">Message (&lt;textarea&gt;):</label><br>
  <textarea rows="3" placeholder="Write here..." style="width: 100%; max-width: 260px; padding: 6px; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;"></textarea>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which tag is used to add each option inside a dropdown list?

  1. <item>
  2. <list>
  3. <option>
  4. <choice>
Show answer

Answer: <option>

The <option> tag is used for the options of a dropdown.

Module 7: Modern InputsIntermediate

29. Modern HTML5 Inputs (date, time, color, range, file)

Calendar picker, colour picker, slider bar and file uploader

Real-life analogy

A colour picker is like a paint box, and a range slider is like a fan regulator that can be turned from 1 to 5!

Explanation

Modern interactive input types of HTML5:

type="date": Opens a built-in calendar popup.
type="time": Opens a clock and AM/PM popup.
type="color": Opens a digital colour palette.
type="range": A slider bar (with min, max).
type="file": Uploading a photo/PDF from the computer.

Golden rules

  • Always set min and max on a range input.

Common mistake

Adding a heavy JavaScript plugin for a calendar, when type='date' is already built into HTML5.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px; font-size: 13px;">
  <form>
    <label>Choose a favourite colour:</label><br>
    <input type="color" value="#10b981" style="margin: 4px 0 10px 0;"><br>

    <label>Volume slider (0 to 100):</label><br>
    <input type="range" min="0" max="100" value="70" style="margin: 4px 0 10px 0;"><br>

    <label>Date of birth calendar:</label><br>
    <input type="date" style="padding: 4px; border: 1px solid #cbd5e1; border-radius: 4px;">
  </form>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which input type is used to let a user pick a colour on a website?

  1. type="palette"
  2. type="color"
  3. type="paint"
  4. type="rgb"
Show answer

Answer: type="color"

type='color' opens the browser's built-in colour picker.

Module 7: Form ValidationIntermediate

30. Input Validation Attributes (required, readonly, disabled)

required, placeholder, readonly, disabled, min, max, pattern

Real-life analogy

Just as an exam form has a red star (*) showing that a field is mandatory (required), these attributes do the same thing.

Explanation

The main attributes that control an input field:

  • required: The form will not submit without it.
  • placeholder: Light hint text shown inside the box.
  • readonly: The user can only read it and cannot change it (but the data will go to the server).
  • disabled: The box becomes completely inactive (grey) (the data does not go to the server).
  • min and max: The minimum and maximum limits for a number or date.

Golden rules

  • Remember the difference between readonly and disabled: the data of readonly is submitted, that of disabled is not.

Common mistake

Treating placeholder as a substitute for label. A label is mandatory for screen readers.

Example code

<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px;">
  <form style="max-width: 260px;">
    <label style="font-size: 12px; font-weight: bold;">Name (required *):</label>
    <input type="text" placeholder="Enter name..." required 
           style="width: 100%; padding: 6px; margin: 3px 0 8px 0; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;">

    <label style="font-size: 12px; font-weight: bold;">Roll number (Readonly):</label>
    <input type="text" value="10582" readonly 
           style="width: 100%; padding: 6px; margin: 3px 0 10px 0; background: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 4px; box-sizing: border-box;">

    <button type="submit" style="background: #0284c7; color: white; border: none; padding: 8px 14px; border-radius: 4px; cursor: pointer;">Submit</button>
  </form>
</body>
</html>

Run & edit this in the live editor

Quick quiz

Which attribute makes an input box inactive (grey) on screen so that its data is not submitted?

  1. readonly
  2. disabled
  3. hidden
  4. locked
Show answer

Answer: disabled

The disabled attribute turns the input off, and its value is not sent in the form submission.

← Course overview · Run the code in the live editor