CSS Selector - How to use CSS id Selector #id








The #id selector adds style the element with the specified id.

An ID selector uses "hash notation" to select elements that have an ID containing a specific value.

The name of the ID value must immediately follow the octothorpe (#).

element1#idname

If no element name precedes the #, then the selector matches all elements containing that ID value.

ID is usually unique in a HTML document.

Examples:

h1#page-title {font-size: 250%;}
body#home {background: silver;}
#example  {background: lime;}




Summary

CSS Version
1

CSS Syntax

#id { 
   style properties 
}

Browser compatibility

#id Yes Yes Yes Yes Yes

Example

<!DOCTYPE html>
<html>
<head>
<style>
#grayStyle<!--from  ww  w .  ja  v a2  s . c  o  m-->
{
    background-color:#eee;
}
</style>
</head>
<body>
   <p id="grayStyle">java2s.com.</p>
</body>
</html>

Click to view the demo