What is the Difference Between <section> and <div> Elements
Have you ever wondered what is the difference between the <section> and <div> elements? If yes, then this snippet is for you!
The <section> tag creates independent sections within a webpage having logically connected content. And the <div> tag is an empty container specifying a division or a section.
In this snippet, we’ll give more details about the <section> and <div> elements and highlight the differences between them.
The <section> Element
According to the W3C specification, the <section> tag means that the content inside this element is grouped. In other words, the content relates to a single theme. It must be an entry in the outline of the page.
Examples of sections can be chapters, several tabbed pages in a tabbed dialog box, or numbered sections of a thesis. E.g., the home page of a website can be divided into many different sections (introduction, news, contact information, etc.). You can use the <article> element instead of <section> if the grouping of the element’s contents is reasonable.
Each section is specified by including headings (h1-h6 element) as child elements of the <section> tag.
Let’s see an example.
Example of using the <section> element:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<h1>W3Docs</h1>
<section>
<h2>W3Docs Sections</h2>
<ul>
<li>Books</li>
<li>Quizzes</li>
<li>Snippets</li>
<li>Tools</li>
<li>String Functions</li>
</ul>
</section>
<section>
<h3>Books</h3>
<p>Learn HTML</p>
<p>Learn CSS</p>
<p>Learn Git</p>
<p>Learn Javascript</p>
<p>Learn PHP</p>
</section>
</body>
</html>
The <div> Element
The <div> element only represents its child elements and doesn’t have a special meaning. It can be used with the lang, title, and class attributes to add semantics that is common to a group of consecutive elements.
This element can also be used in a <dl> tag and wrap groups of <dt> and <dd> elements.
However, a <div> element is quite useful in other situations.
Example of using the <div> tag:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
background-color: #87f5b3
}
</style>
</head>
<body>
<h1>Example</h1>
<div>
<h2>A heading in a <div> tag.</h2>
<p>Some text in a <div> tag.</p>
</div>
<p>Here is some other text in a <p> tag.</p>
</body>
</html>