CSS笔记3

文档流
文档中可显示对象在排列时所占用的位置/空间

标准文档流会产生的问题

  • 高矮不齐,底边对齐
  • 空格会有折叠(多个空格会变成一个)
  • 元素无间隙

脱离文档流

  1. 浮动
  2. 绝对定位
  3. 固定定位

浮动
原理:浮动后脱离了文档流,只有左右浮动,没有上下浮动,相当于在页面上增加了一个浮层来放置内容,可以理解为有两层页面,一层是底层的原来页面,另一侧是脱离文档的上层页面,所以会出现折叠现象。
float属性定于元素在哪个方向浮动,任何元素都可以浮动,放在浮动层里面的元素就不会有文档流上的问题

描述
left元素向左浮动
right元素向右浮动
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1{
            width: 200px;
            height: 200px;
            float: left;
            background-color:aqua;
        }
        #box2{
            width: 400px;
            height: 400px;
            background-color: brown;
        }
        img{
            height: 200px;
            width: 200px;
            float:left
        }
       
        
    </style>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div><img src="https://img.tukuppt.com/photo-big/00/00/94/6152bc0ce6e5d805.jpg" alt="">
        <img src="https://img.tukuppt.com/photo-big/00/00/94/6152bc0ce6e5d805.jpg" alt="">
    </div>
    
</body>
</html>

运行结果:
在这里插入图片描述
浮动可以横向摆放(并且用外边距让元素之间有边距)
当横向摆放的宽度不够时,会到下一行摆放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width:100px;
            height: 100px;
            float :left
        }
        .box1
        {
            background-color: aquamarine;
        }
        .box2
        {
            background-color: black;
        }
        .box3
        {
            background-color: red;
        }
        ul li{
            float:left;
            margin: 0 40px;
        }
    </style>
</head>
<body>

    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <ul>
        <li>导航1</li>
        <li>导航2</li>
        <li>导航3</li>
        <li>导航4</li>
    </ul>
</body>
</html>

运行结果:
在这里插入图片描述
清除浮动
浮动的副作用:

  1. 浮动元素会造成父元素高速塌陷
  2. 后续元素会受到影响

解决方法

  1. 父级元素设置高度
  2. 收影响的元素添加clear属性(有left和right和both的三个值)对子元素添加
  3. overflow清除浮动(在父级标签的样式里面加:overflow:hidden和clear:both)可以适应大小
  4. 伪对象方式在class后面加上::after 里面放,conten:”“ display:block,clear:both
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box0{
            width: 500px;
            background-color: chocolate;
            overflow: hidden;
            clear: both;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: aquamarine;
            float:left
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color:blue;
            float:left
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color:darkorange;
            float:left
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color: green;
            clear: both;
        }
    </style>
</head>
<body>
    <div class="box0"> 
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        
    </div>
    <div class="box4"></div>
   

    
</body>
</html>

运行结果:
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box0{
            width: 500px;
            background-color: chocolate;

        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: aquamarine;
            float:left
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color:blue;
            float:left
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color:darkorange;
            float:left
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color: green;
            
        }
        .box0::after{
            content: "";
            display: block;
            clear: both;
        }
    </style>
</head>
<body>
    <div class="box0"> 
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        
    </div>
    <div class="box4"></div>
   

    
</body>
</html>

运行结果与上面相同

定位

  • relative-相对定位
  • absolute-绝对定位
  • fixed-固定定位
    绝对对定位和固定定位会脱离文档流
    可以用left、top、right、bottom四个方向来调整

相对定位:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    div{
      width: 200px;
      height: 200px;
      background-color: red;
      position: relative;
      top:50px;
      left:100px;
    }
  </style>
  <title>Document</title>
</head>
<body>
  <div>

  </div>
</body>
</html>

运行结果:
在这里插入图片描述
绝对定位(会脱离文档流)
每设置一次都会添加一层,而浮动只有两层

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
      .box1{
      width: 200px;
      height: 200px;
      background-color: red;
      position:absolute;
      top:50px;
      left:100px;
    }
    .box2{
      width:300px;
      height: 300px;
      background-color: blue;
      
    }
  </style>
  <title>Document</title>
</head>
<body>
  <div class="box1">

  </div>
  <div class="box2">

  </div>
</body>
</html>

运行结果:
在这里插入图片描述
固定定位
无论页面如何滚动,该定位都不会动

相对定位和绝对定位是相对于具有定位的父级元素进行位置调制,若父级元素不存在定位则逐级向上,直到顶层文档,即如果子级元素有设置定位,而父级没有设置,子级的定位就会往上找,直到找到有定位的父级元素或者顶层

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
      .box1{
      width: 500px;
      height: 500px;
      background-color: red;
      position:relative ;
      top:50px;
      left:100px;
    }
    .box2{
      width:300px;
      height: 300px;
      background-color: blue;
      position:absolute;
      top:50px;
      right:50px;
    }
  </style>
  <title>Document</title>
</head>
<body>
  <div class="box1">

    <div class="box2"></div>
  </div>
  

  </div>
</body>
</html>

运行结果:
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
      .box1{
      width: 500px;
      height: 500px;
      background-color: red;
    
    }
    .box2{
      width:300px;
      height: 300px;
      background-color: blue;
      position:absolute;
      top:50px;
      right:50px;
    }
  </style>
  <title>Document</title>
</head>
<body>
  <div class="box1">

    <div class="box2"></div>
  </div>
  

  </div>
</body>
</html>

运行结果:
在这里插入图片描述
Z-index可以设置堆叠元素的顺序,有更高的顺序会在前面

圆角
通过border-radlus来设置

  • 四个值,分别为左上角右上右下和左下
  • 三个值,左上,右上角和左下角,右下角
  • 两个,左上和右下,右上和左下角
  • 一个,四个都一样
  • 50%/100%可以变成圆
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box1{
      background-color: red;
      width: 300px;
      height: 300px;
      border-radius: 30px;
    }
    .box2{
      background-color: blue;
      width: 300px;
      height: 300px;
      border-radius: 50%;
    }
  </style>
</head>
<body>
  
  <div class="box1">

  </div>
  <div class="box2">
  </div>
</body>
</html>

运行结果:
在这里插入图片描述

阴影

  • h-shadow-必选,水平方向的阴影位置
  • v-shadow-必选垂直阴影位置
  • blur-可选模糊距离
  • color-可选阴影颜色
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box1{
      background-color: red;
      width: 300px;
      height: 300px;
      border-radius: 30px;
      box-shadow:10px 10px ;
    }
    .box2{
      background-color: blue;
      width: 300px;
      height: 300px;
      border-radius: 50%;
      box-shadow: 10px 10px 10px rgba(0,0,0,0.5);
    }
  </style>
</head>
<body>
  
  <div class="box1">

  </div>
  <div class="box2">
  </div>
</body>
</html>

在这里插入图片描述
动画
动画是使元素从一种样式逐渐变化为另一种样式的效果,可以改变任意多的次数
用百分比来规定变化发生事件,或者用from和to(from和to一般只有一个变化过程)
@keyframes创建动画

animation执行动画
animation: name duration timing-function delay iteration-count direction
名字、持续时间、速度调整、速度开始的时间、播放次数、方向

描述
name设置动画的名称
duration设置动画持续的时间
timing-function设置动画效果的速率
delay设置动画开始的时间(延时执行)
iteration-count设置动画循环的次数,infinite为无限次循环
direction设置动画播放的方向
animation-play-state控制动画的播放状态:running代表播放,而paused代表停止播放
timing-function的值描述
ease逐渐变慢(默认)
linear匀速
ease-in加速
ease-out减速
ease-in-out先加速后减速
direction值描述
normal默认值为normal表示向前播放
alternate动画再第偶数次再向前播放,第奇数次向反方向播放

选择器:hover(鼠标划到这上面就可以执行接下来的命令)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div{
      width: 300px;
      height: 300px;
      background-color: red;
      animation: DH 3s linear 0s infinite;
    }
    div:hover
    {
      animation-play-state: paused;
    }
    @keyframes DH
    {
       0%{
        width:200px;
        background-color: red;
       }
       50%
       {
          width: 400px;
          background-color: blueviolet;
       }
       100%
       {
        width: 200px;
        background-color: green;
       }
    }
    
  </style>
</head>
<body>
  
  <div>

  </div>
</body>
</html>

呼吸图

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box
    {
      width: 500px;
      height: 500px;
      border-radius: 50%;
      box-shadow: 5px 5px 0 rgba(0, 0,0, 0.2);
      background-color: aqua;
      margin: auto;
      animation: DH 2.5s linear 0s infinite alternate;
    }
    
    @keyframes DH
    {
      0%
      {
        opacity: 0.2;
        background-color: aqua;
        width:500px;
      }
      25%
      {
        opacity: 0.5;
        background-color: aquamarine;
        width: 700px;
        height: 700px;
      }
      50%
      {
        opacity: 0.2;
        background-color: burlywood;
        width: 500px;
        height: 500px;
      }
      75%
      {
        opacity: 1;
        background-color: blue;
        width: 100px;
        height: 100px;
      }
      100%
      {
        opacity: 0.5;
        background-color: cornflowerblue;
        width: 100px;
        height: 100px;
      }
    }
  </style>
</head>
<body>
  
  <div class="box"></div>
</body>
</html>

媒体查询
可以让页面在不同终端上有不同的显示
使用mate标签(补全时候自带)会禁止内容在不同终端上面缩放,只显示其自身的大小

当设备不同时可以显示不同的页面

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box
    {
      width: 400px;
      height: 400px;
    }
      @media screen and (max-width: 768px)  
      {
        .box
        {
          background-color:red;
        }
        .box1
        {
          display: none;
        }
        .box2
        {
          display: none;
        }
        
        
      }
      @media screen and (max-width: 996px) and (min-width:768px) 
      {
        .box
        {
          background-color:blue;
        }
        .box1{
          display: block;
        }
        .box2
        {
          display:none;
        }
      }
      @media screen and (min-width: 996px)  
      {
        .box
        {
          background-color:black;
          
        }
        .box1
        {
          display: block;
        }
        .box2
        {
          display:block
        }
        
      }
    
  </style>
</head>
<body>
  
  <div class="box">
    
  </div>
  <p class="box1">hhhh</p>
  <p class="box2">oooo</p>
</body>
</html>
  • display: block为显示,变为块级元素
  • display:none为不显示
  • display:inline变为行内元素(内联元素)

上述代码在不同的终端显示的不一样

雪碧图
可以只导入一张图片,然后调整,用这张图片里面的不同部分

字体图标(需要下载获取,Iconfont)
优点:

  • 轻量化:加载速度块,减少http的请求
  • 灵活:可以用CSS设置大小颜色等等
  • 兼容性:网页字体支持所有现代浏览器
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值