HTML Attributes
HTML Attributes
COMPUTER ELECTIVE 7
HTML ATTRIBUTES
•HTML attributes provide
additional information
about HTML elements.
HTML ATTRIBUTES
• HTML Attributes
• All HTML elements can have attributes
• Attributes provide additional information about
elements
• Attributes are always specified in the start tag
• Attributes usually come in name/value pairs like:
name="value"
THE HREF ATTRIBUTE
• The <a> tag defines a hyperlink. The href attribute
specifies the URL of the page the link goes to:
• Example
<a href="https://www.w3schools.com">Visit
W3Schools</a>
THE SRC ATTRIBUTE
• Example
<img src="img_girl.jpg">
TWO WAYS TO SPECIFY THE URL IN THE SRC
ATTRIBUTE:
• 1. Absolute URL - Links to an external image that is
hosted on another website. Example:
src="https://www.w3schools.com/images/img_girl.jpg".
• Tip: It is almost always best to use relative URLs. They will not break
if you change domain.
THE WIDTH AND HEIGHT ATTRIBUTES
• Example
<img src="img_girl.jpg" width="500" height="600">
THE ALT ATTRIBUTE
• Example
• Example
<p style="color:red;">This is a red
paragraph.</p>
THE LANG ATTRIBUTE
• You should always include the lang attribute inside the <html>
tag, to declare the language of the Web page. This is meant to
assist search engines and browsers.
• The following example specifies English as the language:
<!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>
THE LANG ATTRIBUTE
• Country codes can also be added to the language code in the
lang attribute. So, the first two characters define the language of
the HTML page, and the last two characters define the country.
• The following example specifies English as the language and United States as the country:
• <!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
THE TITLE ATTRIBUTE
• The title attribute (and all other attributes) can be written with
uppercase or lowercase like title or TITLE.
• The HTML standard does not require quotes around attribute values.
• However, W3C recommends quotes in HTML, and demands quotes for
stricter document types like XHTML.
• Good:
<a href="https://www.w3schools.com/html/">Visit our HTML
tutorial</a>
• Bad:
<a href=https://www.w3schools.com/html/>Visit our HTML tutorial</a>
SINGLE OR DOUBLE QUOTES?
• Pointers:
1. Introduction to HTML
2. HTML Elements
3. HTML Attributes