ATTRIBUTES: ADDITIONAL INFORMATION ABOUT AN ELEMENT
Sometimes we have HTML elements that need to have more defined information about them. Enter "Attributes". Think of an HTML attribute like an adjective or an adverb. Adjectives and adverbs describe the noun/verb. It gives additional information. Attributes work in a very similar way. They give additional information about the element.
Attributes are always specified in the start tag.
Please take a look at the two photos below:
(a) |
(b) |
In the first picture (a), you see the HTML code for the web site. If you look at the <h1> tag (located under the <body> tag), you see that it is the element tag for a header. The contents in between the opening and closing <h1> tags says "Hello World!".
The picture (b) it shows how the website renders. "HELLO WORLD!" is in a large font (because it is a header) in the default text color - black.
Now what if we wanted to change that font color to red? We would need to put a style attribute in the starting <h1> element tag. We then need to assign a "value" to that attribute. In this case, because we want the font color to be red, the "value" will be: "color: red;" .
Please see the following two pictures below and pay close attention to the <h1> starting tag.
(c) |
(d) |
Do you notice the inline style attribute in the <h1> starting tag in picture (c)? This is telling the text editor that we are going to be using the "style" name attribute to style the contents within the header element tags (<h1></h1>). The next thing you see is the "=" sign. That is because we are going to be assigning a value to the attribute. In the above case, we need to state that the value is "color: red;" . The "color" property defines the text color to be used. It is followed by a colon and then a space. Next the color "red" is stated to define the color property. Finally, a semicolon is used to close out this css statement.
Please note: Double quotes around attribute values are the most common in HTML.
In picture (d), you see the rendered version: "Hello World!" in red.
********* You may be wondering why the text editor has different colors for the code**********
Syntax Highlighting is a feature that many text editors have to improve the readability for the users. It allows the user to be able to read the code faster and more efficiently, so that they can find what they are looking for. I used Sublime Text Editor for this example.
href = this attribute is for link addresses.
(e) |
(f) *Please note that this is just a picture so the link will not actually work |
Notice in picture (e) the href code for the link attribute. Following that you see the value of the attribute - in this case it is the link address "w3schools.com" . There is another attribute following the href attribute... it is the target attribute. This attribute specifies where to open a linked document. In this case, it is in a new window or tab. As you can see, more than one attribute can be assigned to the starting element tag.
Picture (f) shows you the rendered web page.
Click here for additional attributes and how to use them for your web pages!
Follow me as I learn to build my website bit by bit! IronTreeDev.com
Photo by Philip Strong on Unsplash
Comments
Post a Comment