首页 > 其他分享 >11-CSS定位

11-CSS定位

时间:2024-06-11 20:21:42浏览次数:13  
标签:11 定位 color 元素 box background position Document CSS

01 CSS定位概念理解

01 标准流布局概念的理解

image

02 position属性

image

02 相对定位

依然在标准流中
应用场景: 在不影响其它元素的情况下,对当前元素进行微调

<!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>
      .text {
        /* 相对定位: 相对元素自己原来的位置 */
        position: relative;
        left: 30px;
        top: 50px;
      }
    </style>
  </head>
  <body>
    <span>span元素</span>
    <strong class="text">strong元素</strong>
    <img src="../images/gouwujie01.jpg" alt="">
    <div>div元素</div>
  </body>
</html>
image

2.1 案例1

image
<!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>
    span {
      position: relative;
      font-size: 10px;
      top: -8px;
    }
  </style>
</head>
<body>
  <div>
    3<span>2</span> + 2<span>3</span> = 17
  </div>
</body>
</html>

2.2 案例2

梦幻西游使用背景的方法让图片的重要内容始终居中

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      margin: 0;
      padding: 0;
    }

    .box {
      height: 489px;
      overflow: hidden;
    }

    .box img {
      position: relative;
      /* 先把图片移到最左边 */
      left: -960px;
      /* 再把图片移动包含块的一半{marigin-left的百分比是相对于包含块的} */
      margin-left: 50%;
    }
  </style>
</head>
<body>
  <div class="box">
    <img src="./imgs/mhxy.jpg" alt="">
  </div>
</body>
</html>
image 但是这种方案图片向左移的距离写死了,如果图片的宽度发生了变化,还需要我们去手动修改这是不方便的 修改代码 ```html Document ```

03 固定定位

脱离标准流(相对于视口即可见区域)
使用固定定位前

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <span>span元素</span>
    <img src="./imgs/gouwujie01.jpg" alt="">
    <strong>strong元素</strong>
</body>
</html>
image

使用固定定位后(相当于把原来的元素拿走了,自己再决定放在哪里[用left/right/top/bottom等属性来决定])

<!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>
    strong {
      position: fixed;
    }
  </style>
</head>
<body>
  <span>span元素</span>
    <img src="./imgs/gouwujie01.jpg" alt="">
    <strong>strong元素</strong>
</body>
</html>
image

3.1 案例1

image
<!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>
    .handle {
      position: fixed;
      right: 30px;
      bottom: 30px;
      
    }

    .handle .item {
      text-align: center;
      width: 80px;
      height: 40px;
      color: #fff;
      line-height: 40px;
      background-color: brown;
      border-radius: 8px;
      cursor: pointer;
    }

    .handle .item:hover {
      background-color: red;
    }

    .handle .top {
      margin-bottom: 5px;
    }
  </style>
</head>
<body>
  <div class="handle">
    <div class="item top">顶部</div>
    <div class="item bottom">底部</div>
  </div>
</body>
</html>

04-绝对定位

脱离标准流(参考的是最临近的定位祖先元素如果没有找到这样的祖先元素,参考的是视口)

4.1 基本使用

image-20240610011423442

<!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>
    .box .item1 {
      position: relative;
      width: 400px;
      height: 400px;
      background-color: green;
    }

    .box .item1 > .item2 {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 200px;
      height: 200px;
      background-color: red;
    }

    .box .item1 > .item2 strong {
      position: absolute;
      left: 0;
      bottom: 0;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="item1">
      <div class="item2">
        <strong>strong元素</strong>
      </div>
    </div>
  </div>
</body>
</html>

05-绝对定位

脱离标准流(参考的是最临近的定位祖先元素如果没有找到这样的祖先元素,参考的是视口)

5.1 基本使用

image-20240611200640815

<!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>
    .box .item1 {
      position: relative;
      width: 400px;
      height: 400px;
      background-color: green;
    }

    .box .item1 > .item2 {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 200px;
      height: 200px;
      background-color: red;
    }

    .box .item1 > .item2 strong {
      position: absolute;
      left: 0;
      bottom: 0;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="item1">
      <div class="item2">
        <strong>strong元素</strong>
      </div>
    </div>
  </div>
</body>
</html>

06-position为absolute或fixed的元素的特点

image-20240611200856352

代码案例

<!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 {
      background-color: red;
    }
    /* 可以看到box这个元素直接在页面消失了,所以当设置元素为absolute它就不会向父元素汇报自己的宽度和高度了 */
    .box .container {
      position: absolute;
    }    
  </style>
</head>
<body>
  <div class="box">
    <div class="container">div元素</div>
  </div>
</body>
</html>

07-绝对定位让元素水平居中

image-20240611200948679

<!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 {
      position: relative;
      width: 800px;
      height: 300px;
      background-color: red;
    }
    .container {
      position: absolute;
      width: 200px;
      height: 100px;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      background-color: green;
    }
  </style>
</head>
<body>
  <!-- 水平方向的公式: red box的宽度=绿色盒子的宽度+left+right+margin left + margin right -->
  <div class="box">
    <div class="container"></div>
  </div>
</body>
</html>

image-20240611201025024

08-绝对定位让元素垂直居中

image-20240611201052113

<!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 {
      position: relative;
      width: 800px;
      height: 300px;
      background-color: green;
    }
    .box {
      position: absolute;
      width: 200px;
      height: 100px;
      background-color: red;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="box"></div>
  </div>
</body>
</html>

image-20240611201119217

09-粘性定位

image-20240611201143795

代码示例

image-20240611201214221

10-z-index

image-20240611201243967

标签:11,定位,color,元素,box,background,position,Document,CSS
From: https://www.cnblogs.com/yufc/p/18242652

相关文章

  • day11
    今日day11:三种二叉树的遍历方式1首先是递归遍历。需要首先搞懂前序中序后序遍历的意思,即以根root的位置为准前序即根左右中序即左根右后序即左右根递归则是指在函数中循环调用自身直至跳出递归条件python实现原理仅有遍历顺序的变化为区别,首先声明一个空res数组用以存放数......
  • 大学生HTML期末大作业——HTML+CSS+JavaScript美食网站(零食)
    HTML+CSS+JS【美食网站】网页设计期末课程大作业web前端开发技术web课程设计网页规划与设计......
  • 软著申请之-2024.6.11下证-铺就职称路,软著是基石
    软件著作权(软著)在职称评审过程中扮演着举足轻重的角色,它不仅是科研能力和创新意识的直接体现,也是评价个人专业技术水平和工作业绩的重要依据。首先,软著作为官方认可的知识产权成果,能够有力证明申报人在相关技术领域的研究深度和创新贡献,是评价科研能力和学术水平不可或缺的凭证......
  • 大学生HTML期末大作业——HTML+CSS+JavaScript购物商城(华为手机)
    HTML+CSS+JS【购物商城】网页设计期末课程大作业web前端开发技术web课程设计网页规划与设计......
  • 2024年6月11日Arxiv大语言模型相关论文
    cs.CL:在Token经济中的推理:大语言模型推理策略的预算感知评估原标题:ReasoninginTokenEconomies:Budget-AwareEvaluationofLLMReasoningStrategies作者:JunlinWang,SiddharthaJain,DejiaoZhang,BaishakhiRay,VarunKumar,BenAthiwaratkun摘要:......
  • 会声会影启动报错:找不到mfc110.dll文件的全面解决策略
    在使用会声会影进行视频编辑时,遇到“丢失mfc110.dll”错误提示,无疑会给创作过程带来不小的困扰。这个错误通常意味着系统中缺少了一个重要的动态链接库文件,而mfc110.dll是MicrosoftVisualC++RedistributablePackage的一部分,对于运行许多基于VisualC++开发的应用程序至关重......
  • 云原生周刊:Kubernetes 十周年 | 2024.6.11
    开源项目推荐KubernetesGoatKubernetesGoat是一个故意设计成有漏洞的Kubernetes集群环境,旨在通过交互式实践场地来学习并练习Kubernetes安全性。kube-state-metrics(KSM)kube-state-metrics是一个用于收集Kubernetes集群状态信息的开源项目,它能够提供各种有用的指......
  • 中端 20 纳米 FPGA:10AX115N4F45E3LG、10AX115N2F45I2LG、10AX115N2F45I1SG、10AX115N1
    Arria®10器件系列包括高性能,低功耗的20nm中端FPGA和SoC。Arria®10器件系列实现了:比上一代中高端FPGA更高的性能。通过一套综合节能技术来降低功耗。Arria®10器件专为各领域中高性能、功耗敏感的中端应用而设计。Arria®10GX1150FPGA系列器件:10AX115H3F34E2LG10A......
  • centos7 安装ORACLE 11.2.0.4.0 RAC
    环境:oraclelinux7.7,安装系统时需双网卡,接心跳线。节点一(rac1):网卡一:10.121.116.91网卡二:192.168.1.101节点二(rac2):网卡一:10.121.116.92网卡二:192.168.1.103关闭防火墙systemctlstopfirewalldsystemctldisablefirewalld1.挂载镜像,配置本地......
  • 每日一题——Python实现PAT乙级1111 对称日(举一反三+思想解读+逐步优化)七千字好文
    一个认为一切根源都是“自己不够强”的INTJ个人主页:用哲学编程-CSDN博客专栏:每日一题——举一反三Python编程学习Python内置函数Python-3.12.0文档解读目录我的写法代码点评时间复杂度分析空间复杂度分析综上所述:优化建议我要更强优化建议完整代码和注释优化分析......