Style Tag in HTML5


The HTML <style> tag is used for declaring style sheets within your HTML document. Each HTML document can contain multiple <style> tags. The below code defines the style.
  • background-color: color name or code
  • background-image=url(imagename)
  • font-family: font name
  • font-size: number unit ->units can be px, pt, mm,cm, in
  • font-weight:bold | bolder
  • text-decoration:underline | none
  • position: absolute | relative
  • color: color name or code
  • left=x
  • top=y
  • cursor: hand | crosshair
When a browser reads a style sheet, it will format the document according to it.


HTML tags can contain one or more attributes. Attributes are added to a tag to provide the browser with more information about how the tag should appear or behave. Attributes consist of a name and a value separated by an equals (=) sign, with the value surrounded by double-quotes.
There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes. The attributes that you can add to this tag are listed below.

Element-Specific Attributes

The following table shows the attributes that are specific to this tag/element.
Attributes Introduced by HTML5
Attributes Description             
type Specifies the style sheetlanguage as a content-type (MIME type).
media Specifies the device thatthe styles apply to. It must be a valid media query.
Possible values:
scoped The scoped attributespecifies the style apply only for the style element's parent element and the parent element's children. Not the whole document.

Global Attributes

The following attributes are standard across all HTML 5 tags.
HTML5Global Attributes
accesskey draggable style
class hidden tabindex
dir spellcheck  
contenteditable id title
contextmenu lang  


Event Handler Content Attributes

Here are the standard HTML 5 event handler content attributes.
onabort onerror* onmousewheel
onblur* onfocus* onpause
oncanplay onformchange onplay
oncanplaythrough onforminput onplaying
onchange oninput onprogress
onclick oninvalid onratechange
oncontextmenu onkeydown onreadystatechange
ondblclick onkeypress onscroll
ondrag onkeyup onseeked
ondragend onload* onseeking
ondragenter onloadeddata onselect
ondragleave onloadedmetadata onshow
ondragover onloadstart onstalled
ondragstart onmousedown onsubmit
ondrop onmousemove onsuspend
ondurationchange onmouseout ontimeupdate
onemptied onmouseover onvolumechange
onended onmouseup onwaiting
Types of Cascading Style Sheets (CSS): There are three types of CSS.
  1. Inline CSS
  2. Internal CSS
  3. External CSS
Inline CSS: Inline CSS means applying the code directly on tags using the STYLE attribute.
For example:
  1. <body><form id="form1" runat="server"><div><p style="background-color:Black;color:Yellow">Hello</p><h1 style="font-size:1in;color:red">Hi</h1></div></form></body>    
Internal CSS: Internal CSS means applying the codes on the specified tags or create a named style that can be applied to any kind of tag. Such styles are created in <STYLE> tag under <HEAD> section. To create the named styles use. along with some name called as class name. Use CLASS attribute on tags to apply that class.
For example:
  1. <head runat="server">  
  2.     <title>Untitled Page</title>  
  3.     <style type="text/css">  
  4.         p {  
  5.   colorblue;  
  6.   font-size12pt;  
  7. }  
  8. A {  
  9.   text-decorationnone;  
  10. }  
  11. A:hover {  
  12.   colorred;  
  13. }  
  14. .btn {  
  15.   cursor: hand;  
  16. }  
  17.     </style>  
  18. </head>  
  19. <body>  
  20.     <form id="form1" runat="server">  
  21.     <div>  
  22. <p>%</p>  
  23.         <p>ass<span class="btn">aa</span>sfasfasdf</p>  
  24.         <a href="">  
  25.         asdf</a><br />  
  26.         <a href="">  
  27.         as</a><br />  
  28.         dfas<br />  
  29.         df<br />  
  30.         asf<br />  
  31. <input id="Button1" style="z-index: 100; left: 192px; position: absolute; top: 64px"  
  32. type="button" value="button" class="btn" onclick="return Button1_onclick()" />  
External CSS: An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the <head> section.
  1. <LINK href="filename.css" rel="stylesheet">add the following code in mylibrary.css file.body {  
  2.   background-color: Aqua;  
  3. }  
  4. .testing {  
  5.   font-familyArial;  
  6.   font-size16px;  
  7.   background-color#FF00FF;  
  8.   border-styledotted;  
  9. }  
  10. #first {  
  11. }  
  12. Now using file code. <html ><head ><title>Untitled Page</title><style type="text/css">#abc {  
  13.   color: Red;  
  14.   background-colorblue;  
  15. }  
  16. .xyz {  
  17.   color: Yellow;  
  18.   background-color: Black;  
  19. }</style><link href="MyLibrary.css" rel="Stylesheet" /> </head><body><form id="form1" runat="server"><div><h1 id="abc">Hello to all</h1><p class="xyz">Welcome to India</p><input type="button" value="Save Data" id="abc" /></div><asp:TextBox ID="TextBox1" runat="server" Class="xyz"></asp:TextBox></form><p class="testing">Hello</p></body></html>