17-CSS基础-定位


定位

相对定位

  • 什么是相对定位?

    • 对定位就是相对于自己以前在标准流中的位置来移动
  • 格式:

    • position: relative;
  • 示例程序

<style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 100px;
            height: 100px;
        }
        .box1{
            background-color: red;
        }
        .box2{
            background-color: green;
            position: relative;
            top: 20px;
            left: 20px;
        }
        .box3{
            background-color: blue;
        }
<style>
        
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
  • 相对定位注意点:

    • 在相对定位中同一个方向上的定位属性只能使用一个
      • top/bottom 只能用一个
      • left/right 只能用一个
    • 相对定位是不脱离标准流的, 会继续在标准流中占用一份空间
    • 由于相对定位是不脱离标准流的, 所以在相对定位中区分块级元素/行内元素/行内块级元素
    • 由于相对定位是不脱离标准流的, 并且相对定位的元素会占用标准流中的位置, 所以当给相对定位的元素设置margin/padding等属性的时会影响到标准流的布局
  • 相对定位应用场景:

    • 用于对元素进行微调

      input{
      width: 200px;
      height: 50px;
      }
      img{
      width: 100px;
      height: 50px;

        position: relative;
        top: 20px;
      

      }

      - ![](http://upload-images.jianshu.io/upload_images/647982-faf2a4c295488ed9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
      - ![](http://upload-images.jianshu.io/upload_images/647982-f9fc00e083f01cb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
      
      
    • 配合后面学习的绝对定位来使用

绝对定位

  • 什么是绝对定位?

    • 绝对定位就是相对于body或者某个定位流中的祖先元素来定位
  • 格式:

    • position: absolute;
  • 示例代码


<style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 100px;
            height: 100px;
        }
        .box1{
            background-color: red;
        }
        .box2{
            background-color: green;
            position: absolute;
            left: 0;
            top: 0;
        }
        .box3{
            background-color: blue;
        }
</style>
    
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
  • 绝对定位注意点:
    • 绝对定位的元素是脱离标准流的, 不会占用标准流中的位置
    • 由于绝对定位的元素是脱离标准流的, 所以绝对定位的元素不区分块级元素/行内元素/行内块级元素
    • 如果一个绝对定位的元素是以body作为参考点, 那么其实是以网页首屏的宽度和高度作为参考点, 而不是以整个网页的宽度和高度作为参考点
      • 相对于body定位会随着页面的滚动而滚动
    • 一个绝对定位的元素会忽略祖先元素的padding
<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 300px;
            height: 300px;
            background-color: red;
            border: 10px solid #000;
            padding: 30px;
            position: relative;
            box-sizing: border-box;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            left: 0;
            top: 0;
        }
</style>

<div class="box1">
    <div class="box2"></div>
</div>
  • 绝对定位参考点:
    • 默认情况下所有的绝对定位的元素, 无论有没有祖先元素, 都会以body作为参考点
    • 如果一个绝对定位的元素有祖先元素, 并且祖先元素中有一个是定位流中的元素, 那么这个绝对定位的元素就会以定位流的那个祖先元素作为参考点
    • 如果一个绝对定位的元素有祖先元素, 并且祖先元素中有多个是定位流中的元素, 那么这个绝对定位的元素会以离它最近的那个定位流的祖先元素为参考点
<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 300px;
            height: 300px;
            background-color: red;
            position: relative;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: green;
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            left: 0;
            bottom: 0;
          }
</style>
    
<div class="box1">
    <div class="box2">
        <div class="box3"></div>
    </div>
</div>
<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 300px;
            height: 300px;
            background-color: red;
            position: relative;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: green;
            position: relative;
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            left: 0;
            bottom: 0;
          }
</style>
    
<div class="box1">
    <div class="box2">
        <div class="box3"></div>
    </div>
</div>
  • 绝对定位水平居中
    • 1.注意当一个盒子绝对定位之后不能使用margin: 0 auto;让盒子自身居中
    • 2.如果想让过一个绝对定位的盒子自身居中, 可以使用left: 50%; margin-left:-元素宽度一半px;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>74-绝对定位水平居中</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 400px;
            height: 50px;
            background-color: red;
            position: absolute;
            /*无效*/
            /*margin: 0 auto;*/
            /*有效*/
            left: 50%;
            margin-left:-200px;
        }
    </style>
</head>
<body>
<div></div>
</body>
</html>
  • 绝对定位应用场景:
    • 用于对元素进行微调
    • 配合后面学习的绝对定位来使用

子绝父相

  • 企业开发中一般相对定位和绝对定位都是一起出现, 很少单独使用

  • 为什么要子绝父相?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>71-子绝父相</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ul{
            width: 800px;
            height: 50px;
            background-color: red;
            list-style: none;
            margin: 0px auto;
            margin-top: 100px;
        }
        li{
            width: 100px;
            /*height: 50px;*/
            line-height: 50px;
            float: left;
            background-color: gray;
            text-align: center;
        }
        .li03{
            background-color: darkgray;
            position: relative;
        }
        ul li img{
            /*
            缺点以前的位置仍然被占用, 不能让文字居中对齐
            */
            
            /*position: relative;
            left: -35px;
            top: -15px;*/
            
            /* 浏览器调整之后位置会发生变化*/
            
           /* position: absolute;
            top: 95px;
            left: 535px;*/
            
            
            position: absolute;
            left: 37px;
            top: -5px;
            
        }
    </style>
</head>
<body>
<ul>
    <li>服装城</li>
    <li>美妆馆</li>
    <li>京东超市</li>
    <li class="li03">全球购![](hot.png)</li>
    <li>闪购</li>
    <li>团购</li>
    <li>拍卖</li>
    <li>江哥</li>
</ul>
</body>
</html>
  • 相对定位和绝对定位一般都是用来做覆盖效果的, 当看到某个元素覆盖在另外一个元素上时, 第一时间就要想到定位流

固定定位

  • 什么是固定定位?

    • 固定定位和前面学习的背景关联方式很像, 背景关联方式可以让某个图片不随着滚动条的滚动而滚动, 而固定定位可以让某个盒子不随着滚动条的滚动而滚动
  • 格式:

    • position: fixed;
  • 示例代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>74-固定定位</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        p{
            width: 100px;
        }
        a{

            width: 50px;
            height: 50px;
            background-color: rgba(0, 0, 0, 0.3);
            border-radius: 25px;
            text-decoration: none;
            text-align: center;
            color: #000;

            position: fixed;
            right: 10px;
            bottom: 10px;
        }

    </style>
</head>
<body>
<p>我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字</p>

<a href="#">^<br>顶部</a>

</body>
</html>
  • 固定定位注意点:

    • 固定定位的元素是脱离标准流的, 不会占用标准流中的位置
    • 由于固定定位的元素是脱离标准流的, 所以绝对定位的元素不区分块级元素/行内元素/行内块级元素
    • IE6不支持固定定位
  • 固定定位应用场景:

    • 网页对联广告
    • 网页头部通栏(穿透效果)

静态定位

  • 什么是静态定位?

    • 默认情况下标准流中的元素position属性就等于static, 所以静态定位其实就是默认的标准流
  • 静态定位应用场景:

    • 一般用于配合JS清除定位属性

z-index属性

  • 什么是z-index值?

    • 用于指定定位的元素的覆盖关系
  • 定位元素的覆盖关系:

    • 默认情况下定位的元素一定会盖住没有定位的元素
    • 默认情况下写在后面的定位元素会盖住前面的定位元素
    • 默认情况下所有元素的z-index值都是0, 如果设置了元素的z-index值, 那么谁比较大谁就显示在前面
    • 定位元素的从父现象
      • 父元素没有z-index值, 那么子元素谁的z-index大谁盖住谁
      • 父元素z-index值不一样, 那么父元素谁的z-index大谁盖住谁
  • z-index应用场景
    • 控制界面上的定位元素的覆盖关系, 例如网页中后面的定位元素不能覆盖前面的导航条通栏

学习交流方式:
1.微信公众账号搜索: 李南江(配套视频,代码,资料各种福利获取)
2.加入前端学习交流群:
302942894 / 289964053 / 11550038

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,630评论 1 92
  • CSS 定位 CSS有三种基本的定位机制:普通流,浮动,绝对定位(absolute, fixed):普通流是默认定...
    _空空阅读 5,647评论 0 15
  • 定位 相对定位 对定位就是相对于自己以前在标准流中的位置来移动 格式:position: relative; 相对...
    Strive_12c4阅读 226评论 0 0
  • 一.定位流分类 1.1相对定位1.2绝对定位1.3固定定位1.4静态定位 二.什么是相对定位? 相对定位就是相对于...
    壹点微尘阅读 324评论 0 0
  • 上个月的时候,白夜追凶正式上线了,首映时间的多次推迟,让我有了兴趣去瞧瞧这部网剧。有次看新闻说,白夜追凶某...
    艾特小檬虫阅读 418评论 0 0