二、css3基础

1. css文件在网页内的引入 方法

<link rel = “stylesheet” href = “”>

2. 选择器

  • 1.元素选择器: p div body h1 等,根据元素的标签名来

  • 2.Id 选择器:给元素分配一个id不能重复 以 #开头

    <style type="text/css">
              .red{
                  color: red;
              }
              /*复合选择器,通过标签+类选择器来完成特定的效果*/
              div.red{
                  font-size: 50px;
                  font-weight: 100;
              }
          </style>
      </head>
      <body>
          <div class="red">
              我是div
          </div>
          <p class="red"> 我是p</p>
      </body>
    
    
  • 3.类选择器:class=‘’ 以 . 开头

  • 4.*通配选择器:针对所有标签

  • 5.复合选择器(交集选择器):元素加类一起设置,如果有元素在,必须由元素选择器开头(可以是多个类选择器等)同时满足才能第一次

  • 6.分组选择器: a, h1 {} 选择多个选择器对应的元素(并集选择器)

  • 7.关系选择器: 有父子关系的选择器 ,多层级

    • div > span 子元素
    • div span 后代元素
    • p + span 兄弟节点 选择器 紧挨着的上一个和下一个的关系,最近的
    • p ~ span 选择下面所有的兄弟(后面的元素)
  • 8.属性选择器:元素内的属性 id 和class 除外

    语法 :

    • 元素[属性名] 选择含有指定属性的元素
    • 元素[属性名 = 属性值] 选择相应属性值的元素
    • 元素[属性名 ^ = 属性值] 选择属性值 以 指定值开头 的元素
    • 元素[属性名 $ = 属性值] 选择属性值 以 指定值结尾 的元素
    • 元素[属性名 *= 属性值] 选择属性值 中含有某值 的元素

    如p标签 中设置 title 属性显示的效果为 当鼠标放上去的时候显示为设置的内容

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            p[title]{
                color: red;
            }
        </style>
    </head>
    <body>
        <P title='555'>我是1</P>
        <P title='666'>我是2</P>
        <P>我是3</P>
        <P>我是4/P>
        <P>我是45</P>
    </body>
</html>

  • 9.伪类选择器

    以:加名字 表示

    伪类用来描述一个元素的特殊状态
    比如:第一个子元素、被点击的元素、鼠标移入的元素...

    伪类一般情况下都是使用 : 开头

    :first-child 第一个子元素

        <style type="text/css">
        
                ul >li:first-child{
                    color: blue;
                }
            </style>
    

    :last-child 最后 一个子元素

            ul >li:last-child{
                    color: red;
                }
    

    :nth-child() 选中第n个子元素
    特殊值:
    n 第n个 n的范围0到正无穷
    2n 或 even 表示选中偶数位的元素
    2n+1 或 odd 表示选中奇数位的元素

    ul >li:nth-child(4){
                    color: aqua;
                }
    

    以上这些伪类都是根据所有的子元素进行排序,不同的类型

    :first-of-type
    :last-of-type
    :nth-of-type()

    这几个伪类的功能和上述的类似,不同点是他们是在同类型元素中进行排序

    • :not() 否定伪类

      ul > li:not(:first-child){
                    color: red;
                }
      

    将符合条件的元素从选择器中去除

  • 10.伪元素选择器

    以 ::加名字 表示

  • 11.a 元素的伪类

    • :link 用来表示没访问过的链接(正常的链接)
    • :visited 用来表示访问过的链接
      由于隐私的原因,所以visited这个伪类只能修改链接的颜色
    • :hover 用来表示鼠标移入的状态
    • :active 用来表示鼠标点击
  • 12.伪元素选择器

    伪元素,表示页面中一些特殊的并不真实的存在的元素(特殊的位置)

    伪元素使用 :: 开头

    • ::first-letter 表示第一个字母

    • ::first-line 表示第一行

    • ::selection 表示选中的内容

    • ::before 元素的开始

    • ::after 元素的最后

      before 和 after 必须结合content属性来使用

       div::before{
                  content: '『';
               }
      
              div::after{
                  content: '』';
      

3. 样式的继承

样式的继承,我们为一个元素设置的样式同时也会应用到它的后代元素上

  • 继承是发生在祖先后后代之间的

  • 继承的设计是为了方便我们的开发

  • 利用继承我们可以将一些通用的样式统一设置到共同的祖先元素上

  • 这样只需设置一次即可让所有的元素都具有该样式

    注意:并不是所有的样式都会被继承:
    比如 背景相关的,布局相关等的这些样式都不会被继承。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>

        /* body{
            font-size: 12px;
        } */

        
        p{
            color: red;
            background-color: orange;
        }

        div{
            color: yellowgreen
        }
    
    </style>
</head>
<body>
    <p>
        我是一个p元素
        <span>我是p元素中的span</span>
    </p>

    <span>我是p元素外的span</span>

    <div>
        我是div
        <span>
            我是div中的span
            <em>我是span中的em</em>
   </span>
    </div>
</body>
</html>

4. 选择器的权重

样式的冲突

我们通过不同的选择器,选中相同的元素,并且为相同的样式设置不同的值时,此时就发生了样式的冲突

样式冲突的解决

发生样式冲突时,应用哪个样式由选择器的权重(优先级)决定

选择器的权重

​ 内联样式 1,0,0,0
​ id选择器 0,1,0,0
类和伪类选择器 0,0,1,0
​ 元素选择器 0,0,0,1
​ 通配选择器 0,0,0,0
​ 继承的样式 没有优先级

  • 比较优先级时,需要将所有的选择器的优先级进行相加计算,最后优先级越高,则越优先显示(分组选择器是单独计算的)
  • 选择器的累加不会超过其最大的数量级,类选择器再高也不会超过id选择器
  • 如果优先级计算后相同,此时则优先使用靠下的样式
  • 可以在某一个样式的后边添加 !important ,则此时该样式会获取到最高的优先级,甚至超过内联样式,注意:在开发中这个玩意一定要慎用!不到万不得已的时候不要用,因为一旦用了,想通过js改变样式也不行了
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>

        /* #box1{
            background-color: orange;
        }

        div#box1{
            background-color: yellow;
        } */

        .d1{
            background-color: purple !important;
        }
        .red{
            background-color: red;
            /* font-size: 20px; */
        }

        /* div,p,span{
            background-color:yellowgreen;
        } */

     
         *{
             font-size: 50px;
         }

         div{
             font-size: 20px;
         }

    </style>

</head>
<body>
    <div id="box1" class="red d1 d2 d3 d4" style="background-color: chocolate;">我是一个div <span>我是div中的span</span></div>
</body>
</html>

5.单位

长度单位:

  • 像素

    • 屏幕(显示器)实际上是由一个一个的小点点构成的

    • 不同屏幕的像素大小是不同的,像素越小的屏幕显示的效果越清晰

    • 所以同样的200px在不同的设备下显示效果不一样

  • 百分比

    • 也可以将属性值设置为相对于其父元素属性的百分比
    • 设置百分比可以使子元素跟随父元素的改变而改变
  • em

    • em是相对于元素的字体大小来计算的
    • 1em = 1font-size(当前设置字体 的大小)
    • em会根据字体大小的改变而改变
  • rem

    • rem是相对于根元素的字体大小来计算(html字体大小)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>

        html{
            font-size: 30px;
        }
        .box1{
            width:300px;
            height: 100px;
            background-color: orange;
        }

        .box2{
            width: 50%;
            height: 50%;
            background-color:aqua; 
        }
        .box3{
            font-size: 50px;
            /* width: 10em;
            height: 10em; */
            width: 10rem;
            height: 10rem;
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
    <div class="box3"></div>
</body>
</html>

6.颜色

颜色单位

在CSS中可以直接使用颜色名来设置各种颜色

比如:red、orange、yellow、blue、green ... ...
但是在css中直接使用颜色名是非常的不方便

  • RGB值:(常用)

    RGB通过三种颜色的不同浓度来调配出不同的颜色

    每一种颜色的范围在 0 - 255 (0% - 100%) 之间

    R red,G green ,B blue

    语法:RGB(红色,绿色,蓝色)

  • RGBA: (常用)

    就是在rgb的基础上增加了一个a表示不透明度

    需要四个值,前三个和rgb一样

    第四个表示不透明度:

    1表示完全不透明 0表示完全透明 .5半透明

  • 十六进制的RGB值:(常用)

    语法:#红色绿色蓝色

    颜色浓度通过 00-ff

    如果颜色两位两位重复可以进行简写
    #aabbcc --> #abc

  • HSL值 HSLA值

    H 色相(0 - 360)

    S 饱和度,颜色的浓度 0% - 100%
    L 亮度,颜色的亮度 0% - 100%

    A 透明度,1表示完全不透明 0表示完全透明 .5半透明

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box1{
            width: 100px;
            height: 100px;
          
            background-color: red;
            background-color: rgb(255, 0, 0);
            background-color: rgb(0, 255, 0);
            background-color: rgb(0, 0, 255);
            background-color: rgb(255,255,255);
            background-color: rgb(106,153,85);
            background-color: rgba(106,153,85,.5);
            background-color: #ff0000;
            background-color: #ffff00;
            background-color: #ff0;
            background-color: #bbffaa;
            background-color: #9CDCFE;
            background-color: rgb(254, 156, 156);
            background-color: hsla(98, 48%, 40%, 0.658);
        }
    </style>
</head>
<body>
    
    <div class="box1"></div>

</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容