In HTML, <div>
is a versatile and commonly used element that stands for "division" or "divider." It is a block-level container that is used to group other HTML elements and apply styles or structure to them. Here are some examples of how to use <div>
in HTML:
<div>
Structurehtml<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div Example</title>
<style>
/* CSS for styling the div */
.container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
</style>
</head>
<body>
<!-- Basic usage of <div> -->
<div class="container">
<h1>This is a heading inside a div</h1>
<p>This is a paragraph inside a div.</p>
</div>
</body>
</html>
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Layout Example</title>
<style>
/* CSS for layout */
.header {
background-color: #f2f2f2;
padding: 20px;
text-align: center;
}
.main-content {
margin: 20px;
}
.footer {
background-color: #f2f2f2;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<!-- Layout structure using <div> -->
<div class="header">
<h1>Header</h1>
</div>
<div class="main-content">
<p>Main content goes here.</p>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nested Divs Example</title>
<style>
/* CSS for styling nested divs */
.outer-container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
.inner-container {
background-color: #f2f2f2;
padding: 10px;
margin: 10px;
}
</style>
</head>
<body>
<!-- Nested <div> example -->
<div class="outer-container">
<h2>Outer Container</h2>
<div class="inner-container">
<p>Inner container content goes here.</p>
</div>
</div>
</body>
</html>
In these examples, the <div>
element is used to group content, create layout structures, and apply styles. CSS is used to style the <div>
elements for better visual presentation.