Introduction
The <div>
element is used as a container for grouping and styling content. The <div>
element is essential for creating layouts, applying styles, and structuring your web pages.
What is a <div> Element?
The <div>
element (short for “division”) is a block-level container used to group other HTML elements together. It has no semantic meaning by itself, but it is commonly used to apply CSS styles and JavaScript functions to a specific section of content.
Syntax
<div>
Content goes here.
</div>
Basic Example
The following example demonstrates a basic use of the <div>
element to group a heading and a paragraph together.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic Div Example</title>
</head>
<body>
<div>
<h1>Welcome to My Website</h1>
<p>This is a paragraph inside a div element.</p>
</div>
</body>
</html>
Styling with CSS
The <div>
element is commonly used with CSS to apply styles to a section of a web page. By assigning a class or an ID to a <div>
, you can target it with CSS rules.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Div Example</title>
<style>
.container {
background-color: #f4f4f4;
border: 1px solid #ddd;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Styled Div</h1>
<p>This div has a background color, border, padding, and margin.</p>
</div>
</body>
</html>
Using Divs for Layout
The <div>
element is often used to create multi-column layouts and to organize different sections of a web page.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div Layout Example</title>
<style>
.header, .footer {
background-color: #f4f4f4;
padding: 10px;
text-align: center;
}
.row {
display: flex;
}
.column {
flex: 50%;
padding: 10px;
}
</style>
</head>
<body>
<div class="header">
<h1>My Website</h1>
</div>
<div class="row">
<div class="column" style="background-color:#aaa;">
<h2>Column 1</h2>
<p>This is column 1.</p>
</div>
<div class="column" style="background-color:#bbb;">
<h2>Column 2</h2>
<p>This is column 2.</p>
</div>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
Nesting Divs
You can nest multiple <div>
elements within each other to create complex layouts and structures.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nesting Divs Example</title>
<style>
.outer {
background-color: #f4f4f4;
padding: 20px;
}
.inner {
background-color: #ddd;
padding: 10px;
margin: 10px;
}
</style>
</head>
<body>
<div class="outer">
<h1>Outer Div</h1>
<div class="inner">
<h2>Inner Div</h2>
<p>This is a nested div.</p>
</div>
</div>
</body>
</html>
Real-World Use Case: Creating a Page Layout
A common use case for the <div>
element is creating a complete page layout with a header, navigation bar, content area, and footer.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Layout with Divs</title>
<style>
.header, .footer {
background-color: #333;
color: white;
text-align: center;
padding: 10px 0;
}
.nav {
background-color: #f4f4f4;
padding: 10px;
}
.nav ul {
list-style-type: none;
padding: 0;
}
.nav ul li {
display: inline;
margin-right: 10px;
}
.content {
padding: 20px;
margin: 20px;
background-color: #e2e2e2;
}
</style>
</head>
<body>
<div class="header">
<h1>My Website</h1>
</div>
<div class="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<div class="content">
<h2>Welcome to My Website</h2>
<p>This is the main content area.</p>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
Conclusion
The HTML <div>
element is a fundamental building block for creating layouts and organizing content on web pages. By using <div>
elements along with CSS, you can style and structure your web pages effectively. Understanding how to use <div>
elements is essential for web development, enabling you to create complex and responsive designs.