前端----css 选择器

 

css 为了修饰页面作用, 让页面好看

⑴ css的引入方式
1,行内样式
body里面
2,内接样式
在html里面的 style 里面
3,外接样式
两种:①链接式: <link rel="stylesheet" href="./index.css">
②导入式:<style type="text/css">
@import url('./index.css');
</style>

优先级:
行内优先级(最高) > 内接的优先级 > 外接优先级(引入)

id 的优先级 比 class 的优先级要高

⑵ 基础选择器
id 选择器:
#app

类选择器:
.app (它选择的是共性)

标签选择器:
div...


text -decoration :underline 下划线

字体大小只有偶数 没有奇数



高级选择器 高级选择器分为:后代选择器、子代选择器、并集选择器、交集选择器 后代选择器 使用空格表示后代选择器。顾名思义,父元素的后代(包括儿子,孙子,重孙子) 1 .container p{ 2 color: red; 3 } 4 .container .item p{ 5 color: yellow; 6 } 子代选择器 使用>表示子代选择器。比如div>p,仅仅表示的是当前div元素选中的子代(不包含孙子....)元素p。 1 .container>p{ 2 color: yellowgreen; 3 } 并集选择器 多个选择器之间使用逗号隔开。表示选中的页面中的多个标签。一些共性的元素,可以使用并集选择器 1 /*并集选择器*/ 2 h3,a{ 3 color: #008000; 4 text-decoration: none; 5 6 } 比如像百度首页使用并集选择器。 body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td { margin: 0; padding: 0 } /*使用此并集选择器选中页面中所有的标签,页面布局的时候会使用*/ 交集选择器 使用.表示交集选择器。第一个标签必须是标签选择器,第二个标签必须是类选择器 语法:div.active 比如有一个<h4 class='active'></h4>这样的标签。 那么 1 h4{ 2 100px; 3 font-size: 14px; 4 } 5 .active{ 6 color: red; 7 text-decoration: underline; 8 } 9 /* 交集选择器 */ 10 h4.active{ 11 background: #00BFFF; 12 } 它表示两者选中之后元素共有的特性。 04-属性选择器 属性选择器,字面意思就是根据标签中的属性,选中当前的标签。 语法: 1 /*根据属性查找*/ 2 /*[for]{ 3 color: red; 4 }*/ 5 6 /*找到for属性的等于username的元素 字体颜色设为红色*/ 7 /*[for='username']{ 8 color: yellow; 9 }*/ 10 11 /*以....开头 ^*/ 12 /*[for^='user']{ 13 color: #008000; 14 }*/ 15 16 /*以....结尾 $*/ 17 /*[for$='vvip']{ 18 color: red; 19 }*/ 20 21 /*包含某元素的标签*/ 22 /*[for*="vip"]{ 23 color: #00BFFF; 24 }*/ 25 26 /**/ 27 28 /*指定单词的属性*/ 29 label[for~='user1']{ 30 color: red; 31 } 32 33 input[type='text']{ 34 background: red; 35 }

伪类选择器

伪类选择器一般会用在超链接a标签' 我们一定要遵循"爱恨准则"  LoVe HAte 

 1               /*没有被访问的a标签的样式*/
 2         .box ul li.item1 a:link{
 3             
 4             color: #666;
 5         }
 6         /*访问过后的a标签的样式*/
 7         .box ul li.item2 a:visited{
 8             
 9             color: yellow;
10         }
11         /*鼠标悬停时a标签的样式*/
12         .box ul li.item3 a:hover{
13             
14             color: green;
15         }
16         /*鼠标摁住的时候a标签的样式*/
17         .box ul li.item4 a:active{
18             
19             color: yellowgreen;
20         }
View Code

再给大家介绍一种css3的选择器nth-child()

/*选中第一个元素*/
        div ul li:first-child{
            font-size: 20px;
            color: red;
        }
        /*选中最后一个元素*/
        div ul li:last-child{
            font-size: 20px;
            color: yellow;
        }
        
        /*选中当前指定的元素  数值从1开始*/
        div ul li:nth-child(3){
            font-size: 30px;
            color: purple;
        }
        
        /*n表示选中所有,这里面必须是n, 从0开始的  0的时候表示没有选中*/
        div ul li:nth-child(n){
            font-size: 40px;
            color: red;
        }
        
        /*偶数*/
        div ul li:nth-child(2n){
            font-size: 50px;
            color: gold;
        }
        /*奇数*/
        div ul li:nth-child(2n-1){
            font-size: 50px;
            color: yellow;
        }
        /*隔几换色  隔行换色
             隔4换色 就是5n+1,隔3换色就是4n+1
            */
        
        div ul li:nth-child(5n+1){
            font-size: 50px;
            color: red;
        }
View Code

伪元素选择器

 

废话不多说,直接上代码!!!

复制代码
/*设置第一个首字母的样式*/
        p:first-letter{
            color: red;
            font-size: 30px;

        }
        
/* 在....之前 添加内容   这个属性使用不是很频繁 了解  使用此伪元素选择器一定要结合content属性*/
        p:before{
            content:'alex';
        }
        
        
/*在....之后 添加内容,使用非常频繁 通常与咱们后面要讲到布局 有很大的关联(清除浮动)*/
        p:after{
            content:'&';
            color: red;
            font-size: 40px;

原文地址:https://www.cnblogs.com/liuafan/p/9442949.html