×

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 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.

Font tag in HTML

HTML stands for Hypertext Mark-up Language and is used to create web pages and other information that can be later displayed in a web browser. It also consists of a...

4 minutes read.

HTML Images

HTML has <img> tag to display image on the web pages or web application. This tag is an empty tag i.e. we don't have to close this tag while using. SYNTAX: <img src="source address" attributes="alt, height, width"> Example: <html>   <body>   <center>Image tag Example</center>   <img src="https://www.tutorialandexample.com/resources/images/favicon.png"    alt="tutorialandexample"/>   </body>   </html> Output: Image...

1 minute read.

HTML City tag

HTML <City> tag HTML < cite > tag specifies a citation, a reference or title to a creative work, quoted content, books, websites, a research paper, a blog post, a painting,...

3 minutes read.

HTML &lt;body> alink Attribute

HTML <body> alink Attribute What is HTML? The HTML or HyperText Markup Language is used to design web pages. The file extension name for HTML file is ".html". It was released in...

2 minutes read.

HTML Forms

Forms are required when we want to collect data from the user. Html forms consists of different fields to take user inputs such as: input, text area, checkbox, password, radio...

4 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.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 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 <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 <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 SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.

HTML <script> Tag

For embedding a client -side script, we use script tag in html tag. This <script> tag can be used for containing scripting statements. It can also point another external script file...

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 Tutorial

Introduction This HTML tutorial helps beginners and professionals to learn HTML easily. HTML is a widely used web technology for website development. It stands for Hyper Text Markup Language. It is mainly used to...

7 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.

How to Create a Portfolio Gallery using HTML and CSS?

First, before starting this, the user should know some of the basics of HTML and CSS to create a portfolio gallery. To add more functionality to the website, we can also...

7 minutes read.

HTML <br> tag

HTML is referred to as Hyper Text Markup Language, which is the most popular language for creating Web pages. HTML explains the construction of a Web page. It contains a...

4 minutes read.

HTML Id Attribute

The HTML id attribute represents the unique identifier for an element of the HTML document. Each element has its unique identifier. The id attribute is a global attribute that can be...

12 minutes read.

Most Commonly used Tags in HTML

In this tutorial, we'll look at the most popular HTML tags. We know HTML is a fundamental requirement for creating a website. As a result, it is essential to remember...

9 minutes read.