一.标签选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> h1 { color: red; } h2{ color: green; } </style> </head> <body> <h1>Hello World!</h1> <h1>Hello World!</h1> <h2>Hello !</h2> <h2>Hello !</h2> </body> </html>
二.类选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { color: green; } .box1 { font-size: 26px; } </style> </head> <body> <div class="box box1">Hello World!</div> <span class="box">Hello World!</span> </body> </html>
三.ID选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #box{ color: green; } </style> </head> <body> <div id="box">Hello World!</div> </body> </html>
四.通配符选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; color: red; } </style> </head> <body> <div>Hello World</div> </body> </html>
五.后代选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> ul li { color: red; } ul li a { color: green; } .nav li { color: yellow; } </style> </head> <body> <ul> <li>ul li</li> <li>ul li</li> <li>ul li</li> <li>ul li</li> <li>ul li <a href="">a</a></li> </ul> <ol> <li>ol li</li> <li>ol li</li> <li>ol li</li> <li>ol li</li> <li>ol li</li> </ol> <ul class="nav"> <li>nav li </li> <li>nav li </li> <li>nav li </li> <li>nav li </li> <li>nav li </li> </ul> </body> </html>
六.子元素选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .nav>a { color: red; } </style> </head> <body> <div class="nav"> <a href="#">Hello World</a> <p> <a href="#">Hello World</a> </p> </div> </body> </html>