HTML attribute: Difference between revisions

Content deleted Content added
wrote lead
Description: c/e plus example
Line 3:
 
==Description==
HTML attributes generally appear as name-value pairs, separated by "<code>="</code>, and are written within the start tag of an element, after the element's name:
:<''tag'' ''attribute''="''value''">''(content to be modified by the tag)''</''tag''>
::Where ''tag'' names the HTML element, ''attribute'' is the name of the attribute, set to the provided ''value''.
 
:<''source lang="html4strict"><tag'' ''attribute''="''value''">''(content to be modified by the tag)''</''tag''></source>
The value may be enclosed in single or double quotes, although values consisting of certain characters can be left unquoted in HTML (but not XHTML).<ref>{{cite web|url=http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.2.2 |title=On SGML and HTML |publisher=World Wide Web Consortium |date=|accessdate=November 16, 2008}}</ref><ref>{{cite web|url=http://www.w3.org/TR/xhtml1/diffs.html#h-4.4|title=XHTML 1.0 - Differences with HTML&#160;4|publisher=World Wide Web Consortium|date=|accessdate=November 16, 2008}}</ref> Leaving attribute values unquoted is considered unsafe.<ref>{{cite web|first=Jukka|last=Korpela|url=http://www.cs.tut.fi/~jkorpela/qattr.html|title=Why attribute values should always be quoted in HTML|publisher=Cs.tut.fi |date=July 6, 1998|accessdate=November 16, 2008}}</ref>
 
Where <code>tag</code> names the HTML element class, and <code>attribute</code> is the name of the attribute, set to the provided <code>value</code>. The value may be enclosed in single or double quotes, although values consisting of certain characters can be left unquoted in HTML (but not XHTML).<ref>{{cite web|url=http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.2.2 |title=On SGML and HTML |publisher=World Wide Web Consortium |date=|accessdate=November 16, 2008}}</ref><ref>{{cite web|url=http://www.w3.org/TR/xhtml1/diffs.html#h-4.4|title=XHTML 1.0 - Differences with HTML&#160;4|publisher=World Wide Web Consortium|date=|accessdate=November 16, 2008}}</ref> Leaving attribute values unquoted is considered unsafe.<ref>{{cite web|first=Jukka|last=Korpela|url=http://www.cs.tut.fi/~jkorpela/qattr.html|title=Why attribute values should always be quoted in HTML|publisher=Cs.tut.fi |date=July 6, 1998|accessdate=November 16, 2008}}</ref>
 
Although most attributes are provided as paired names and values, some affect the element simply by their presence in the start tag of the element<ref name="tagshtml">{{cite web|url=http://www.w3.org/History/19921103-hypertext/hypertext/WWW/MarkUp/Tags.html |title=Tags used in HTML |publisher=World Wide Web Consortium |date=November 3, 1992 |accessdate=November 16, 2008}}</ref> (like the <code>ismap</code> attribute for the <code>img</code> element<ref>{{cite web|url=http://www.w3.org/TR/1999/REC-html401-19991224/struct/objects.html#adef-ismap|title=Objects, Images, and Applets in HTML documents |publisher=World Wide Web Consortium |date=December 24, 1999|accessdate=November 16, 2008}}</ref>).
Line 14:
 
* The <code>id</code> attribute provides a document-wide unique identifier for an element.<ref>However, multiple identifiers may apply to the same element; in particular an element may be inside another element, each having an identifier.</ref> This can be used as [[CSS selector]] to provide presentational properties, by browsers to focus attention on the specific element, or by scripts to alter the contents or presentation of an element. Appended to the URL of the page, the URL directly targets the specific element within the document, typically a sub-section of the page. For example, the ID "Attributes" in <code><nowiki>http://en.wikipedia.org/wiki/HTML#Attributes</nowiki></code>
* The <code>class</code> attribute provides a way of classifying similar elements. This can be used for [[semantics|semantic]] purposes, or for presentation purposes. Semantically, for example, classes are used in [[microformat]]s. Presentationally, for example, an HTML document might use the designation <code>class="notation"</code> to indicate that all elements with this class value are subordinate to the main text of the document. Such elements might be gathered together and presented as footnotes on a page instead of appearing in the place where they occur in the HTML source. Another presentation use would be as a [[CSS selector]].
* An author may use the <code>style</code> non-attributal codes presentational properties to a particular element. It is considered better practice to use an element’s <code>id</code> or <code>class</code> attributes to select the element with a stylesheet, though sometimes this can be too cumbersome for a simple and specific or ad hoc application of styled properties.
* The <code>title</code> attribute is used to attach subtextual explanation to an element. In most browsers this attribute is displayed as what is often referred to as a [[tooltip]].
Line 20:
The abbreviation element, <code>abbr</code>, can be used to demonstrate these various attributes:
 
:<source lang="html4strict"><abbr id="anId" class="aClass" style="color:blue;" title="Hypertext Markup Language">HTML</abbr></source>
 
This example displays as <span id="anId" class="aClass" style="color:blue;" title="Hypertext Markup Language">HTML</span>; in most browsers, pointing the cursor at the abbreviation should display the title text "Hypertext Markup Language."