4 replies [Last post]
sawatdee
Offline
newbie
Last seen: 6 years 20 weeks ago
Joined: 2008-03-21
Posts: 8
Points: 0

Are there any difference between the following two elements?



If so, what are the differences?

Also, what are the differences between these elements?

dhaynes
dhaynes's picture
Offline
Regular
Midwest, US
Last seen: 6 years 9 weeks ago
Midwest, US
Timezone: GMT-4
Joined: 2007-05-20
Posts: 38
Points: 0

sawatdee wrote:Are there any

sawatdee wrote:
Are there any difference between the following two elements?


If so, what are the differences?

Also, what are the differences between these elements?


Both elements will display exactly the same way due to you changing the display of one in each example. Semantically they are different, meaning they are supposed to be used for certain things. Here are some examples of their proper usage.

DIV Tag: http://www.w3schools.com/tags/tag_div.asp

SPAN Tag: http://www.w3schools.com/tags/tag_span.asp

Need a Wordpress site? I'm your guy.

Jenni
Jenni's picture
Offline
Enthusiast
Seattle
Last seen: 5 years 21 weeks ago
Seattle
Joined: 2007-03-09
Posts: 115
Points: 0

Well basically a div is a

Well basically a div is a "block level element" meaning it inherently cause a line break. A span is a box like a div but won't cause a break, unless you set the "display" attribute to "block". Read those two links he posted, you should really understand the semantics of these elements before you start coding css, because its super easy to use the wrong kind of element for the wrong purpose. You won't know if you've used it correctly, because the result will be the same, but it won't make sense to other developers and could cause problems later down the road.

sawatdee
Offline
newbie
Last seen: 6 years 20 weeks ago
Joined: 2008-03-21
Posts: 8
Points: 0

The reason I asked is

The reason I asked is because I found some sample code on the Internet where someone used In his explanation, he wrote, "it does not turn the span into a block level element and the normal rules for inline elements in html must still be obeyed".

I only know of two differences between block and inline elements. First, block elements have an implied line break before and after them, whereas inline elements do not. Second, a block element takes up the entire width of its parent, whereas an inline element takes up only the width of its content. Are there other differences? I have run some tests and can not find any "normal rules for inline elements in html" that make a block level span behave any differently than a plain old div.

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 1 year 47 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

DIV and SPAN are both

DIV and SPAN are both non-semantic containers. Their only "physical" difference is that one is by default display:block and the other is by default display:inline.(*)

However ... Smile

Within the HTML and XHTML doctypes there are various rules as to what elements can appear where. In those rules DIV is a block level container. It can contain just about anything, but it can only appear within other block level containers. A SPAN is an inline level container. Its very restricted in what can appear within it - mostly only other inline level elements, however it can appear almost anywhere. In some places they can both appear (e.g. LI, DT) in others only one can. e.g. BODY, FORM (DIV); Hx, DT, P, STRONG, A (SPAN).

So, when you need an non-semantic element the one you choose is dependent on where in the html structure the element is located. Then if the default display setting is not what you require, change it in the CSS. I think we might be getting a couple more of these in the next versions of HTML/XHTML, the newish inline-block should have its own non-semantic container - although given it generally can appear anywhere an inline level element can, I guess it could end up being a SPAN.

(*) Sawatadee, those differences you have described are the differences between the display:block and display:inline. Appearance of elements are completely controlled by their styles. Default appearance is the default styles applied in the browser stylesheet. E.g. If you search for FF's stylesheets (res directory iirc) you'll find "strong {font-weight: bold; }" amongst all the other standard styles.