HTML5 Tags and Attributes
Basics of HTML 5 - Part 3
Foreword: In this part of the series, I talk about HTML5 tags and attributes.
By: Chrysanthus Date Published: 3 Apr 2015
Single Tag Elements and Double Tag Elements
An element is made up of either a single tag or a pair of tags. When it is of a double tag, content goes in between (the tags). You should have seen examples of these in the previous parts of the series.
When you have a Double Tag Element to type, you can type the pair of tags on one line in your text editor or on two lines. The following two sets of tags are equivalent:
is equivalent to,
It is good to type a single tag element on one line. However, if the tag is too long, you can type it on two or several lines; do not break the words in the tag.
Consider the following tags:
The first one is for the Body element. The second one is for the Paragraph element. These tags can be nested as follows:
<body><p> </p> </body>
The following is the same as above:
What we have above is logical because a paragraph can only be content of the Body element. Single tag elements will always find themselves inside a double tag element.
Now, in HTML5, tags must be properly nested. This means that the following is correct:
<body><p> </p> </body> … right
while the following is wrong:
<body><p> </body></p> … wrong
In the first case, the two paragraph tags are inside the Body element; that is correct. In the second case, one of the paragraph tags is inside the body element and the other is outside it; that is wrong.
The following code illustrates how you can nest tags with contents
body content can go here
paragraph content can go here
body content again can go here
Consider the following Body element:
The content of the Body element is “Hello World”. Now it is possible for you to make your web page appear red, in the browser. To do this, modify the first tag of the body element as follows:
What we have added to the first tag is, bgcolor="red". You have the text, “bgcolor” followed by an equal sign and then “red” in quotes. With the simple web page, this is what you have:
You can try the resulting simple web page code. If you do that your page will be red at the browser.
What we have added in the first tag of the body element is an attribute.
The attribute we have above is:
“bgcolor” is the actual attribute; “red” is the value. Many attributes exist as attribute/value pairs. Note that some attributes do not have values; you will see some of these later.
Note: you will learn a more modern way of giving the background color later on. In this part of the series, the emphasis is to understand attributes and use of tags.
A Boolean attribute indicates that something is true or false. Do not worry about the meaning of that for now. What concerns us here is that a Boolean attribute does not have a value. So if the above tag were a Boolean attribute, all you would have seen in the tag would have been “bgcolor”; with no equal sign and no value.
Position of Attributes
You type your attributes in the start tag of a double tag element. Single tag elements have their attributes within their opening and closing angle brackets.
You can name a tag by the word or text that appears just after the open angle brackets (start tag). The Body element has “body” just after the open angle bracket, as in <body>; the Horizontal Rule element has “hr” just after the open angle bracket as in <hr>.
Rules about Tags
A tag begins with an open angle bracket and ends with a close angle bracket.
All tag names should be in small letters.
Never omit the end tag of a double tag element.
Number of Attributes in the Tag
You can have as many attributes in a tag as the attributes are relevant. The attributes should be separated by spaces (spacebar). Space on either side of the equal sign is optional.
Some Rules about Attributes
Attribute values should be quoted.
Double or single quotes are allowed.
Attribute names should be in small letters
Space is allowed on either or both sides of the equal sign.
The following examples are Okay:
bgcolor = "red"
Formal Definition of an Attribute
An attribute is a property (characteristic) of an element.
In this article, you have seen the meaning of tags and attributes. We note that tag names and attribute names should be in small letters and the end tag of a double tag element should not be omitted. In the next part of the series, you will see interesting elements, under the topic of HTML Text Formatting Basics.
See you then.
Related LinksBasics of HTML 5
Basics of ECMAScript
HTML DOM Basics
Text Elements in HTML
Microsyntax Dates and Times in HTML
Common Idioms without Dedicated Elements
HTML Embedded Content
HTML Insecurities and Prevention
Presentation Mathematical Markup Language
More Related Links
PurePerl MySQL API
Major in Website Design
Perl Course - Optimized
Web Development Course