HTML | Div Tag - GeeksforGeeks

The HTML <div> tag defines sections in HTML documents, serving as containers styled with CSS or controlled with JavaScript. It’s easily customized using class or id attributes and can contain various content.

Note: Browsers add line breaks before and after <div> elements by default.

Div tag Usage

  • The div tag is the block-level tag.
  • It is used for applying styles and layout structure <div> and allows us to manipulate and position content through CSS.
  • It also divides content into logical sections, aiding in the readability and maintainability of the code.
  • As we know, a Div tag is a block-level tag containing the entire width. Hence, every div tag will start from a new line and not the same line.
html <!DOCTYPE html> <html> <head> <style> div{ color:white; background-color:009900; margin:2px; font-size:25px; } </style> </head> <body> <div> div tag </div> <div> div tag </div> <div> div tag </div> <div> div tag </div> </body> </html>

We can use CSS in any of the divisions (<div> tag) using the following methods:

Table of Content

  • Using class
  • Inline CSS
  • Difference Between div tag and span tag

1. Using class

We can use class on that particular div and apply CSS either inside a <style> tag or linking an external CSS file.

  • In case of internal CSS: We need to define Class in the <head> section of HTML within <style> element.
  • In case of External CSS: We need to create a separate .css file and include it in HTML code inside <head> section using <link> element.
html <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="color.css"> </head> <body> <center> <div class="color"> <caption> <h3>GEEKSFORGEEKS</h3> </caption> <h3> Inline CSS is not USED in THIS method. </h3> </div> </center> </body> </html> CSS .color{ height:400px; width:600px; border:1pxsolid; background-color:009900; }

2. Inline CSS

We can directly use CSS in div also. This method does not require class. Div in HTML coding is used as a container tag also because it is the one that can contain all other tags.

html <!DOCTYPE html> <html> <body> <center> <div style="height:300px; width:500px; color:white; border:1px solid; background-color: black;"> <caption> <h3>GEEKSFORGEEKS</h3> </caption> <h3> Inline CSS is USED in THIS method. In this div no class is used. </h3> </div> </center> </body> </html>

Difference Between div tag and span tag

PropertiesDiv TagSpan Tag
Elements TypesBlock-LevelInline
Space/WidthContain Whole Width AvailableTakes only required Width
ExamplesHeadings, Paragraph, formAttribute, image
UsesWeb-layoutcontainer for some text
AttributesNot required,with common css, classNot required,with common css, class

The span tag does not create a line break similar to a div tag, but rather allows the user to separate things from other elements around them on a page within the same line. avoiding of line break, results only that selected text to change, keeping all the other elements around them same.

html <!DOCTYPE html> <html> <head> <style> div{ color:white; background-color:009900; margin:2px; font-size:25px; } span{ color:black; background-color:gray; margin:5px; font-size:25px; } </style> </head> <body> <div> div tag </div> <div> div tag </div> <div> div tag </div> <div> div tag </div> <span>span-tag</span> <span>span-tag</span> <span>span-tag</span> <span>span-tag</span> </body> </html>

HTML Div Tag – FAQs

What is the purpose of the <div> tag in HTML?

The <div> tag is a block-level container element used to group and organize content within a web page. It does not have any inherent styling or semantic meaning but is often used for layout purposes when combined with CSS.

How is the <div> tag different from the <span> tag?

The <div> tag is a block-level element, meaning it takes up the full width available and starts on a new line. The <span> tag, in contrast, is an inline element, meaning it only takes up as much space as its content and does not force a line break.

Can I nest <div> tags inside each other?

Yes, you can nest <div> tags inside each other. This is common practice in web development for creating more complex layouts or sections within a page.

What is the default display property of a <div>?

The default display property of a <div> is block. This means it takes up the full width available and causes a line break before and after it.

How can I use a <div> to create a responsive layout?

You can use CSS techniques like Flexbox or Grid inside <div> containers to create responsive layouts that adapt to different screen sizes.

A

AkashRawat Follow Improve Previous Article HTML dir Tag Next Article HTML dl Tag

Từ khóa » Html5 Tag Div