The CSS id selector matches element having a specific id attribute This can be used to specify CSS style based on element id. Note that id should be unique and no two elements should have same id.
Here is the syntax for defining style using id selector.
#id1 { style properties } /*element having id id1*/ div#id1 { style properties } /*div element having id id1*/
Example – style using id selector
<style> /* div#id1 can also be used to be more specific */ #id1 { width: 100px; height:100px; background-color: lightgreen; } </style> <div id="id1"> Hello world </div>
Related
- CSS type selectors - define style by element name
- CSS universal selector
- CSS attribute presence and value selectors
- CSS attribute substring selectors
- CSS class selectors - define style by class name
- CSS - style for element having two classes (both)