<div>
Element: <div>
Element?✔ The <div>
(short for division) is a block-level container used to group elements together for styling and layout purposes.
✔ It does not add any visual changes by itself but helps in organizing content and applying CSS styles.
✔ It is often used in web layouts, UI designs, and JavaScript-based dynamic content manipulation.
<div>
<div>
This is a container.
</div>
✔ The <div>
acts as a wrapper around content.
✔ By default, it takes up the full width of the page.
<div>
?Purpose | Example |
---|---|
Grouping Content | Wraps related content together |
Styling Sections | Applies CSS styles to grouped elements |
Creating Layouts | Helps structure a webpage using CSS |
JavaScript Manipulation | Used for dynamic updates and interactivity |
<div>
<div>
<h2>About Us</h2>
<p>We provide high-quality web development services.</p>
</div>
✔ The heading (<h2>
) and paragraph (<p>
) are grouped inside a <div>
.
✔ This allows styling them together as a single unit.
<div>
with CSSYou can use CSS to customize the appearance of a <div>
.
<div style="background-color: lightblue; padding: 15px; border-radius: 8px;">
This is a styled div.
</div>
✔ Background color (lightblue
)
✔ Padding (15px
) to add spacing
✔ Rounded corners (border-radius: 8px
)
<div>
The <div>
element is widely used to create webpage structures.
<div style="width: 100%; background-color: gray; padding: 10px; text-align: center;">
<h2>Website Header</h2>
</div>
<div style="display: flex;">
<div style="width: 30%; background-color: lightgray; padding: 10px;">
Sidebar
</div>
<div style="width: 70%; background-color: white; padding: 10px;">
Main Content Area
</div>
</div>
<div style="width: 100%; background-color: gray; padding: 10px; text-align: center;">
<p>Footer Section</p>
</div>
✔ Header (<div>
) at the top.
✔ Sidebar (<div>
) on the left, Main Content (<div>
) on the right.
✔ Footer (<div>
) at the bottom.
<div>
ElementsYou can place <div>
inside another <div>
for complex layouts.
<div style="border: 2px solid black; padding: 15px;">
<h2>Outer Div</h2>
<div style="background-color: yellow; padding: 10px;">
Inner Div
</div>
</div>
✔ The inner <div>
is placed inside the outer <div>
for structured design.
<div>
Behavior with CSS:You can modify how <div>
behaves using CSS properties.
✔ Convert <div>
to Inline Block:
div {
display: inline-block;
width: auto;
}
✔ Make <div>
Hidden Until Needed:
div {
display: none;
}
✔ Center a <div>
Horizontally:
div {
width: 50%;
margin: auto;
}
<div>:
✅ Use <div>
only when necessary (avoid "div soup").
✅ Use semantic elements (<header>
, <section>
, <footer>
) instead of <div>
.
✅ Always add CSS for styling and layout adjustments.
✅ Use nested <div>
properly for structured layouts.
✅ Optimize <div>
usage for better performance and readability.
@asadmukhtar