Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is HTML div tag?
2.1.
Syntax
2.2.
Example
2.2.1.
Code
2.3.
HTML
2.3.1.
Output
2.3.2.
Explanation
2.4.
Uses of html div tag
3.
What is HTML span tag?
3.1.
Syntax
3.2.
Example
3.2.1.
Code
3.3.
HTML
3.3.1.
Output
3.3.2.
Explanation
3.4.
Uses
4.
Difference Between div and span
4.1.
Example
4.1.1.
Code
4.2.
HTML
4.2.1.
Output
4.2.2.
Explanation
5.
Frequently Asked Questions
5.1.
What is the difference between span and div?
5.2.
What is the difference between span and div in Vue?
5.3.
Can I use div instead of span?
5.4.
Why is div used?
5.5.
What is the difference between Div and span and P for text?
6.
Conclusion
Last Updated: Apr 28, 2024
Easy

Difference between <div> and <span> Tag in HTML

Introduction

HTML (HyperText Markup Language) is the language we use to structure content on web pages. HTML does this using different tags. Each tag serves a unique purpose. For example, the <br> tag creates a line break. 

The <div> and <span> tags are the most common HTML tags we use to group and style content on web pages. Understanding the differences between these two is crucial; both have varying effects on the layout and presentation of the content.

difference between div and span

This article will discuss the div tag, the span tag, their uses and their differences.

What is HTML div tag?

<div> tag is an HTML tag which defines a web page division. It creates a block-level element that groups multiple HTML elements like images, text, etc. A block ​​element takes up the entire width of its parent container and starts on a new line, pushing down the following content.

On inspecting the source of a webpage, we can see that almost every web page uses a lot of nested <div> tags.

Syntax

The syntax for the <div> tag is as follows.

<div>
    <!-- Content to group -->
</div>


We can apply many different attributes to the div tag, just like any other tag in HTML.

Example

Let us look at an example to understand the div tag.

Code

  • HTML

HTML

<!DOCTYPE html>
<html>

<head>
   <title>What is the div tag?</title>
   <style>
       .box {
           width: 50%;
           border: 2px solid black;
           background-color: lightblue;
           display: flex;
           justify-content: center;
           align-items: center;
       }
   </style>
</head>

<body>
   <div class="box">
       <h1>Welcome to Coding Ninjas!</h1>
   </div>
   <div class="box">
       <p>Using the <code>&lt;div&gt;</code> tag:</p>
   </div>
</body>

</html>

Output

<div> example

Explanation

This HTML code displays two boxes on a webpage with some text inside them. We define the styling of these boxes using CSS properties defined in a <style> tag in the <head> section of the document. The <div> tag creates boxes that appear in sequential order on the webpage.

Uses of html div tag

We can use the <div> tag to do the following.

  1. It groups related elements together, making applying a similar style to all these elements easier.
     
  2. It also creates the page layout by dividing it into rows, columns, etc. The div tag also allows the creation of a sidebar or a navbar.
     
  3. It helps in applying CSS properties to a block of content.

What is HTML span tag?

A <span> tag is an HTML tag that defines a small document section and applies styles to some text or part of a document. It creates an inline element which takes up the same space as its content. The <span> tag creates no line breaks like the <div> tag. Instead, it allows us to apply styles to a part of a document without affecting the rest.

Syntax

The syntax for the <span> tag is as follows.

<span>
    <!-- Content to style -->
</span>


We can apply many different attributes to the span tag, just like any other tag in HTML.

Example

Let us look at an example to understand the span tag.

Code

  • HTML

HTML

<!DOCTYPE html>
<html>

<head>
   <title>What is the span tag?</title>
   <style>
       .highlight {
           background-color: yellow;
       }
   </style>
</head>

<body>
   <h1>Coding Ninjas</h1>
   <p>
       <span class="highlight">Coding Ninjas</span> is a place that trains passionate people in various technologies.
       Here, we can see that the <span class="highlight">span tag</span> styles the text by highlighting it.
   </p>
</body>

</html>


Output

<span> example

Explanation

This HTML code displays a paragraph on a webpage with some text inside it. We define the styling of the text using CSS properties defined in a <style> tag in the <head> section of the document. The <span> tag creates a section of highlighted text that appears in line with the surrounding text.

Uses

We can use the <span> tag to do the following.

  1. It applies styles such as font size, colour, or weight to a specific word or phrase within a larger block of text.
     
  2. It creates tooltips using the “title” attribute, which specifies the text appearing when the user hovers over the element.

Difference Between div and span

The following table shows the key differences between the div and span tag.

Comparison Criteria

div

span

Element type

It creates a block-level element.

It creates an inline element.

Line Break

There is an implicit line break before and after the element.

There is no implicit line break.

Purpose

Organises and styles sections of content.

Styles smaller pieces of inline text.

Width

Takes up the entire width of its parent container.

It only takes up the width of its content.

Nesting

It usually uses a lot of nesting.

We don't usually use nested span tags.

Align attribute

We can attach the align attribute to a div tag.

We cannot attach the align attribute to a span tag.

Example

Let us look at an example to understand the difference between div and span more clearly.

Code

  • HTML

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>div vs span example 1</title>
</head>

<body>
   <div class="parent" style="width: 50%">
       <div style="background-color: aqua"> &lt;div&gt; element 1 </div>
       <div style="background-color: greenyellow"> &lt;div&gt; element 2 </div>
       <div style="background-color: orange"> &lt;div&gt; element 3 </div>
   </div>
   <br>
   <span style="background-color: greenyellow;"> &lt;span&gt; element 1 </span>
   <span style="background-color: aqua;"> &lt;span&gt; element 2 </span>
   <span style="background-color: orange;"> &lt;span&gt; element 3 </span>
</body>

</html>


Output

<div> Vs <span> example

Explanation

The above HTML code demonstrates the difference between div and span.

The example shows that the <div> tag groups three other <div> tags together inside a parent container. Each of these children <div> tags has its background colour and appears on a new line, taking up the entire width of the parent container.

The example also shows that the <span> tag applies a different background colour to each text element. Unlike the <div> tag, the <span> tag does not create a new line for each element, and they appear side-by-side.

Frequently Asked Questions

What is the difference between span and div?

A span is an inline element used to group and style text or inline elements, while a div is a block-level element used to create sections or divisions in a document and style them as a whole.

What is the difference between span and div in Vue?

In Vue, the difference between span and div remains the same as in HTML. However, Vue allows you to dynamically bind data and apply conditional rendering to both span and div elements in the template.

Can I use div instead of span?

Yes, you can use a div instead of a span, but keep in mind that div is a block-level element and may affect the layout of surrounding elements, whereas span is an inline element and does not introduce line breaks.

Why is div used?

Div is used to create sections or divisions in a document and style them as a whole. It provides a way to structure and organize content on a webpage, making it easier to apply styling and layout to grouped elements.

What is the difference between Div and span and P for text?

Div and span are generic containers used for grouping and styling content, whereas p (paragraph) is a semantic element specifically used for defining paragraphs of text. Div and span are block and inline elements, respectively, while p represents a block-level element by default.

Conclusion

This article discussed the div tag, the span tag, their uses and the difference between div and span. Understanding these two tags' differences can help us create well-structured and visually appealing web pages.

To learn more about web development, we recommend reading the following articles:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Coding!

Live masterclass