CSS基础之相对定位,绝对定位,固定定位,z-index

1.相对定位

1.1.相对定位,就是微调元素位置的。让元素相对自己原来的位置,进行位置调整。也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位

a position:relative; → 必须先声明,自己要相对定位了

b left:100px; → 然后进行调整。

c top:150px; → 然后进行调整。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 200px;
            height: 200px;
        }
        .box1{
            background-color: yellowgreen;
        }
        .box2{
            background-color: skyblue;
            position: relative;
            top: 150px;
            left: 100px;
        }
        .box3{
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>
图片.png

1.2.不脱标,老家留坑,形影分离,相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘。

图片.png

1.3.相对定位的用途:相对定位有坑,所以一般不用于做“压盖”效果。页面中,效果极小。就两个作用:

a:微调元素

b:做绝对定位的参考,子绝父相

1.4.相对定位的定位值

a:可以用left、right来描述盒子右、左的移动,相当于marginLeft ,marginRight

b:可以用top、bottom来描述盒子的下、上的移动,marginTop,marginBottom

图片.png

图片.png

图片.png

2.绝对定位

2.1.绝对定位的盒子,是脱离标准文档流的。所以,所有的标准文档流的性质,绝对定位之后都不遵守了。绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
        }
        .box1{
            background-color: yellowgreen;
        }
        .box2{
            background-color: skyblue;
            position: absolute;
            top: 100px;
            left: 140px;
        }
        .box3{
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>
图片.png

2.2.参考点,如果用top描述,那么定位参考点就是页面的左上角,而不是浏览器的左上角,如果用bottom描述,那么就是对应的页面的左下角,当页面滚动时,这个盒子显示的位置也跟者滚动。绝对定位脱标!:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            bottom: 100px;
            left: 100px;
        }
    </style>
</head>
<body>
    <div></div>
    <img src="images/2.jpg" alt="" />
</body>
</html>
图片.png

2.3. 以盒子为参考点

a:一个绝对定位的元素,如果父辈元素中出现了也定位了的元素,那么将以父辈这个元素,为参考点。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 400px;
            height: 400px;
            border: 10px solid red;
            margin: 100px;
            position: relative;
        }
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            top: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>
图片.png
b: 不一定是相对定位,任何定位,都可以作为参考点,子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 400px;
            height: 400px;
            padding: 100px;
            border: 10px solid red;
            margin: 100px;
            position: relative;
        }
        .box2{
            width: 300px;
            height: 300px;
            border: 50px solid blue;
        }
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute; 
            top: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2">
            <p></p>
        </div>
    </div>
</body>
</html>
图片.png

2.4.绝对定位的盒子居中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 400px;
            height: 60px;
            background-color: green;
            position: absolute;
            left: 50%;
            margin-left: -200px;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>
图片.png

3.固定定位

3.1.固定定位,就是相对浏览器窗口定位。页面如何滚动,这个盒子显示的位置不变。固定定位脱标!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: fixed;
            top: 100px;
            left: 100px;
        }
    </style>
</head>
<body>
    <p></p>
    <img src="images/2.jpg" alt="" />
</body>
</html>
图片.png
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body{
            /*为什么要写这个?*/
            /*不希望我们的页面被nav挡住*/
            padding-top: 60px;
            /*IE6不兼容固定定位,所以这个padding没有什么用,就去掉就行了*/
            _padding-top:0;
        }
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 60px;
            background-color: #333;
            z-index: 99999999;
        }
        .inner_c{
            width: 1000px;
            height: 60px;
            margin: 0 auto;

        }
        .inner_c ul{
            list-style: none;
        }
        .inner_c ul li{
            float: left;
            width: 100px;
            height: 60px;
            text-align: center;
            line-height: 60px;
        }
        .inner_c ul li a{
            display: block;
            width: 100px;
            height: 60px;
            color:white;
            text-decoration: none;
        }
        .inner_c ul li a:hover{
            background-color: gold;
        }
        p{
            font-size: 30px;
        }
        .btn{
            display: block;
            width: 120px;
            height: 30px;
            background-color: orange;
            position: relative;
            top: 2px;
            left: 1px;
        }
    </style>
</head>
<body>
    <div class="nav">
        <div class="inner_c">
            <ul>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
            </ul>
        </div>
    </div>
    
    <img src="images/2.jpg" alt="" />

    <p>
        <a href="" class="btn">按钮</a>
    </p>
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
 
     
</body>
</html>
图片.png

4.z-index值

● z-index值表示谁压着谁。数值大的压盖住数值小的。
● 只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。
● z-index值没有单位,就是一个正整数。默认的z-index值是0。
● 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。
● 从父现象:父亲怂了,儿子再牛逼也没用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 200px;
            height: 200px;
            background: yellowgreen;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 444;
        }
        .box2{
            width: 200px;
            height: 200px;
            background: skyblue;
            position: absolute;
            top: 180px;
            left: 180px;
            z-index: 333;
        }
    </style>
</head>
<body>
    <div class="box1">绿</div>
    <div class="box2">蓝</div>
</body>
</html>

图片.png

如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 200px;
            height: 200px;
            background: yellowgreen;
        }
        .box2{
            width: 200px;
            height: 200px;
            background: skyblue;
            position: relative;
            top: 100px;
            left: 30px;
        }
        .box3{
            width: 200px;
            height: 200px;
            background: pink;
            /*为了z-index值生效,必须加上一个定位:*/
            position: relative;
            top: 0;
            left: 0;
            z-index: 999;
        }
    </style>
</head>
<body>
    <div class="box1">绿</div>
    <div class="box2">蓝</div>
    <div class="box3">粉</div>
</body>
</html>
图片.png

从父现象:父亲怂了,儿子再牛逼也没用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .linzhiying{
            width: 200px;
            height: 200px;
            background-color: blue;
            position: relative;
            z-index: 10;
        }
        .tianliang{
            width: 200px;
            height: 200px;
            background-color: orange;
            position: relative;
            z-index: 9;
        }
        .kimi{
            width: 60px;
            height: 60px;
            background-color: green;
            position: absolute;
            top: 300px;
            left: 450px;
            z-index: 454;
        }
        .cindy{
            width: 60px;
            height: 60px;
            background-color: pink;
            position: absolute;
            top: 130px;
            left: 490px;
            z-index: 45454;
        }
    </style>
</head>
<body>
    <div class="linzhiying">
        <p class="kimi"></p>
    </div>
    <div class="tianliang">
        <p class="cindy"></p>
    </div>
</body>
</html>
图片.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,703评论 1 92
  • 1.CSS盒模型 盒子模型有两种,分别是标准盒子模型和IE盒子模型。 CSS 把盒模型分为两种基本形态:Block...
    莫失丿莫忘阅读 527评论 0 2
  • CSS 定位 CSS有三种基本的定位机制:普通流,浮动,绝对定位(absolute, fixed):普通流是默认定...
    _空空阅读 5,698评论 0 15
  • 他,风趣幽默。 他,足智多谋。 他,霸气侧漏。 他,口才了得。 他,头脑灵活。 …… 他身上有太多太多的优点让我膜...
    薏米维玮阅读 165评论 2 0
  • 原文:https://github.com/electron/electron/blob/master/docs/...
    Shmily落墨阅读 1,433评论 0 2