Definition and usage of id selector:
Grammar structure one:
#myid{ Rules }
This grammar structure can select id is the element of myid.
Grammar structure two:
E#myid{ sRules }
This grammar structure can select the E element with the id myid.
Example code:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ask.php.cn/" /> <title>php中文网</title> <style type="text/css"> #mytest{ color:blue; } </style> </head> <body> <div> <ul> <li id="mytest">php中文网欢迎您</li> <li>php中文网</li> </ul> <p id="mytest">php中文网欢迎您</p> </div> </body> </html>
The code can set the text color of the element with the id mytest to blue.
Special Note: The id should be unique in the entire html document, but in the document below there are two elements with the same id, but the effect we want is still achieved, but in our actual operation We must put an end to this phenomenon.
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ask.php.cn/" /> <title>php中文网</title> <style type="text/css"> li#mytest{ color:blue; } </style> </head> <body> <div> <ul> <li id="mytest">php中文网欢迎您</li> <li>php中文网</li> </ul> <p>php中文网欢迎您</p> </div> </body> </html>
The code can set the text color in the li element with the id mytest to blue.