The <div> (division) element is a block-level container used to group related content together.
It has no visual appearance by default but is one of the most commonly used HTML elements for creating layouts and organizing content.
<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
└── FooterThe <span> element is an inline container used to group or style a small portion of text.
Unlike <div>, a <span> does not start on a new line.
<p>
Learn <span>HTML</span> first.
</p><p>
Learn <span style="color:red;">HTML</span> first.
</p>In this example, only the word HTML is styled.
| Feature | div | span |
|---|---|---|
| Type | Block Element | Inline Element |
| Starts on New Line | Yes | No |
| Used For | Large Sections | Small Parts of Text |
| Layout Creation | Yes | No |
<div>Block Element</div>
<span>Inline</span>
<span>Inline</span>Block Element
Inline Inline
The id attribute uniquely identifies an HTML 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");Good Examples
id="header"
id="main-content"
id="userProfile"Avoid
id="header"
id="main-content"
id="userProfile"Only one element should use a specific ID.
Correct
<h1 id="title">Title</h1>Incorrect
<h1 id="title">One</h1>
<h2 id="title">Two</h2>The class attribute is used to group multiple elements together.
Unlike IDs, classes can be reused throughout the page.
<p class="text">
Paragraph One
</p>
<p class="text">
Paragraph Two
</p>.text {
color: green;
}Both paragraphs become green.
<button
class="btn primary"
>
Save
</button>This button belongs to both:
| Feature | id | class |
|---|---|---|
| Unique | Yes | No |
| Reusable | No | Yes |
| CSS Selector | # | . |
| JavaScript Usage | Single Element | Multiple Elements |
#header {
background: black;
}
.card {
border: 1px solid gray;
}The style attribute applies CSS directly to an 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 allow developers to store custom information directly on HTML elements.
All data attributes begin with:
data-
<div data-user="samir">
</div><button
data-product-id="101"
>
Buy Now
</button>
Stored Data:
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><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 |
The following example combines everything learned in this lesson:
<div
id="profile"
class="card"
data-user-id="101"
>
<h2>Samir</h2>
<p>
Frontend Developer at
<span class="company">
Kharaayo
</span>
</p>
</div>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.Mastering these concepts will make it much easier to build layouts, apply styling, and create interactive web applications.