Click here to Skip to main content
Click here to Skip to main content

Five XHTML Elements You Didn't Know But Were Afraid to Ask

By , 15 May 2006
 

Introduction

With experience, working with ASP.NET, I noticed some important XHTML tags that are rarely used by web developers, even the experienced ones, so I thought of writing this article to promote them.

Most of the mentioned elements are HTML compatible, but all of them are XHTML compatible. I've tested these elements on Internet Explorer 6 and Firefox 1.5. Also, I've validated the demo project against XHTML 1.1 and XHTML 1.0 Transitional on the W3C Validator.

The elements are optgroup, label, fieldset/legend, col/colgroup and acronym. In this article, I point out the differences in displaying each of these elements between IE and Firefox, moreover, I show their relation with ASP.NET. What follows is a quick introduction for each element:

1. optgroup

optgroup sample

<label for="county">County</label>
<select id="county">
    <optgroup label="England">
        <option>Surrey</option>
        <option>Kent</option>
    </optgroup>
    <optgroup label="Scotland">
        <option>Orkney</option>
    </optgroup>
</select>

<label for="languages">Languages</label>
<select size="6" multiple="multiple" id="languages">
    <optgroup label="Classical">
        <option>C#</option>
        <option>C++</option>
        <option>Java</option>
    </optgroup>
    <optgroup label="Web">
        <option>ASP.NET</option>
        <option>PHP</option>
    </optgroup>
</select>

The optgroup, which stands for Option Group, is an element that groups a set of option elements, inside a select element, and labels them. It is perfect for showing states and cities, countries and states, and other similar categorized items.

Unfortunately, this element is not generated by any ASP.NET server controls, however, I believe you can easily inherit from the DropDownList, ListBox, or the Item controls and make the necessary modifications to generate it.

2. label

label sample

<label for="FirstName">Full&nbsp;Name</label>
<input id="FirstName" type="text" />
Gender:&nbsp;
<label for="male">Male</label>
<input id="male" type="radio" name="gender" />
<label for="female">Female</label>
<input id="female" type="radio" name="gender" />

The label element provides a caption for a form element. Its main attribute is the "for" attribute which will point to the element that you want to provide a caption for.

In addition to providing a caption for the element, label will make selecting the associated element easier; so by clicking on the caption, you will be setting focus to the associated element, as if it is making the caption text a part of the associated element. In the provided demo, try clicking on the word "Full Name" and the text box next to it will be selected; also, when you click on the word "Male", the radio button will be selected.

ASP.NET generates this element for the Text property of the RadioButton and CheckBox server controls (and other controls as well). This explains the secret of setting focus to the control whenever clicking the text next to it. In ASP.NET 2.0, the Label server control has a new attribute AssociatedControlID that will render the control as an HTML label associated to the specified element versus rendering it as a span element.

3. fieldset/legend

fieldset sample

<fieldset>
    CP is a good place to publish XHTML related articles. 
</fieldset>

<fieldset>
    <legend>Personal Information</legend>
    <label for="FirstName">Full&nbsp;Name</label>
    <input id="FirstName" type="text" />
    Gender:&nbsp;
    <label for="male">Male</label>
    <input id="male" type="radio" name="gender" />
    <label for="female">Female</label>
    <input id="female" type="radio" name="gender" />
</fieldset>

The fieldset is an element that creates a frame, and legend is an optional element that creates a title for this frame.

In ASP.NET 2.0, there is a new attribute GroupingText for the Panel web control that will render it a legend and fieldset. However, ASP.NET 1.1 doesn't have any standard web control that could generate this element.

4. col/colgroup

optgroup sample

<table border="1">
    <colgroup>
        <col width="150px" />
        <col width="10px"  />
        <col width="220p" />
    </colgroup>
    <tr>
        <td valign="top">1</td>
        <td>&nbsp;</td>
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
        <td>&nbsp;</td>
        <td>4</td>
    </tr>
</table>

The colgroup element is a table element. Its only child element is the col element. Each col element affects a column in the table. You can specify attributes that apply to all the columns in the colgroup element, and specific attributes for individual columns in the col element. You can also omit the colgroup element and keep the col elements.

The advantage of using the col element is that you only need to specify an attribute once rather than adding the same attribute across every td element in the column, and the result is less attributes and easier to manage code.

For cross-browser issues, use the width attribute only with the colgroup and col elements because I noticed that Firefox doesn't support all the attributes of the colgroup and col.

Sadly, the ASP.NET's Table and GridView server controls do not generate these elements.

5. acronym

acronym sample

<acronym title="Code Project">CP</acronym> is a good place to publish 
<acronym title="Extensible HyperText Markup Language">XHTML</acronym>
related articles.

The acronym element provides a definition for a term via the title attribute. By using this element, you are helping readers who are not familiar with the term, better optimizing your page for search engines and supporting accessibility.

By default, IE 6 doesn't show any sign on the text surrounded by this element; however, Firefox displays a dashed line under it. Both browsers display a tool tip of the full meaning whenever you stop your cursor over it.

If you like this element, have a look at the abbr (abbreviation) element which has a similar role.

Conclusion

I have spotted some elements that I thought need to have extra light on; however, there are more hidden elements to spot like th, tf, base, address, bdo, blockquote, code, dl, dt, dd, and dfn elements.

This article served as a quick reference for each element; for a full list of the attributes and usage, try the W3 Schools, htmldog.com, and you can always go back to the official documentations at the World Wide Web Consortium.

If you found the contents of this article useful, then please remember to vote.

History

  • 25 April 2006 - First version.
  • 10 May 2006 - Second version.
  • 13 May 2006 - Third version.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Adam Tibi
Team Leader
United Kingdom United Kingdom
Member
Software Consultant, Lives in Guildford/Surrey, UK.
 
www.AdamTibi.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membergagan sawaliya9 Aug '12 - 1:53 
nice
GeneralNice articlememberEvoluteur18 Mar '10 - 7:06 
Thanks for sharing.
GeneralWowmemberkonikula21 Dec '09 - 13:41 
Wow, because I really didn't know, except for label and fieldset, which I found in PhpMyAdmin long ago. Good article! Rose | [Rose]
RantFieldset Must always have a LegendmemberUgot2BkidNme8 Jun '09 - 6:54 
Fieldsets must always begin with a Legend. Otherwise use a div and put a border on it. Fieldset is a grouping and a grouping requires a label in this case it is a legend please do not start using Fieldsets without legends it is not proper.
GeneralGood article!..memberbgriffin_tpa26 Jul '07 - 4:51 
I was browseing, looking for help on something else when I
saw this. I have a totally unrelated question you may know the
answer to.
 
Can I write the HTML string being rendered by a control to a file?
I want to see whats actually happening. How would I do that?
Im trying to understand the internal workings better.
Does the browser provide a trace / debug option or use InnerHTML
or outerHTML?
 
I appreciate any help.

GeneralRe: Good article!..memberkevnord26 Jul '07 - 5:42 
Definitely. You'll want to do something like this...
 
using System.Text;
using System.IO;
using System.Web.UI;
 
public void RenderControlToFile(Control ctrl, string filepath)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
 
ctrl.RenderControl(hw);
File.WriteAllText(filepath, sb.ToString();
}
 

GeneralRe: Good article!..memberbgriffin_tpa26 Jul '07 - 7:25 
Thanks..I need to try it on an HTML control (mostly)
not an ASP.NET control..sorry I forgot to mention that,
but it should still work(?) What I really need to look at is
 
<object classid="clsid:D76D712E-4A96-11d3-BD95-D296DC2DD072"  
      height=153 onprerender="XYZ" id="FGearly" name="FGearly"
      style="LEFT: 16px; TOP: 200px; position: absolute;" width=438>
</object>
 
I'm thinking I can use the snippet you sent in a VB script inserted
in the header?..
 
I appreciate your help.
GeneralRe: Good article!..memberbgriffin_tpa26 Jul '07 - 11:45 
It dies in the vb script on the 3 "new" definitions..
specifically the 1st one..stringbuilder. If I put this
in the code behind (where it belongs) it cant see the
<object> in order to pass it to:
 
RenderControlToFile(Control ctrl, string filepath)
 
This code is perfect for an ASP.net control and I'm
going to keep it on file for the future,but for now..
What should I do?
 
==== Works perfectly on an *.HTM form ================
 
<script id="XYZ" language = "vbscript" runat="server">
Sub window_Onload
  
      fgEarly.FontSize = 9
End Sub
</script>
...
<body>
<object classid="clsid:275DBBA0-805A-11CF-91F7-C2863C385E30"
      height="154" id="fgEarly" style="LEFT: 16px; TOP: 352px;
      position: absolute;" width="438">No good.</object>
</body>
=======================================================
..but fgEarly is "undefined" in the VBscript on an .aspx form..??

GeneralOPTGROUPmemberbiggun17 Jul '07 - 8:38 
Hi,
 
First of all, thanks for your good article, those tags are cool hidden stuff. I realize that recently, i spend some time on implemeting a function that generates the HTML code of a SELECT tag with indentation. Just what the OPTGROUP does...
 
Unfortunately, as of HTML 4.0, OPTGROUP tags can't be nested. You can't get something like this :
Categorie 1
Sub Categorie 1.1
Item 1
Item 2
Sub Categorie 1.2
Item 1
Categorie 2
Sub Categorie 2.1
....
 
By the way, with some non breakable spaces in the text of OPTION tag and CSS, you can mimic nested OPTGROUP.
 
Nice job.
Stephan
GeneralRe: OPTGROUPmemberAdam Tibi17 Jul '07 - 23:28 
Thank you Stephen,
 
Yes, you can reach the same LOOK with CSS, however, the indented item is unselectable in the optgroup which is also an additional benefit.
 
Cheers,
Adam Tibi
 
Make it simple, as simple as possible, but not simpler.

GeneralNice Articlemembersmsayami16 Jul '07 - 11:27 
Thanks for reminding useful stuffsSmile | :)
GeneralGood One!memberCharuT29 Oct '06 - 14:01 
It was a very useful artcile Mr.Adam Tibi. Please keep up the good work and Thank You!
 
Sincerely,
Charu. Smile | :)

 
Charu.

GeneralClarification re: labelmemberVBScab18 Sep '06 - 21:22 
Some good stuff here. However, on reading, I thought that the reason LABEL is useful hasn't been made crystal-clear, so let me try to do that.
 
With elements like radio buttons and check boxes, a user has to click on the actual element itself. A good number of users, however, are used to clicking on the text which is associated with the element and THIS is what the LABEL tag enables - any text (or other element, IIRC) inside the tag can be clicked to select/de-select the tagged element.
 
You can see an excellent example right here. If you click 'New Message' at the top of the posts, you can see a check box 'Ignore HTML tags in this message (good for code snippets)' beneath the row of smileys. Without LABEL, you'd have to click the check box itself to select that option. You can, however, click anywhere on the text to select it.
GeneralRe: Clarification re: labelmemberAdam Tibi19 Sep '06 - 8:34 
I totally agree. Not to mention the great benefit for screen readers, hence, accessibility.
 
Thank you for the comment,
cheers,
Adam Tibi
 
Make it simple, as simple as possible, but not simpler.

GeneralYou missed CaptionadminChris Maunder23 May '06 - 15:52 
Kinda handy.
 
cheers,
Chris Maunder
CodeProject.com : C++ MVP

GeneralGreat Things But.....memberSameers (theAngrycodeR )21 May '06 - 13:07 
Some of the things you mentioned I already know but some are really new and nice too. Thanks for sharing.... But, is there any browser wide compatibility issue in all of these? Do you think this all will run exactly same on firefox and other browsers as it will run on IE?
 
Cheers,
Sameers

 
Need custom software? Contact DevelopersINN[^]
Need to add reminders for your Outlook emails? Try Outlook Personal Assistant[^]
GeneralRe: Great Things But.....memberAdam Tibi21 May '06 - 22:57 
Thank you!
 
I don't think, I have tested the code on IE and FireFox and it is working correctly! However, IE adds its own theme to them like adding a round edges and stuff. So, they are XHTML, IE and FF compliant.
 
I didn't test this on Netscape because Netscape is now using IE engine.
 
Cheers,
Adam Tibi

 
Make it simple, as simple as possible, but not simpler.
GeneralRe: Great Things But.....memberxxrono7 May '07 - 10:10 
hi thanks for tha article
 
any reason why on my mozzila it doesn't work?i'm talking about the fieldset/legend
 
in mozzila i don't get rounded corners,looks not good..
something in my css?
 
thanks
GeneralRe: Great Things But.....memberAdam Tibi7 May '07 - 22:44 
Hi,
 
The round corners is an IE only behaviour, there is no W3C specifications that says so. You can have your own CSS to control its look.
 
Regards,
Adam Tibi
 
Make it simple, as simple as possible, but not simpler.

Generalfieldset rounded cornersmemberwebber12345613 May '06 - 8:02 
I'm not able to get rounded corners for fieldset with either Firefox or IE 6.
 
is there a specific DTD required ?
GeneralRe: fieldset rounded cornersmemberAdam Tibi13 May '06 - 8:24 
I think the rounded corners only shows on Win XP + IE 6, but I am not sure, do you have Win XP?
 
Firefox doesn't show rounded corners.
 
Regards,
Adam Tibi
 
Make it simple, as simple as possible, but not simpler.
GeneralRe: fieldset rounded cornersmemberwebber12345613 May '06 - 9:40 
yep, win xp sp2 and IE 6
 
-- modified at 16:11 Saturday 13th May, 2006
 
I can get firefox to work with
 
fieldset {
-moz-border-radius: 8px;
_position: relative;
background-color: #c0e0ff;
border-radius: px;
width: 85%;
}
 
legend {
position:relative;
topBlush | :O .5em
}
 

With no background color applied and above style I can get rounded corners in both browsers. If I have a background color applied, the rounded corners don't work in ie6 but still work in Firefox .
 
Smile | :)
 

 

-- modified at 16:20 Saturday 13th May, 2006
GeneralRe: fieldset rounded cornersmemberJon Rista16 Jun '06 - 19:26 
IE6 will apply the skinned "groupbox" style to fieldsets in the browser, unless you explicitly take steps to render all web page controls using the default html styling (which is possible with IE6). If you are using a non-standard skin for WindowsXP (i.e., StyleXP, WindowBlinds, etc.), your fieldsets will have whatever style the skin defines applied to them.
GeneralRe: fieldset rounded cornersmemberxxrono8 May '07 - 1:25 
COol Post
 
Cheers
 
ninja coding

GeneralClarification acronym vs. abbrmemberl0b010 May '06 - 22:38 
Nice article; thanks!
 
However, the use of acronym vs. abbr should be clarified a bit: An acronym is an abbreviation which can be pronounced as a word. E.g., "HTML" is an abbreviation, but not an acronym - It's pronounced "Age tee emm ell". "WYSIWYG" is an acronym. So both your "acronym" elements should really be "abbr".

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 15 May 2006
Article Copyright 2006 by Adam Tibi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid