4

I'm new to html, I know the tag defines a division or a section in an HTML document, but you can sometimes see the use of div tag like this:

<div>This is some text</div>

shouldn't we use some text display purpose tags like <p>?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • It would entirely depend on how you wish to either access or style the element on your page. – li x Jun 17 '17 at 13:43
  • Possible duplicate of [Should I use the

    tag in markup?](https://stackoverflow.com/questions/1261104/should-i-use-the-p-tag-in-markup)
    – KyleS Jun 17 '17 at 13:45
  • Duplicate of https://stackoverflow.com/questions/28529390/ok-to-have-text-in-div-without-paragraph-tag/28529404#28529404 – vikramvi Apr 28 '21 at 13:36

6 Answers6

5

Gererally if you use tags like <p> or <span> it makes applying CSS easier, and JavaScript and jQuery too, but otherwise it's perfectly fine to use just plain text. Refer to this: https://stackoverflow.com/a/28529404/7733026

Yes, it is ok to use a <div> element without <p>.

A <p> would tell that the text within a <div> element is split into paragraphs, thus if you have text split into paragraphs, you should use <p>; on the other hand, a <p> cannot contain elements other than so-called phrasing content; thus you cannot have a <div> inside a <p>.

For example, if you have some text in a div, and you want some of it to be blue and some of it to be red, you would do this:

.blue {
  color: blue;
}
.red {
  color: red;
}
<div>
  <p class="blue">blue</p>
  <p class="red">red</p>
</div>

Because it's impossible to say with only CSS (and very hard with JS) 'get the text that says blue and give it the color of blue', but it's very easy to say with either, 'get the <p> that has a class of blue and make the color blue'

James Douglas
  • 3,328
  • 2
  • 22
  • 43
2

In general, <div> tags should be used as a last option, when a more semantic tag is available.

Bobby Speirs
  • 667
  • 2
  • 7
  • 14
1

Yes, the correct tag for describing paragraph of text is <p>, but in this case, I suppose the author of the code above wanted to use <div> because it does not come with predefined (by browsers) styles like margin.

Albert221
  • 5,223
  • 5
  • 25
  • 41
1

The <p> tag defines a paragraph. The <div> tag defines a division or a section in a HTML document. and basically use other tags in <div> tag and use <div> to division HTML page. but you can use <div> without any other tag but it's not good for SEO issues.

mshomali
  • 664
  • 1
  • 8
  • 27
0

The <div> tag is used to group block-elements to format them with CSS. You can set some id for particular div and you can take action using that id.

<div style="color:#0000FF" id="xyz">
  <h3>This is a heading</h3>
  <p>This is a paragraph.</p>
</div> 

Its not necessary that you should always wrap text with <p>.

Dhaval
  • 1
  • 1
0

Using <p> tag is the de-facto choice for writing text. However one can use <div> or a <span> tag for writing text depending on the scenario.

You can also use <p> inside <div>

<div><p>Your content here</p></div>
Tal
  • 1,091
  • 12
  • 25