HTML div tag

Html <div> element is a block level element which contains the content or other elements. This element is nothing on its own. It is useful when used with class, title attributes.

Example

<div>
 <p>This block has content and may have other elements.It can be designed using css poperties.
 Height and width can also be defined for it.</p>
</div>
<div>
 <p>This is the secong block of content. This block can also be designed.</p>
</div>
Try </>

Specific uses

Actually, it is used to style a group of its elements using CSS.The following example styles all paragarphs instead of styling the paragarphs separately.

Example


<div contenteditable>
 <p>This block has content and may have other elements.The content inside div element is editable.</p>
</div>
Try </>

It may also be used to set properties for more than one elements at a time. The following example sets the attribute contetneditable instead of individual paragraphs.

Example

<!DOCTYPE html>
<html>
<style>
div{
	width:50%;
	height:50%;
	background-color:lightblue;
}

</style>
<body>

<div>
 <p>This block has content and may have other elements.The content is being styled by the CSS properties.</p>
</div>
	
</body>
<html>
Try</>

div should only be used when no other element is suitable for the content. For example, we use <nav> element for navigation bar. But div is a generic type element.

Tag omission

Both opening and closing tags are necessary for the div element.

Global Attributes

All global attributes are applicable on div element.



Was this article helpful?

 

Email:

Message:

Get the newsletter