×

Div Tag in HTML

In HTML, ‘div’ is a tag that specifies division or section in HTML document. But before going in detail of it, let’s first understand about tags in HTML. The tag in HTML can be imagined as a keyword which basically defines how our browser(web-browser) will interpret, format and display the content. The tags act as a line of difference between our HTML content and non-HTML content, this helps the browser to distinguish both efficiently.

HTML tags can be classified into 3 main parts:

  • Opening tag,
  • Content and
  • Closing tag.
Eg:  <tag> content </tag>

Note: There are some tags in HTML which do not require the closing tag, i.e., they are referred to as unclosed tags.

Semantic Tags vs Non-semantic tags in HTML

A semantic tag in HTML is a tag whose name clearly describes its purpose and its content.

Eg:  <h1> —> Used to show important text (generally the heading of the document)

<h6> —> Used to show less important text

<Nav> —>  Used to show navigation information of webpage

<nav>
  <a href="/html/">HTML</a> |
  <a href="/css/">CSS</a> |
  <a href="/js/">JavaScript</a> |




</nav>

Non-semantic tags in HTML

The non-semantic tags can be referred to as the tags in HTML which tells nothing about their content.

Eg:  <div> –> creates a generic container that groups other elements.

<span> –> creates an inline text fragment.

<div> Tag in HTML

The div tag in HTML is a block level element which is used to create a generic container which is generally not visible and is used to group other elements.

When a div tag is declared, all the tags inside it are assigned as a whole separate block. This means if we declare a paragraph (<p> ‘ ‘ ‘ </p>) tag inside a div tag. The paragraph will have a whole block assigned to it.

Eg:

<!DOCTYPE html>
<html>
  <head>
    <title>HTML div Tag</title>
  </head>




  <body>
    <div id="contentinfo">
      <p>Welcome to our website.</p>
      <p>Checkout our huge collection of technical content</p>
<p> Enjoy! your session</p>




    </div>
  </body>
</html>

Output:

Welcome to our website.
Checkout our huge collection of technical content
Enjoy! your session

Code 1: Example of div tag in HTML

<!DOCTYPE html>
<html>
  <head>
    <title>HTML div Tag</title>
  </head>
  <style>
    .contentinfo {
      border: 6px outset maroon;
      background-color: rgb(142, 224, 252);
      text-align: center;
    }
    </style>
    </head>
  <body>
      <h3>This text is outside the div tag</h3>
    <div class="contentinfo">
      <p>Welcome to our website.</p>
      <p>Checkout our huge collection of technical content</p>
      <p>Enjoy! your session</p>
    </div>
  </body>
</html>

Output:

Div Tag in HTML

<div> vs <span> | Difference between div and span tags

<div> <span>
It is a block level element It is an inline element
It creates a generic container that groups other elements. It creates an inline text fragment.
Whole block is assigned to each individual entity inside this tag.Only required space is assigned.
It adds a line break after its closing tag. It doesn’t add a line break after its closing tag.
Eg:
<p>This is first paragraph</p>
<p>This is second paragraph</p>
<p>This is third paragraph</p>
Eg:
<em> This is em tag </em> <em>em is used to </em>  <em> emphasize text</em>   
Output:
This is first paragraph
This is second paragraph
This is third paragraph 
Output:
This is em tag em is used to emphasize text

Styling the content of <div>

The main reason to inherit the feature of div tag which allocates a separate block to each individual entity, is to enjoy the benefit of styling each entity on the webpage according to user choice/theme. This not only helps in scaling but also helps in designing beautiful web pages.

Code 2: Styling the content

Html file: index.html

<!DOCTYPE html>
<html>
  <head>
    <title>HTML div Tag</title>
  </head>
  <link rel=stylesheet href="style.css">
    </head>
  <body>
   
        <div class="div_styling">
    </div>
    <div class="text_styling">
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptate quo
          ullam modi alias assumenda, itaque libero? Quas quidem sint illo.
        </p>
        <p>
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Necessitatibus
          ipsam eaque rem dicta, quos quas ipsum.
        </p>
  </div>
  </body>
</html>

CSS file: style.css

body {
  margin: 0 auto;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #e2dcdc;
}




.div_styling {
  background-color: #3ee5eb;
  width: 185px;
  height: 185px;
  border-radius: 50%;
}




.text_styling {
  font-family: cursive, sans-serif;
  font-size: 1.2rem;
  font-weight: bolder;
  font-style: italic;
}

Output:

Div Tag in HTML

Code 3: Basic HTML web page layout using div tag

<!DOCTYPE html>
<html>
  <head>
    <title>Basic HTML Layout using Div</title>
  </head>
  <body
  style="background-color: blanchedalmond;">
    <div style="width: 100%">
      <div style="background-color: rgb(71, 207, 207); text-align: center">
        <h1>Example of page layout using div tag</h1>
      </div>
      <div
        style="
          background-color: rgb(230, 82, 82);
          height: 350px;
          width: 30%;
          float: left;
        "
      >
        <h2>Main Menu</h2><br />
        <p><a href="Link L1 ">sample link L1</href><br /> </p>
        <p><a href="Link L2 ">sample link L2</href><br /> </p>
        <p><a href="Link L3 ">sample link L3</href> <br /><hr><hr>
      </div>
      <br>
      <br>
      <div
        style="background-color: orange;
        color: black;
        height: 400px;
        width: 70%;
        float: left"
        >
       <h3> This is a sample content. </h3>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Error
          excepturi corporis accusamus quas consectetur ab modi voluptatibus
          quaerat reprehenderit, sint dignissimos quibusdam quae reiciendis
          temporibus.
        </p>
      </div>
  </body>
</html>

Output:

Div Tag in HTML

Code 4: Example of span tag in HTML

File 1: index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example of span tag</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <p>
      We can use span tags to target text with CSS styling, like with
      <span id="blue-text">this blue text</span> and
      <span id="orange-text">this orange text</span>.
    </p>




    <p>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur,
      necessitatibus.
      <span id="new-font"
        >Lorem ipsum dolor sit amet consectetur, adipisicing elit.
      </span>
      or <span id="new-font-size">font size</span>.
    </p>
  </body>
</html>

File 2: style.css

#blue-text {
  color: blue;
}
#orange-text {
  color: orange;
}




#new-font {
  font-family: Helvetica;
}




#new-font-size {
  font-size: 28px;
}

Output:

We can use span tags to target text with CSS styling, like with this blue text and this orange text.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur, necessitatibus. Lorem ipsum dolor sit amet consectetur, adipisicing elit. or font size.

Conclusion:

The div tag in HTML is a block level element which is used to create a generic container which is generally not visible and is used to group other elements. Some key points of div tag are:

It is a block level element

Creates a generic container that groups other elements.

Whole block is assigned to each individual entity inside this tag.

It adds a line break after its closing tag.

Related Topics

HTML <address> Tag

Hyper Text Markup Language, HTML is the best markup language for creating Web pages. HTML consists of a wide variety of elements. In all of these elements, one element is...

4 minutes read.

HTML <style> tag

The section of information that we want to style is contained in html style tag. It includes CSS, which is used to design the text and images in the web page. Syntax: <head> <title>…………....

3 minutes read.

HTML Text Format

Introduction HTML may format text in two different ways. The HTML tags define a text property to make it appear one way or another. A text element may also be formatted...

7 minutes read.

HTML <samp> tag

The Samp tag in HTML is used for defining the sample output from the computer program. This tag is a phrase type tag and a container tag that means the...

2 minutes read.

Html &lt;dd> Tag

In an HTML description list, an item's definition or description is indicated using the definition description ("dd" tag). A <dd> tag can contain paragraphs, line breaks, images, links, and lists. It...

4 minutes read.

Responsive HTML5 and CSS3 Tutorial

Introduction to HTML5 Tutorial HTML5 is the latest and upgraded version of HTML. HTML is a hypertext markup language. Technically, it is not a programming language. It is a markup language. HTML...

17 minutes read.

HTML <span> tag

The span tag in HTML is used as an inline container element to mark up a specific or special chunk of a text or a section of a page. If you...

2 minutes read.

HTML Audio

HTML AUDIO: HTML5 provides a method to embed the audio file into a webpage by using HTML5. Before HTML5 audio files is used to play on the browser with the help of...

1 minute read.

HTML Font Color

The HTML <font> tag has an attribute called color, which defines the color of the text. We use the color attribute inside the <font> tag to specify the color of...

6 minutes read.

HTML &lt;blink> Tag

In HTML, the blink tag is used to create the text which flashes. The blink tag is an inline element in HTML. It is observed that the blink tag used...

2 minutes read.

HTML Canvas

HTML Canvas: Using JavaScript, the < canvas > tag in HTML is used for drawing graphics on web page. It can be used to draw routes, boxes, text, gradient, and...

3 minutes read.

HTML Form Action

The <form> tag has an attribute named action that contains a URL. The HTML action attribute defines the webpage URL on which you want to send the data after submitting...

4 minutes read.

HTML Charsets

In this tutorial, we are going to discuss about the charsets in the HTML. The letters, numbers, and several other symbols are shown correctly by the web browser. All of this...

2 minutes read.

Html <colgroup> Tag

A HTML table's column group is specified by the "colgroup" tag. If you want to apply various properties to one or more <col> elements in an HTML table, then you...

4 minutes read.

HTML Paragraphs

In html, there are some elements which starts with new line and one of them is paragraph. Which is said to be a block of text. In html <p> tag...

4 minutes read.

CSS Clearfix

CSS Clearfix: A clearfix (or clear float) in CSS is for a component to clear or fix the child component, so we don’t need to insert additional markup. The clear...

4 minutes read.

How to insert spaces/tabs in text using HTML/CSS

What is HTML? HTML or HyperText Markup Language is used to design web pages. It was released in 1993. The file extension name for HTML file is “.html”. An HTML file...

4 minutes read.

HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our...

4 minutes read.

HTML Unordered List

In HTML, we have <ul> tag which defines the unordered list. And it is also known as bulleted list, because as we know it is not ordered list therefore it...

2 minutes read.

HTML Video

HTML VIDEO: HTML5 has introduced the <video> tag to embed video into the webpage directly without using the plug-ins.   Syntax: <video attributes >     <source src="address" type="video/mp4">     Your browser does not support the video tag.   </video> <video width="320" height="240" controls>     <source src="mountain.mp4" type="video/mp4">     Your browser does not support the video tag.   </video> Output: ← Prev ...

1 minute read.