For working professionals
For fresh graduates
More
Learn HTML: A Comprehensive Tu…
1. HTML Tutorial
2. HTML Basics
3. HTML Syntax
4. HTML Elements
5. HTML Attributes
6. HTML Comments
7. HTML Semantic
8. HTML Form Elements
9. HTML Head
10. HTML Title
11. HTML Styles
12. HTML Paragraphs
13. HTML Symbols
14. HTML Emojis
15. HTML Formatting
16. HTML Entities
17. HTML Audio
18. HTML Images
19. HTML Lists
20. HTML Links
21. SVG in HTML
22. HTML Forms
23. HTML Video
24. HTML Canvas
25. Adjacency Lists
26. HTML Input Types
27. HTML Tables
28. HTML Table Border
29. Cell Spacing and Cell Padding
30. HTML Semantic Elements
31. HTML Layout
32. html blocks and inline
33. HTML Div
Now Reading
34. Difference Between HTML and CSS
35. Image Map in HTML
36. HTML Drag and Drop
37. HTML Iframes
38. Divide and Conquer Algorithm
39. Difference Between HTML and XHTML
40. HTML Code
41. HTML Colors
42. HTML CSS
43. HTML Editors
44. HTML Examples
45. Class in HTML
46. HTML Exercises
47. HTML ID
48. Understanding HTML Encoding: A Comprehensive Guide
49. HTML Table Style
50. HTML Script
51. Introduction to HTML
HTML div is a very useful tool for grouping different HTML elements together. This helps in maintaining the structure of the web page. The term "div" stands for "division" and is named so because it separates the content of a webpage into various parts or containers. These containers can then be styled, formatted, or modified with CSS (Cascading Style Sheets) or JavaScript.
I use HTML div in almost all my projects. In this tutorial, let me show how you can too.
HTML div tag helps massively in HTML programming. It is a very widely used element in the modern day. Here in this section of the tutorial, let me discuss some of the advantages of HTML div with you.
<div> is mostly used for setting up a webpage's layout structure. We use <div> components for dividing your webpage into sections like header, navigation, content, sidebar, and footer. To add style and organization, surround each section in a <div>.
Let me share an example to explain better.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> HTML Div Structure Layout Usecase </title>
</head>
<body>
<div id="header">
<h2> Header Content here </h2>
<!-- Header content -->
</div>
<div id="nav">
<h2> Navbar Content here </h2>
<!-- Navigation menu -->
</div>
<div id="content">
<h2> Main Content here </h2>
<!-- Main content -->
</div>
<div id="sidebar">
<h2> Sidebar Content here </h2>
<!-- Sidebar content -->
</div>
<div id="footer">
<h2> Footer Content here </h2>
<!-- Footer content -->
</div>
</body>
</html>
We can use the div id in HTML attribute to target specific div in CSS and JavaScript.
<div> elements are commonly used to group relevant information together. You can use a <div> to organize paragraphs, images, or form components.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> HTML Div Grouping and Styling Usecase </title>
</head>
<body>
<div class="content-group">
<p> This is just to demonstrate the usecase.</p>
<img src="image.jpg" alt="Example Image">
<form>
<!-- Form elements -->
</form>
</div>
</body>
</html>
You can use div class HTML to assign a class to a different div in HTML. This helps target the div in the class when calling them via CSS or JavaScript.
Responsive web design relies heavily on <div> elements for adaptable layouts. Using <div> and CSS rules like display: flex or grid, you may develop responsive designs that adjust to different screen sizes.
Let me show with the help of an example.
Modal windows and popups are often created with <div> elements. The modal content is placed within a <div>, which is hidden by default but visible when activated. Let me show with the help of a div tag in HTML example.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> HTML Div Modal and POPUP Usecase </title>
</head>
<body>
<div id="modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2> Example </h2>
<p>Modal and popup content goes here.</p>
</div>
</div>
</body>
</html>
There are many ways you might want to position a div container HTML. To alter the div layout HTML, we have a lot of alignment div HTML attributes that we can use to position an HTML div exactly the way we want to. Let us get into it in this section of the tutorial.
There are three types of alignment when it comes to positioning a div in HTML. Let us discuss them one by one.
Let me explain with the help of a simple div code HTML.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Horizontal Div Alignment Example </title>
<style>
.container {
display: flex;
justify-content: space-around;
align-items: center;
height: 100vh;
background-color: lightgreen;
}
.box {
width: 100px;
height: 100px;
background-color: #333;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<h2> Horizontal Div Alignment Example </h2>
<div class="container">
<div class="box">Left Div</div>
<div class="box center-align">Center Div</div>
<div class="box right-align">Right Div</div>
</div>
</body>
</html>
In the above example,
You can align a <div> to the top or bottom of its container with Flexbox or CSS Grid. Let me show you with the help of an example.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vertical Div Alignment Example</title>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
}
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 300px; /* Adjust height as needed */
border: 2px solid #333;
padding: 10px;
background-color: lightgreen;
}
.box {
width: 100px;
height: 100px;
background-color: #333;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
font-weight: bold;
}
.top {
align-self: flex-start;
}
.center {
align-self: center;
}
.bottom {
align-self: flex-end;
}
</style>
</head>
<body>
<div class="container">
<div class="box top"> Top Div </div>
<div class="box center"> Center Div </div>
<div class="box bottom"> Bottom Div </div>
</div>
</body>
</html>
In the above example,
People tend to confuse between div and span in HTML. div span HTML difference lies more or less in their use cases. Let me discuss their differences to give you a better idea.
Point of difference | div | span |
Behaviour of display | The <div> tag is a block-level element, starting on a new line and taking up the full width of its parent container. Block-level elements generate a "block" of content. | The <span> tag is an inline-level element, meaning it does not start a new line and only takes up the necessary width. Inline-level elements are commonly used inside block-level elements to design or group certain sections of content. |
Why are they used | The <div> tag is commonly used to create sections or divisions on a webpage. It is frequently used for layout, grouping groupings of items, and applying styles or scripts to several elements at once. | The <span> tag is commonly used to style or apply inline-level formatting to specific text or content within a block-level element. It is helpful for applying styles to specific words, phrases, or tiny sections of text. |
Let me spare an example to explain better.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div between Span Example</title>
<style>
.container {
border: 2px solid #333;
padding: 10px;
margin-bottom: 20px;
}
.box {
width: 80px;
height: 80px;
background-color: lightgreen;
border: 1px solid #333;
margin: 5px;
}
.highlight {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box A</div>
<div class="box">Box B</div>
<div class="box">Box C</div>
</div>
<div class="container">
This is a <span class="highlight ">span</span> element inside a paragraph.
</div>
</body>
</html>
In the above example,
HTML div plays an important role in managing HTML elements. We have discussed several div examples HTML in this tutorial explaining the several use cases of HTML div in web development. Now you can apply the concept of HTML div in your future projects also.
To learn more advanced concepts of HTML and web development, I would recommend doing a certified course from a reputed platform. One such platform that I recommend is upGrad. Their courses are curated by some of the best professors in this field. The courses are also in collaboration with some of the best universities around the world.
The HTML <div> element is a block-level container that organizes and styles content. It has no intrinsic semantic value, however it is commonly used for layout and applying CSS styles.
HTML <div> element creates divisions or sections inside a web page. It is mostly used for layout, content structure, and CSS styling. This helps to organize text, improves readability, and allows for more flexible design possibilities.
The <div> tag is used in HTML to generate a <div> element. You can provide properties such as id or class for styling and JavaScript interaction.
Using <div> elements in HTML creates divisions or sections inside a webpage. They assist with layout structure, content organization, and CSS styling for design and presentation.
To show HTML material in a <div>, you can utilize JavaScript's innerHTML property or simply insert HTML code within the <div> tags.
HTML div IDs are unique identifiers assigned to <div> elements using the id attribute. You can target and style a specific <div> with CSS or change it with JavaScript.
To conceal an <div> in HTML, set its style attribute to display: none, or use JavaScript to adjust its style (.display) the display property to 'none'.
Yes, you can insert a <div> element inside a table cell (<td> or <th>) in HTML. You can use <div> components to structure and style content in a table cell.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.