博客列表 >CSS选择器学习笔记

CSS选择器学习笔记

贰杰
贰杰原创
2020年04月07日 01:39:49743浏览

一、CSS选择器

简单选择器

1.分类

分类说明比例
标签选择器普通标签<h2></h2>;使用h2{}
属性选择器通过属性选择grand=”one”;使用方法[grand=”one”]
类选择器通过class定义class=”top”;.top{}
id选择器通过id来定义id=”foot”;#{}

2.举例

  • 九宫格

<!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>        .container{            width: 300px;            height: 300px;            display: grid;            grid-template-columns:repeat(3,1fr);            gap: 5px;        }        .item{            font-size: 2em;            background-color: lightskyblue;            display: flex;            justify-content: center;             align-items: center;        }        body{            background-color:lightcyan;        }        /* .item.center{background-color: lightgreen;} */        .center{background-color: lightgreen;}        #first {            background-color:rgb(0, 255, 0);        }        .item#first{background-color: yellow;}        div#first{background-color: red;}         .item[title="hello"]{background-color:lime;}           </style>  </head>  <body><div class="container">    <div class="item" id="first">1</div>    <div class="item">2</div>    <div class="item">3</div>    <div class="item">4</div>    <div class="item center">5</div>    <div class="item" title="hello">6</div>    <div class="item">7</div>    <div class="item">8</div>    <div class="item">9</div></div></div>    </div>  </body></html>

二、群组选择器

  • h2,.top,#footer{}

三、上下文选择器

序号分类说明
1后代选择器最上层选择器
2子选择器>
3同组相邻选择器+
4同级所有选择器~

代码举例

<!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>        .container{            width: 300px;            height: 300px;            display: grid;            grid-template-columns:repeat(3,1fr);            gap: 5px;        }        .item{            font-size: 2em;            background-color: lightskyblue;            display: flex;            justify-content: center;             align-items: center;        }        /* body div{background-color: yellow;} */        /* body div{border: 1px solid #000;} */        /* body>div{border: 1px solid #000;} */        /* .item.center+div{border: 1px solid #000;} */        .item.center~div{border: 1px solid #000;}           </style>  </head>  <body><div class="container">    <div class="item">1</div>    <div class="item">2</div>    <div class="item">3</div>    <div class="item">4</div>    <div class="item center">5</div>    <div class="item">6</div>    <div class="item">7</div>    <div class="item">8</div>    <div class="item">9</div></div></div>    </div>  </body></html>

四、伪类选择器

1、代码举例

<!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>        .container{            width: 300px;            height: 300px;            display: grid;            grid-template-columns:repeat(3,1fr);            gap: 5px;        }        .item{            font-size: 2em;            background-color: lightskyblue;            display: flex;            justify-content: center;             align-items: center;        }       /* .container :first-child{background-color:yellow;} */       /* .container :last-child{         background-color:red;         } */         /* .container>:nth-child(1){background-color: #fff;} */         /* .container>:nth-child(2n){background-color: blue;}         .container>:nth-child(2n-1){background-color:yellow;} */         /* .container>:nth-child(even){background-color: blue;}         .container>:nth-child(odd){background-color:yellow;} */         .container>:nth-child(-n+3){background-color: #fff;}         .container>:nth-child(n+5){background-color: #fff;}           </style>  </head>  <body><div class="container">    <div class="item">1</div>    <div class="item">2</div>    <div class="item">3</div>    <div class="item">4</div>    <div class="item">5</div>    <div class="item">6</div>    <div class="item">7</div>    <div class="item">8</div>    <div class="item">9</div></div></div>    </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>        .container{            width: 300px;            height: 300px;            display: grid;            grid-template-columns:repeat(3,1fr);            gap: 5px;        }        .item{            font-size: 2em;            background-color: lightskyblue;            display: flex;            justify-content: center;             align-items: center;        }        .container span:nth-of-type(2n){background-color: #fff;}         </style>  </head>  <body><span class="container">    <div class="item">1</div>    <div class="item">2</div>    <div class="item">3</div>    <div class="item">4</div>    <span class="item">5</span>    <span class="item">6</span>    <span class="item">7</span>    <span class="item">8</span>    <span class="item">9</span></div></div>    </div>  </body></html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议