CSS中position详解

CSS中position详解一些关于定位和叠加的理解一 position 属性 position 有 5 个值 分别为 static relative absolute fixed sticky 1 1staticstati 是 position 的默认属性 元素会按正常的文档流进行排序 不会受到 top bottom left right 的影响 1 2relativerel 相对定位的元素会较正常文档流中的位置进

一些关于定位和叠加的理解

一、position属性

position有5个值,分别为static,relative,absolute,fixed,sticky。

1.1 static

static是position的默认属性,元素会按正常的文档流进行排序,不会受到top,bottom,left,right的影响。

1.2 relative

relative相对定位的元素会较正常文档流中的位置进行偏移,受top,bottom,left,right的影响。就是元素还在文档流中像static一样占着位置,但视觉上会有偏移,多用于absolute绝对定位的父元素。

1.3 absolute

absolute绝对定位会脱离正常的文档流,相对于最近的进行过定位的(非static)父级元素定位,若没有父级元素进行过定位,则相对于即浏览器窗口定位。

1.4 fixed

1.5 sticky

 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="box"> <div>title 
       div> <div class="stickyBar">stickyBar 
        div> <p>this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph!this is a paragraph! 
         p>  
          div>  
           body>  
            html> <style> div.box{ 
             height: 2000px; } div.stickyBar { 
             position: -webkit-sticky; position: sticky; top: 0; padding: 5px; background-color: #cae8ca; border: 2px solid #4CAF50; width: 200px; } div p { 
             width: 200px; }  
             style> 

二、元素叠加时的几种状态

2.1

 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="one"> <p>divOne 
       p> <p>position: absolute; 
        p>  
         div> <div class="two"> <p>divTwo 
          p> <p>position: absolute; 
           p>  
            div>  
             body>  
              html> <style> p { 
               margin: 0; } div { 
               width: 200px; height: 200px; border: 1px solid black; } .one { 
               position: absolute; background-color: red; top: 10px; left: 10px; } .two { 
               position: absolute; background-color: blue; top: 100px; left: 100px; }  
               style> 

在这里插入图片描述

2.2

同一级别的元素使用了relative,absolute,fixed,sticky,那么z-index值大的元素在上面。

 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="one"> <p>divOne 
       p> <p>position: relative; 
        p>  
         div> <div class="two"> <p>divTwo 
          p> <p>position: absolute; 
           p>  
            div>  
             body>  
              html> <style> p { 
               margin: 0; } div { 
               width: 200px; height: 200px; border: 1px solid black; } .one { 
               position: relative; background-color: red; top: 10px; left: 10px; z-index: 99; } .two { 
               position: absolute; background-color: blue; top: 50px; left: 100px; z-index: 1; }  
               style> 

在这里插入图片描述

2.3

同级别的元素,relative,absolute,fixed,sticky定位会在static上面。

 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="one"> <p>absolute 
       p>  
        div> <div class="two"> <p>static 
         p>  
          div>  
           body>  
            html> <style> p { 
             margin: 0; } div { 
             width: 200px; height: 200px; border: 1px solid black; box-sizing: border-box; } .one { 
             position: absolute; background-color: red; top: 0; left: 0; } .two { 
             position: static; background-color: blue; margin: 100px 0 0 100px; padding-top: 100px; }  
             style> 

在这里插入图片描述

2.4

example1

在这里插入图片描述

 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="one"> <p>absolute 
       p> <p>z-index:2 
        p> <div class="child"> <p style="padding-left: 28px">absolute 
         p> <p style="padding-left: 28px">z-index:1 
          p>  
           div>  
            div> <div class="two"> <p style="padding-left: 100px">absolute 
             p> <p style="padding-left: 100px">z-index:1 
              p> <div class="child"> <p style="padding-left: 28px">absolute 
               p> <p style="padding-left: 28px">z-index:99 
                p>  
                 div>  
                  div>  
                   body>  
                    html> <style> p { 
                     margin: 0; } div { 
                     width: 200px; height: 200px; border: 1px solid black; box-sizing: border-box; } div .child { 
                     width: 100px; height: 100px; margin: 0 0 0 70px; position: relative; font-size: 14px; } .one { 
                     position: absolute; background-color: red; top: 0; left: 0; z-index: 2; } .one .child { 
                     background-color: palevioletred; z-index: 1; } .two { 
                     position: absolute; background-color: blue; top: 100px; left: 100px; z-index: 1; } .two .child { 
                     background-color: purple; z-index: 99; }  
                     style> 
example2
 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="one"> <p>static 
       p> <div class="child"> <p style="padding-left: 28px">relative 
        p> <p style="padding-left: 28px">z-index:1 
         p>  
          div>  
           div> <div class="two"> <p style="padding-left: 100px">static 
            p> <div class="child"> <p style="padding-left: 28px">static 
             p>  
              div>  
               div>  
                body>  
                 html> <style> p { 
                  margin: 0; } div { 
                  width: 200px; height: 200px; border: 1px solid black; box-sizing: border-box; } div .child { 
                  width: 100px; height: 100px; margin: 0 0 0 70px; font-size: 14px; } .one { 
                  position: static; background-color: red; } .one .child { 
                  position: relative; background-color: palevioletred; z-index: 1; } .two { 
                  position: static; background-color: blue; margin: -150px 0 0 75px; } .two .child { 
                  position: static; background-color: purple; }  
                  style> 
example3

2.5

最后再来看一种被transform影响到的情况。

父级红色未加transform

在这里插入图片描述

父级红色加了transform

在这里插入图片描述

 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
     title>  
      head> <body> <div class="one"> <div class="oneChild"> 
       div>  
        div> <div class="two">  
         div>  
          body>  
           html> <style> p { 
            margin: 0; } div { 
            width: 200px; height: 200px; border: 1px solid black; } .one { 
            position: static; background-color: red; /*transform: translate(1px,1px);*/ } .oneChild { 
            width: 100px; height: 100px; background-color: pink; margin: 50px 0 0 50px; position: relative; z-index: 2; } .two { 
            position: absolute; background-color: blue; top: 100px; left: 100px; z-index: 1; }  
            style> 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221246.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午6:34
下一篇 2026年3月17日 下午6:34


相关推荐

  • 不是单组分组函数

    不是单组分组函数问题:一:SELECT tablespace_name, SUM(bytes) freeFROM dba_free_space不是单组分组函数原因: 1、如果程序中使用了分组函数,则有两种情况可以使用:程序中存在group by,并指定了分组条件,这样可以将分组条件一起查询出来改为:  SELECT tablespace_name, SUM(bytes) freeFROM dba_free_spa…

    2022年6月30日
    32
  • 几种常见的距离计算公式

    几种常见的距离计算公式在学习分类、聚类、预测、推荐算法的过程中常常会遇到比较两个或多个对象的相似性,而相似性的度量可以通过计算距离来实现。我们常用的距离计算公式是欧几里得距离公式,但是有时候这种计算方式会存在一些缺陷,那么就需要另外的计算方法去加以补充,本文将介绍几种在机器学习中常用的计算距离。在做很多研究问题时常常需要估算不同样本之间的相似性度量(SimilarityMeasurement),这时通常采用的方法就…

    2022年6月19日
    48
  • 动态ActionForm

    动态ActionForm在正常操作中 每一个 Action 必须对应一个 Actio

    2026年3月20日
    2
  • webstorm 快捷键 失效问题[通俗易懂]

    webstorm 快捷键 失效问题[通俗易懂] 解决方案一:file-&gt;Settings-&gt;Keymap-&gt;设置为Default解决方案二:file-&gt;Settings-&gt;IdeaVim-&gt;取消对勾重启即可。原博客地址:https://blog.csdn.net/jianyuling199/article/details/80772479…

    2022年6月23日
    56
  • BA无标度网络模型构造算法

    BA无标度网络模型构造算法BA 无边度网络模型构造算法 1 增长 从一个具有 m 0 个节点的联通网络开始 每次引入一个新的节点 并且连到 m 个已经存在的节点上 这里 m 2 优先连接 一个新的节点与一个已经存在的节点 i 相连的概率 w 与节点 i 的度 k i 之间的关系为 w k i k 1 k 2 k 3 k n 其中 n 为网络中的节点的总个数 特别的说

    2026年3月18日
    2
  • 三道计算时间复杂度的题目

    三道计算时间复杂度的题目出处 算法第四版 EditionSedge 著 问题 1 4 7 三道小题初看觉得很简单 但是仔细一分析 a b 小题里面的内循环操作次数是和外层的 n i 值有关 并不是简单的操作 N 次 很久没有算过时间复杂度了 稍微感到有点棘手 publicstatic intN intsum 0 for intn N n gt 0 n 2 for inti 0 i lt

    2026年3月16日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号