Forum : about div and span
Brief description  about Online courses   join in Online courses
View yasmin  banu 's Profile

about div and span

May i know the difference between div and span? for class we are using span and for some symbols using div. Is that correct?
Asked by yasmin banu | Jul 26, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi

<div>

The <div> tag defines logical divisions (defined) in your Web page. It acts a lot like a paragraph tag, but it divides the page up into larger sections.

<div> also gives you the chance to define the style of whole sections of HTML. You could define a section of your page as a call out and give that section a different style from the surrounding text.

But that's not all it does! The <div> tag gives you the ability to name certain sections of your documents so that you can affect them with style sheets or Dynamic HTML.

One thing to keep in mind when using the <div> tag is that it breaks paragraphs. It acts as a paragraph end/beginning, and while you can have paragraphs within a <div> you can't have a <div> inside a paragraph.

The primary attributes of the <div> tag are:
style
class
id

Even if you don't use style sheets or DHTML, you should get into the habit of using the <div> tag. This will give you more flexibility when more XML parsers become available. Also, you can use the id and name attributes to name your sections so that your Web pages are well formed (always use the name attribute with the id attribute and give them the same contents).

Because the <center> tag has been deprecated in HTML 4.0, it is a good idea to start using

<div style="text-align: center;">

to center the content inside your div.
More About the <div> Tag


<span>

The <span> tag has very similar properties to the <div> tag, in that it changes the style of the text it encloses. But without any style attributes, the <span> tag won't change the enclosed items at all.

The primary difference between the <span> and <div> tags is that <span> doesn't do any formatting of it's own. The <div> tag acts includes a paragraph break, because it is defining a logical division in the document. The <span> tag simply tells the browser to apply the style rules to whatever is within the <span>.
The <span> tag has no required attributes, but the three that are the

most useful are:
style
class
id

Use <span> when you want to change the style of elements without placing them in a new block-level element in the document. For example, if you had a Level 3 Heading (<h3>) that you wanted the second word to be red, you could surround that word with

<span style="color : #f00;">2ndWord</span>

and it would still be a part of the <h3> tag, just red.


Regards
Jul 27, 2010