Nearby lessons
8 of 24π¦ HTML <div> Element
π What is the <div> Element?
The <div> element is a generic block container used to group HTML elements for styling or scripting.
The <div> element is like an empty box. It groups other HTML elements together so you can style or manage them with CSS or JavaScript. It's super flexible!
π <div> Element Example
This example shows three <div> blocks aligned left, center, and right using CSS:
Code Example
PREVIEW READY
π¦ Common Uses of <div>
- π Create layout sections (header, footer, sidebar)
- π¨ Group elements to apply styles
- π§© Organize content for JavaScript control
- π§± Build grid or flexbox layouts
- π± Make content responsive
β Best Practices
- π‘ Use semantic elements like
<section>or<article>when they fit - π« Avoid using too many nested
<div>s (called div soup!) - π Give your
<div>s clear and meaningful class names - π§Ό Prefer CSS classes over inline styles
- βΏ Ensure accessibility when using
<div>for interactive parts - π Use ARIA roles if you're using
<div>for non-semantic tasks
π <div> vs Semantic Elements
| Attribute | Description |
|---|---|
<header> | π Top section like page or article headers |
<footer> | π Bottom section like footnotes or copyright |
<section> | π Group of related content or topic |
<article> | π° Independent content like blogs or news |
<nav> | π§ Navigation links like menus or toolbars |
π§ Test Your Knowledge
3 QuestionsProgress: 0 / 3
Keep Going!HTML Block vs Inline