CSS定位中“父相子绝”

一、定位的介绍

  定位有三种:相对定位(position:relative)、绝对定位(position:absolute)、固定定位(position:fixed)

二、三种定位的用法,特点和实例

2.1、相对定位  特性:不脱标、形影分离、老家留坑

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title>Document</title> 6 6 <style type="text/css"> 7 7 8 8 *{ 9 9 padding: 0; 1010 margin: 0; 1111 } 1212 div{ 1313 width: 200px; 1414 height: 200px; 1515 1616 } 1717 .box1{ 1818 background-color: red; 1919 } 2020 .box2{ 2121 background-color: green; 2222 position: relative; 2323 /*top: 50px;*/ 2424 left: 300px; 2525 } 2626 .box3{ 2727 background-color: blue; 2828 } 2929 3030 3131 </style> 3232 </head> 3333 <body> 3434 3535 <!-- 相对定位三大特性: 1.不脱标 2.形影分离 3.老家留坑 3636 所以说 相对定位 在页面中没有什么太大的作用。影响我们页面的布局。但是我们不要使用相对定位来做压盖效果--> 3737 3838 <div class="box1"></div> 3939 <div class="box2"></div> 4040 <div class="box3"></div> 4141 4242 4343 </body> 4444 </html>

相对定位的特性

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title>超链接美化</title> 6 6 <style type="text/css"> 7 7 /*清除默认样式*/ 8 8 *{ 9 9 padding: 0; 10 10 margin: 0; 11 11 } 12 12 ul{ 13 13 list-style: none; 14 14 } 15 15 .nav{ 16 16 width: 960px; 17 17 /*height: 40px;*/ 18 18 overflow: hidden; 19 19 margin: 100px auto ; 20 20 background-color: purple; 21 21 /*设置圆角*/ 22 22 border-radius: 10px; 23 23 } 24 24 .nav ul li{ 25 25 float: left; 26 26 width: 160px; 27 27 height: 40px; 28 28 line-height: 40px; 29 29 text-align: center; 30 30 } 31 31 .nav ul li.xiaoming{ 32 32 position: relative; 33 33 top: 40px; 34 34 left: 30px; 35 35 } 36 36 .nav ul li a{ 37 37 display: block; 38 38 width: 160px; 39 39 height: 40px; 40 40 color: white; 41 41 font-size: 20px; 42 42 text-decoration: none; 43 43 font-family: 'Hanzipen SC'; 44 44 } 45 45 /*a标签除外,不继承父元素的color*/ 46 46 47 47 48 48 .nav ul li a:hover{ 49 49 background-color: red; 50 50 font-size: 22px; 51 51 } 52 52 </style> 53 53 </head> 54 54 <body> 55 55 56 56 <div class="nav"> 57 57 <ul> 58 58 <li> 59 59 <a href="">网站导航1</a> 60 60 </li> 61 61 <li class="xiaoming"> 62 62 <a href="">网站导航2</a> 63 63 </li> 64 64 <li> 65 65 <a href="">网站导航3</a> 66 66 </li> 67 67 <li> 68 68 <a href="">网站导航4</a> 69 69 </li> 70 70 <li> 71 71 <a href="">网站导航5</a> 72 72 </li> 73 73 <li> 74 74 <a href="">网站导航6</a> 75 75 </li> 76 76 </ul> 77 77 </div> 78 78 </body> 79 79 </html> 80 80 81 81 82 82 83 83 <!-- 因为相对定位有坑,占着茅房不拉屎,所以我们一般不要使用相对定位来做压盖效果。它在页面中,效果作用极小,就两个作用: 84 84 1.微调元素位置 85 85 2.做绝对定位的参考(父相子绝) 讲绝对定位会讲 86 86 87 87 --> 88 88 89 89 90 90 91 91 <!DOCTYPE html> 92 92 <html lang="en"> 93 93 <head> 94 94 <meta charset="UTF-8"> 95 95 <title>Document</title> 96 96 <style type="text/css"> 97 97 *{ 98 98 padding: 0; 99 99 margin: 0; 100100 } 101101 div{ 102102 margin: 100px; 103103 } 104104 .user{ 105105 font-size: 25px; 106106 } 107107 .btn{ 108108 position: relative; 109109 top: 3px; 110110 left: -5px; 111111 } 112112 113113 </style> 114114 </head> 115115 <body> 116116 <!-- 微调我们元素位置--> 117117 118118 <div> 119119 120120 <input type="text" name="username" class="user"> 121121 <input type="button" name="" value="点我" class="btn"> 122122 </div> 123123 124124 </body> 125125 </html>

相对定位的用途

2.2、绝对定位  特性:脱标、做遮盖效果,提升了层级、设置绝对定位之后,不区分行内元素和块级元素,都能设置宽高

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title>Document</title> 6 6 <style type="text/css"> 7 7 8 8 *{ 9 9 padding: 0; 1010 margin: 0; 1111 } 1212 div{ 1313 width: 200px; 1414 height: 200px; 1515 } 1616 .box1{ 1717 background-color: red; 1818 1919 /*绝对的定位: 1.脱标 2.做遮盖效果,提升层级 3.设置绝对定位之后,不区分行内元素和块级元素,都能设置宽高。*/ 2020 position: absolute; 2121 } 2222 .box2{ 2323 background-color: green; 2424 2525 } 2626 .box3{ 2727 background-color: blue; 2828 } 2929 span{ 3030 width: 100px; 3131 height: 100px; 3232 background-color: pink; 3333 position: absolute; 3434 } 3535 3636 3737 </style> 3838 </head> 3939 <body> 4040 4141 4242 4343 <div class="box1"></div> 4444 <div class="box2"></div> 4545 <div class="box3"></div> 4646 <span>文字</span> 4747 4848 </body> 4949 </html>

绝对定位的特性

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 body{ 8 8 width: 100%; 9 9 height: 2000px; 1010 border: 10px solid green; 1111 } 1212 1313 .box{ 1414 width: 200px; 1515 height: 200px; 1616 background-color: red; 1717 /*绝对定位参考点: 1818 1.当我使用top属性描述的时候 是以页面的左上角(跟浏览器的左上角区分)为参考点来调整位置 1919 2.当我使用bottom属性描述的时候。是以首屏页面左下角为参考点来调整位置。 2020 */ 2121 position: absolute; 2222 top: 100px; 2323 /*bottom: 100px;*/ 2424 left: 18px; 2525 } 2626 </style> 2727 </head> 2828 <body> 2929 <div class="box"> 3030 3131 </div> 3232 3333 3434 3535 </body> 3636 </html>

绝对定位参考点

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 *{ 8 8 padding: 0; 9 9 margin: 0; 1010 } 1111 .box{ 1212 width: 300px; 1313 height: 300px; 1414 border: 5px solid red; 1515 margin: 100px; 1616 /*父盒子设置相对定位*/ 1717 position: relative; 1818 padding: 50px; 1919 } 2020 .box2{ 2121 width: 300px; 2222 height: 300px; 2323 background-color: green; 2424 position: relative; 2525 2626 } 2727 2828 .box p{ 2929 width: 100px; 3030 height: 100px; 3131 background-color: pink; 3232 /*子元素设置了绝对定位*/ 3333 position: absolute; 3434 top: 0; 3535 left: 0; 3636 } 3737 3838 /*父辈元素设置相对定位,子元素设置绝对定位,那么会以父辈元素左上角为参考点 3939 这个父辈元素不一定是爸爸,它也可以是爷爷,曾爷爷。 如果父亲设置了定位,那么以父亲为参考点。那么如果父亲没有设置定位,那么以父辈元素设置定位的为参考点 4040 */ 4141 4242 </style> 4343 </head> 4444 <body> 4545 <div class="box"> 4646 4747 <div class="box2"> 4848 <p></p> 4949 </div> 5050 </div> 5151 5252 </body> 5353 </html>

绝对定位以盒子作为参考点

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 *{ 8 8 padding: 0; 9 9 margin: 0; 1010 } 1111 .box{ 1212 width: 300px; 1313 height: 300px; 1414 border: 5px solid red; 1515 margin: 100px; 1616 /*父盒子设置相对定位*/ 1717 position: absolute; 1818 padding: 50px; 1919 } 2020 2121 2222 .box p{ 2323 width: 100px; 2424 height: 100px; 2525 background-color: pink; 2626 /*子元素设置了绝对定位*/ 2727 position: absolute; 2828 top: 10px; 2929 left: 20px; 3030 } 3131 3232 3333 </style> 3434 </head> 3535 <body> 3636 3737 <!-- 不仅仅是父相子绝,父绝子绝 ,父固子绝,都是以父辈元素为参考点 。 3838 3939 注意了:父绝子绝,没有实战意义,做站的时候不会出现父绝子绝。因为绝对定位脱离标准流,影响页面的布局。相反‘父相子绝’在我们页面布局中,是常用的布局方案。因为父亲设置相对定位,不脱离标准流,子元素设置绝对定位,仅仅的是在当前父辈元素内调整位置信息。 4040 4141 --> 4242 <div class="box"> 4343 4444 <p></p> 4545 4646 </div> 4747 4848 </body> 4949 </html>

绝对定位以父辈盒子做参考点2

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 *{ 8 8 padding: 0; 9 9 margin: 0; 1010 } 1111 .father{ 1212 width: 300px; 1313 height: 300px; 1414 margin: 100px; 1515 border: 10px solid red; 1616 position: relative; 1717 padding: 50px; 1818 } 1919 .father p{ 2020 width: 100px; 2121 height: 100px; 2222 background-color: yellow; 2323 position: absolute; 2424 top: 10px; 2525 left: 40px; 2626 } 2727 </style> 2828 </head> 2929 <body> 3030 <div class="father"> 3131 <p></p> 3232 </div> 3333 3434 </body> 3535 </html>

绝对定位的盒子无视父辈的padding

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 *{ 8 8 padding: 0; 9 9 margin: 0; 1010 } 1111 .box{ 1212 width: 100%; 1313 height: 69px; 1414 background: #000; 1515 } 1616 .box .c{ 1717 width: 960px; 1818 height: 69px; 1919 background-color: pink; 2020 /*margin: 0 auto;*/ 2121 position: relative; 2222 left: 50%; 2323 margin-left: -480px; 2424 2525 /*设置绝对定位之后,margin:0 auto;不起任何作用,如果想让绝对定位的盒子居中。当做公式记下来 设置子元素绝对定位,然后left:50%; margin-left等于元素宽度的一半,实现绝对定位盒子居中*/ 2626 } 2727 2828 2929 </style> 3030 </head> 3131 <body> 3232 <div class="box"> 3333 <div class="c"></div> 3434 </div> 3535 3636 </body> 3737 </html>

绝对定位盒子居中

2.3、固定定位  特性:脱标、遮盖,提成层级、固定位置

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 8 8 *{ 9 9 padding: 0; 1010 margin: 0; 1111 } 1212 p{ 1313 width: 100px; 1414 height: 100px; 1515 background-color: red; 1616 /*固定定位:固定当前的元素不会随着页面滚动而滚动, 1717 特性:1.脱标 2.提升层级 3.固定不变 不会随页面滚动而滚动 1818 1919 参考点:设置固定定位,用top描述。那么是以浏览器的左上角为参考点 2020 如果用bottom描述,那么是以浏览器的左下角为参考点 2121 2222 作用: 1.返回顶部栏 2.固定导航栏 3.小广告 2323 2424 */ 2525 position: fixed; 2626 bottom: 30px; 2727 right: 40px; 2828 } 2929 </style> 3030 </head> 3131 <body> 3232 3333 <div> 3434 <p></p> 3535 <img src="./bojie.jpg" alt=""> 3636 <img src="./bojie.jpg" alt=""> 3737 <img src="./bojie.jpg" alt=""> 3838 <img src="./bojie.jpg" alt=""> 3939 <img src="./bojie.jpg" alt=""> 4040 <img src="./bojie.jpg" alt=""> 4141 4242 </div> 4343 </body> 4444 </html>

固定定位特性和应用场景

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title>固定导航栏</title> 6 6 <style type="text/css"> 7 7 *{ 8 8 padding: 0; 9 9 margin: 0; 10 10 } 11 11 ul{ 12 12 list-style: none; 13 13 } 14 14 a{ 15 15 text-decoration: none; 16 16 } 17 17 body{ 18 18 /*给body设置导航栏的高度,来显示下方图片的整个内容*/ 19 19 padding-top: 49px; 20 20 } 21 21 .wrap{ 22 22 width: 100%; 23 23 height: 49px; 24 24 background-color: #000; 25 25 /*设置固定定位之后,一定一定要加top属性和left属性*/ 26 26 position: fixed; 27 27 top: 0; 28 28 left: 0; 29 29 30 30 31 31 } 32 32 .wrap .nav{ 33 33 width: 960px; 34 34 height: 49px; 35 35 margin: 0 auto; 36 36 37 37 } 38 38 .wrap .nav ul li{ 39 39 float: left; 40 40 width: 160px; 41 41 height: 49px; 42 42 43 43 text-align: center; 44 44 } 45 45 .wrap .nav ul li a{ 46 46 width: 160px; 47 47 height: 49px; 48 48 display: block; 49 49 color: #fff; 50 50 font: 20px/49px "Hanzipen SC"; 51 51 background-color: purple; 52 52 } 53 53 .wrap .nav ul li a:hover{ 54 54 background-color: red; 55 55 font-size: 22px; 56 56 } 57 57 58 58 59 59 60 60 </style> 61 61 </head> 62 62 <body> 63 63 <div class="wrap"> 64 64 <div class="nav"> 65 65 <ul> 66 66 <li> 67 67 <a href="#">网页开发</a> 68 68 </li> 69 69 <li> 70 70 <a href="#">网页开发</a> 71 71 </li> 72 72 <li> 73 73 <a href="#">网页开发</a> 74 74 </li> 75 75 <li> 76 76 <a href="#">网页开发</a> 77 77 </li> 78 78 <li> 79 79 <a href="#">网页开发</a> 80 80 </li> 81 81 <li> 82 82 <a href="#">网页开发</a> 83 83 </li> 84 84 </ul> 85 85 </div> 86 86 </div> 87 87 <img src="./bojie.jpg" alt=""> 88 88 <img src="./bojie.jpg" alt=""> 89 89 <img src="./bojie.jpg" alt=""> 90 90 <img src="./bojie.jpg" alt=""> 91 91 <img src="./bojie.jpg" alt=""> 92 92 <img src="./bojie.jpg" alt=""> 93 93 <img src="./bojie.jpg" alt=""> 94 94 <img src="./bojie.jpg" alt=""> 95 95 <img src="./bojie.jpg" alt=""> 96 96 <img src="./bojie.jpg" alt=""> 97 97 <img src="./bojie.jpg" alt=""> 98 98 <img src="./bojie.jpg" alt=""> 99 99 100100 101101 </body> 102102 </html>

固定定位_固定导航栏

三、父相子绝

当父盒子设定为相对定位,子盒子绝对定位的参考点是父盒子的左上角。如果父盒子设定为绝对定位或者固定定位,子盒子同样是以父盒子的左上角做参考点,但是那样,父盒子就脱离标准流,没什么意义。所以一般情况下,都是要遵从“父相子绝”的原则。附一个父相子绝的案例,如下:

1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="UTF-8"> 5 5 <title></title> 6 6 <style type="text/css"> 7 7 *{ 8 8 padding: 0; 9 9 margin: 0; 1010 } 1111 .box{ 1212 width: 300px; 1313 height: 300px; 1414 border: 5px solid red; 1515 margin: 100px; 1616 /*父盒子设置相对定位*/ 1717 position: relative; 1818 padding: 50px; 1919 } 2020 .box2{ 2121 width: 300px; 2222 height: 300px; 2323 background-color: green; 2424 position: relative; 2525 2626 } 2727 2828 .box p{ 2929 width: 100px; 3030 height: 100px; 3131 background-color: pink; 3232 /*子元素设置了绝对定位*/ 3333 position: absolute; 3434 top: 0; 3535 left: 0; 3636 } 3737 3838 /*父辈元素设置相对定位,子元素设置绝对定位,那么会以父辈元素左上角为参考点 3939 这个父辈元素不一定是爸爸,它也可以是爷爷,曾爷爷。 如果父亲设置了定位,那么以父亲为参考点。那么如果父亲没有设置定位,那么以父辈元素设置定位的为参考点 4040 */ 4141 4242 </style> 4343 </head> 4444 <body> 4545 <div class="box"> 4646 4747 <div class="box2"> 4848 <p></p> 4949 </div> 5050 </div> 5151 5252 </body> 5353 </html>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )