Div in HTML
? <div> in HTML
The <div> element is a block-level container used to group together other HTML elements. It doesn’t have any semantic meaning by itself but is essential for layout and styling.
What is <div> used for?
Group related elements together
Apply CSS styles or JavaScript to a section
Create page structure or layout (like columns, sections)
Basic Syntax
<div> <h2>Section Title</h2> <p>This is some content inside a div.</p></div>Common Usage Example
<div class="container"> <header> <h1>My Website</h1> </header> <div class="content"> <p>Welcome to my site!</p> </div> <footer> <p>© 2025 My Website</p> </footer></div>Styling a <div> with CSS
.container { width: 80%; margin: auto; background-color: #f0f0f0; padding: 20px;}Why use <div>?
It’s a flexible, generic container
Helps organize HTML for easier CSS styling and scripting
Can nest other elements inside for complex layouts
Want examples on layout with divs, or how to replace <div> with more semantic tags like <section> or <article>?