HTML provides elements and attributes that help organize, group, and identify content on a webpage. These are essential for styling with CSS and adding functionality with JavaScript.
<div>The <div> (division) tag is a block-level container used to group related elements.
It has no visual appearance by default.
<div>
<h1>Frontend Development</h1>
<p>Learn HTML, CSS, and JavaScript.</p>
</div>
DIV
├── Heading
└── Paragraph
<div>
<h2>About Us</h2>
<p>Information about the company.</p>
</div>
<div>
<h2>Services</h2>
<p>Our services list.</p>
</div>
<div class="navbar">
Navigation
</div>
<div class="hero">
Hero Section
</div>
<div class="footer">
Footer
</div>
Page
├── Navbar
├── Hero
├── Content
└── Footer
<span>The <span> tag is an inline container used to group or style a small portion of text.
Unlike <div>, it does not start on a new line.
<p>
Learn <span>HTML</span> first.
</p>
Learn HTML first.
<p>
Learn <span style="color:red;">HTML</span> first.
</p>
Learn HTML first.
(HTML appears red.)
div and span| Feature | div |
span |
|---|---|---|
| Type | Block Element | Inline Element |
| New Line | Yes | No |
| Used For | Large Sections | Small Parts of Text |
| Layout | Yes | No |
<div>Block Element</div>
<span>Inline</span>
<span>Inline</span>
Block Element
Inline Inline
idThe id attribute uniquely identifies an element.
Every ID should be unique within a webpage.
<tag id="unique-name">
<h1 id="main-heading">
Welcome
</h1>
<h1 id="title">
Frontend Development
</h1>
#title {
color: blue;
}
<h1 id="title">
Hello
</h1>
const heading =
document.getElementById("title");
id="header"
id="main-content"
id="userProfile"
id="123header"
id="main content"
Only one element should use a particular ID.
<h1 id="title">Title</h1>
<h1 id="title">One</h1>
<h2 id="title">Two</h2>
classThe class attribute groups multiple elements together.
Unlike IDs, classes can be reused throughout a webpage.
<p class="text">
Paragraph One
</p>
<p class="text">
Paragraph Two
</p>
.text {
color: green;
}
Both paragraphs become green.
An element can belong to multiple classes at the same time.
<button
class="btn primary"
>
Save
</button>
This button belongs to:
btnprimary| Feature | id |
class |
|---|---|---|
| Unique | Yes | No |
| Reusable | No | Yes |
| CSS Selector | # |
. |
| JavaScript | Single Element | Multiple Elements |
#header {
background: black;
}
.card {
border: 1px solid gray;
}
styleThe style attribute applies CSS directly to an HTML element.
This technique is called Inline CSS.
<h1 style="color: blue;">
Hello World
</h1>
<p
style="
color:red;
font-size:20px;
"
>
Welcome
</p>
<h1 style="color:red;">
Title
</h1>
<h2 style="color:red;">
Subtitle
</h2>
<h1 class="red-text">
Title
</h1>
<h2 class="red-text">
Subtitle
</h2>
.red-text {
color: red;
}
Data attributes are custom attributes used to store extra information on HTML elements.
All data attributes begin with:
data-
<div data-user="samir">
</div>
<button
data-product-id="101"
>
Buy Now
</button>
product-id = 101
<div
data-user="samir"
data-role="admin"
data-country="nepal"
>
</div>
<button
data-id="101"
>
Buy
</button>
const button =
document.querySelector("button");
console.log(
button.dataset.id
);
101
<button
data-product-id="15"
>
Add to Cart
</button>
<div
data-user-id="25"
>
</div>
<body
data-theme="dark"
>
</body>
The following example combines everything learned in this lesson.
<div
id="hero"
class="section"
data-page="home"
>
<h1>
Welcome
</h1>
<p>
Learn
<span class="highlight">
HTML
</span>
today.
</p>
</div>
| Feature | Value |
|---|---|
| Element | div |
| ID | hero |
| Class | section |
| Data Attribute | page="home" |
| Inline Element | span |
| Item | Purpose |
|---|---|
div |
Block-level container |
span |
Inline container |
id |
Unique identifier |
class |
Reusable group identifier |
style |
Inline CSS styling |
data-* |
Store custom data |
<div
id="profile"
class="card"
data-user-id="101"
>
<h2>Samir</h2>
<p>
Frontend Developer at
<span class="company">
Kharaayo
</span>
</p>
</div>
In modern web development:
div structures sections of a webpage.span styles small pieces of content.id identifies unique elements.class groups reusable elements.style applies quick inline CSS.data-* stores custom information for JavaScript.