3 组合选择器
页面元素比较复杂,存在多个嵌套。为了更加灵活选择页面中的元素,CSS中还提供了组合选择器。组合选择器就是将多个基本选择器通过一定的规则连接起来组成一个复杂选择器。
后代子代选择器
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| |
| <style> |
| |
| .c1 .c2{ |
| color: red; |
| } |
| |
| .c3 .c5{ |
| color: red; |
| } |
| .c3 > .c5{ |
| color: red; |
| } |
| .c3 .c4 .c5{ |
| color: red; |
| } |
| |
| </style> |
| </head> |
| <body> |
| |
| |
| <div class="c1"> |
| <div class="c2">item1</div> |
| </div> |
| <div class="c2">item2</div> |
| |
| |
| <div class="c3"> |
| <div class="c4"> |
| <div class="c5">item3</div> |
| </div> |
| <div class="c5">item4</div> |
| </div> |
| |
| </body> |
| </html> |
与或选择器
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| |
| <style> |
| |
| p.c1{ |
| color: red; |
| } |
| |
| p.c1,#i1{ |
| color: red; |
| } |
| |
| </style> |
| </head> |
| <body> |
| |
| |
| <div class="c1">item1</div> |
| <p class="c1">item2</p> |
| <div>item3</div> |
| <p id="i1">item4</p> |
| |
| </body> |
| </html> |
兄弟选择器
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| |
| <style> |
| |
| #i1 + div.c1{ |
| color: red; |
| } |
| #i1 + div.c2{ |
| color: red; |
| } |
| |
| |
| #i1 ~ div.c2{ |
| color: red; |
| } |
| #i1 ~ div{ |
| color: red; |
| } |
| |
| </style> |
| </head> |
| <body> |
| |
| <p id="i1">item0</p> |
| <div class="c1">item1</div> |
| <div class="c2">item2</div> |
| <div class="c3">item3</div> |
| <div class="c4">item4</div> |
| |
| </body> |
| </html> |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>css组合选择器</title> |
| |
| <style> |
| |
| |
| |
| |
| |
| .c1 > .c3{ |
| color: chartreuse; |
| } |
| |
| |
| .c1 .c2 .c3{ |
| color: gold; |
| } |
| </style> |
| |
| </head> |
| |
| <body> |
| <div class="c1"> |
| <div class="c2"> |
| <div class="c3">item1</div> |
| </div> |
| </div> |
| |
| <div class="c1"> |
| <div class="c3">item2</div> |
| </div> |
| </body> |
| </html> |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>css组合选择器2</title> |
| |
| <style> |
| p.c1, #i1{ |
| color: red; |
| background-color: darkseagreen; |
| font-size: 30px; |
| } |
| |
| </style> |
| |
| </head> |
| <body> |
| |
| <div class="c1">item1</div> |
| <p class="c1">item2</p> |
| <div>item3</div> |
| |
| <p id="i1">item4</p> |
| |
| </body> |
| </html> |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>css组合选择器3</title> |
| |
| <style> |
| |
| |
| |
| |
| |
| |
| .c2 ~ div{ |
| color: gold; |
| } |
| </style> |
| |
| </head> |
| <body> |
| |
| <div class="c1">item1</div> |
| <div class="c2">item2</div> |
| <div class="c3">item3</div> |
| <div class="c4">item4</div> |
| <div class="c5">item5</div> |
| <p>item6</p> |
| |
| </body> |
| </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步