user8033404
user8033404

Reputation:

Using div tag to display text in html

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

Upvotes: 6

Views: 48965

Answers (6)

James Douglas
James Douglas

Reputation: 3446

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'

Upvotes: 6

Tal
Tal

Reputation: 1231

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>

Upvotes: 0

Dhaval
Dhaval

Reputation: 1

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

Upvotes: 0

mshomali
mshomali

Reputation: 654

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.

Upvotes: 1

Bobby Speirs
Bobby Speirs

Reputation: 667

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

Upvotes: 2

Albert221
Albert221

Reputation: 7072

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.

Upvotes: 1

Related Questions