The SPAN and DIV HTML Elements
How to Use SPAN and DIV with CSS on Web Pages
The SPAN and DIV elements are very useful when dealing with CSS(Cascading Style Sheets).
These two elements in a similar fashion, but they serve different purposes.
The DIV Element :The DIV elements define logical divisions in your web page. It acts a lot like a <P> element, by placing newlines before and after the division. A division can have multiple paragraphs in it.
Using the DIV Tag
To use the DIV element, simply surround the area of your page that you want as a separate division with the <div> and </div> tags:
<p>contents of div</p>
The DIV element gives you the chance to define the style of whole sections of the HTML. You can define a division of your page as a call out and give that area a different style from the surrounding text. That area could have images, paragraphs and headlines, anything you wanted. The DIV element also gives you the ability to ID areas of your documents so that you can change them with Ajax and dynamic HTML.
The DIV element is different in new HTML5 SECTION element because it does not give the enclosed content any semantic meaning. If you aren’t sure whether the block of content should be a DIV or a SECTION, think about what that content’s purpose is and why you need the DIV or SECTTION element.
1. If you need the element simply to add styles to that area of the page, you should use the DIV element.
2. If that area of the page has a specific meaning, for example it holds all your social media elements or it contains your blogroll, then you should use the SECTION element.
One thing to keep in mind when using the DIV element 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 element are:
Even if you don’t use style sheets or DHTML, you should get into the habit of using the DIV element. This will give you more flexibility and future proof your HTML. Also, you can use the id to identify your divisions so that your web pages are well formed.
Because the CENTER element has been deprecated in HTML 4.0 and is obsolete in HTML5, it is a good idea to start using <div style=”text-align: center;”> to center the content inside your DIV.
Learn More About the DIV Element
The SPAN Element
The SPAN element has very similar properties to the DIV element, in that it changes the style of the text it encloses. But without any style attributes, the SPAN element won’t change the enclosed items at all.
The primary difference between the SPAN and DIV elements is that SPAN doesn’t do any formatting of it’s own. The DIV element includes a paragraph break. The SPAN element simply tells the browser to apply the style rules to whatever is within the SPAN.
To use the SPAN element, simply surround the text that you want to add styles to with the<span> and </span> tags:
<p><span class=”highlight”>Highlighted text</span> and non-highlighted text.</p>
The SPAN element has no required attributes, but the three that are the most useful are the same as for the DIV element:
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. For example:
<h3>This is My <span style=”color: red;”>Awesome</span> Headline</h3>
- How can we submit a form without a submit buttom?
- How do I center a table?
- Can I include comments in my Style Sheet?
- In DHTML what is the difference between “FontSize” and “Font Size”
- HTML is a Programming Language or Scripting Language?
- How do I quote font names in quoted values of the style attribute?
- What are META tags?
- How can I check for errors om html?
- How do I make my div 100% height?
- what can be done with style sheets that can nit be accomplished with regular HTML ?
- Is there anything that can’t be replaced by style sheets ?
- What browsers support style sheets? To what extent?
- Do any WYSIWYG editors support the creation of Style Sheets? Any text-based HTML editors?
- Can you use someone else’s Style Sheet without permission?
- What does the “Cascading” in “Cascading Style Sheets” mean?
- Which style specification method should be used? Why?
- What are the advantages/disadvantages of the various style methods?
- How do you override the underlining of hyperlinks?
- What is the difference between ID and CLASS?
- Which characters can CSS-names contain?
Download the answers file here