HTML Tag Attributes

In HTML, tags can have their own attribures a key value pair inside element start tag. You can think of an attribute as a piece of code attached to a tag which supplies additional information. The attribute always consists of attribute name with equal sign (=) and the value of the attribute which is enclosed in quotes, even if the attribute is a boolean attribute.

Example

<p id="elementSection" align="left">Adding content 
of paragraph element.</p>

<img src="image.gif" alt="This attribute will 
display when mouse over on this image."/>

In HTML, attributes are very useful when you want to perform some operation with the help of javascript or apply some visual effects using CSS on HTML element. Let's assume that if you want to change text color when mouse over your paragraph or change the image size when mouse over the image. You can achieve this by attaching attribute to the elements.

Example

<!DOCTYPE html>
<html>
<TITLE>HTML Attribute Example Code</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function stext() {
        var x = document.getElementById("div1");
        var y = document.getElementById("div2");
        x.style.color = 'red';
        y.style.color = 'black';
    }

    function htext() {
        var x = document.getElementById("div1");
        var y = document.getElementById("div2");
        x.style.color = 'black';
        y.style.color = 'red';
    }
</SCRIPT>
</HEAD>

<body>
<span id="div1" style="color:black;" onmouseover="stext()" 
onmouseout="htext()">www.techstrikers.com</span><p />
<hr color="black" />
<span id="div2" style="color:red;" onmouseover="htext()" 
onmouseout="stext()">www.techstrikers.com</span> 

</body>
</html>

In above example "id" attribute is using javascript to access the element and perform text color operation on span element.

Try Now

HTML Tag Common Attributes List

The below table contains complete list of commomnly used attributes and their description which is supported by HTML version.

Attribute NameAttribute Description
IdThis events will encounter when page loads.
AltThis events will encounter when page loads.
ClassThis events will encounter when page unloads.
StyleThis events will encounter when page loads.
WidthThis events will encounter when page unloads.
HeightThis events will encounter when page loads.
AlignThis events will encounter when page unloads.
ValignThis events will encounter when page loads.
BgcolorThis events will encounter when page unloads.
BackgroundThis events will encounter when page loads.

HTML Id Attribute

This is very common attribute of HTML element to distinguish each element from others. In HTML web page, multiple element may have same name but id cannot it must be unique.

Example

//This is correct syntax
<p name="section" id="elementSection1" align="left">Adding content 
of paragraph element.</p>

//This is also correct syntax
<p name="section" id="elementSection2" align="left">Adding content 
of paragraph element.</p>

//This is incorrect syntax id must be unique
<p name="section" id="elementSection1" align="left">Adding content 
of paragraph element.</p>

HTML Alt Attribute

This is very common attribute of HTML element to display brief information about that element. Let's assume that you have web page that content list of "img" element and you want to display some brief information about each image when user mouse over on particular image.

Example

//This is correct syntax
<img src="image.gif" alt="This is image from US."/>

<img src="image.gif" alt="This is image from UK."/>

<img src="image.gif" alt="This is image from INDIA."/>

HTML Width and Height Attribute

This is very common attribute of HTML element to display set width and height for element. Let's assume that you have web page that contains a list of "img" elements and you want to display all images in equal width and height. In HTML, width and height can be set in "px" for example width="100px" or in percentage as width="100%".

Example

<img src="image.gif" alt="This is image from US." width="25px" height="55px"/>

<img src="image.gif" alt="This is image from UK." width="25%" height="55%"/>

See Also

HTML Form Tags
HTML Page Formating Tags
HTML Other Tags


 
 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.