<div> TagBy the end of this lesson students will be able to:
<div> element.<div> element.class and id attributes to <div> elements.<div> to control appearance.<div> containers.<div>?The <div> (short for “division”) is a generic block‑level container. It does not convey any semantic meaning, but it groups related content so that you can style or manipulate it as a unit.
<div>
…content…
</div>
Because <div> is a block‑level element, it starts on a new line and stretches to the full width of its parent container.
class and id AttributesAttributes allow you to target a <div> with CSS or JavaScript.
<div class="sidebar">
…sidebar content…
</div>
<div id="mainContent">
…main article…
</div>
For quick demonstrations you can add a style attribute directly inside the tag.
<div style="background:#f0f0f0; padding:15px;">
This box has a light‑grey background and padding.
</div>
<div> ElementsNesting creates a hierarchy, making it easier to control layout.
<div class="page">
<div class="header">…</div>
<div class="content">
<div class="article">…</div>
<div class="sidebar">…</div>
</div>
<div class="footer">…</div>
</div>
<div> tag – results in unexpected layout.id on more than one element – breaks uniqueness.class names for meaning – consider semantic tags (<header>, <nav>, etc.) where appropriate.<!DOCTYPE html>
<html>
<head>
<title>Two‑Column Example</title>
<style>
.container { display:flex; }
.main { flex:2; background:#e8f5e9; padding:10px; }
.side { flex:1; background:#e3f2fd; padding:10px; }
</style>
</head>
<body>
<div class="container">
<div class="main">
<h2>Main Content</h2>
<p>…</p>
</div>
<div class="side">
<h2>Sidebar</h2>
<p>…</p>
</div>
</div>
</body>
</html>
<div> structure in the two‑column layout.| Attribute | Purpose | Example |
|---|---|---|
class |
Group multiple elements for shared styling | <div class="menu">…</div> |
id |
Unique identifier for a single element | <div id="header">…</div> |
style |
Apply inline CSS declarations | <div style="color:red;">…</div> |
<div> that uses a class to set a background colour and padding.<div> elements and assign each a unique id.style that changes the font size of the header.<div> for generic block containers.<div>.class for reusable styles; id for a single element.style is useful for demonstration, but external CSS is preferred for larger projects.<div> structures help organise complex layouts.