前端开发之CSS篇四
一、相对定位
二、绝对定位
三、固定定位
四、z-index
前言
定位有三种:1、相对定位 2、绝对定位 3、固定定位
这三种定位,每种都暗藏玄机,所以要每个单独剖析。
1️⃣ 相对定位
1、三大特性
- 1、不脱标 (遵循标准流)
- 2、形影分离
- 3、老家留坑,占着茅坑不拉屎,很恶心
所以说相对定位没什么太大的用处,还影响页面布局。不建议使用相对定位来做压盖效果
2、好处
- ① 微调元素信息
- ② 做绝对定位的参考(父相子地)
3、示例:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>相对定位</title>
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- .box1{
- width:300px;
- height: 50px;
- background-color: dodgerblue;
- /*如果仅对当前元素设置相对定位,那么与标准流下的盒子没有什么区别*/
- /*position: relative;*/
- /*!*设置相对定位,我们可以使用四个方向的属性 top left right bottom*/
- /*相对定位:相对于自己原本的本身定位 top:30px;*/
- /*那么盒子相对原来的位置向下移动30px*!*/
- /*top:30px;*/
- /*left:30px;*/
- }
- .box2{
- width:300px;
- height: 200px;
- margin:100px;
- }
- .user{
- font-size: 20px;
- }
- .btn{
- position: relative;
- top: 1px;
- left:-5px;
- }
- </style>
- </head>
- <body>
- <div class="box1"></div>
- <div class="box2">
- <input type="text" name="username" class="user">
- <input type="button" name="" value="点我" class="btn">
- </div>
- <div class="box3"></div>
- </body>
- </html>
2️⃣ 绝对定位
1、特性:
- ① 脱标(脱离标准流),
- ② 做遮盖效果,
- ③ 设置绝对定位后,不区分行内元素和块状元素,都能设置宽高(相比设display:block,设置绝对定位会脱标)
示例:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>绝对定位</title>
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- .box3{
- width:300px;
- height:200px;
- background-color: #5cb85c;
- position: absolute; /*遮盖下一个盒子*/
- }
- .box4{
- width:300px;
- height:200px;
- background-color: #b3d7ff;
- /*position: absolute; !*遮盖下一个盒子*!*/
- }
- .box5{
- width:300px;
- height:200px;
- background-color:indianred;
- }
- span{
- width:200px;
- height:100px;
- background-color: #7d74ff;
- position: absolute; /*相当于display:block,将行内元素转成了块状元素*/
- }
- </style>
- </head>
- <body>
- <div class="box3"></div>
- <div class="box4"></div>
- <div class="box5"></div>
- <span>你好,明天</span>
- </body>
- </html>
2、绝对定位参考点
- 设置属性top时,定位点在页面左上角,不是浏览器页面,一定要区分
- 设置属性为bottom时,定位点在页面左下,页面滑动缩放时也能随之滑动缩放
示例:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>绝对定位参考点</title>
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- body{
- width:1000px;
- height: 1000px;
- border: 2px solid yellowgreen;
- }
- .box6{
- width:300px;
- height:200px;
- background-color: #b3d7ff;
- position: absolute;
- /*top:100px; !*设置属性top时,定位点在页面左上角,不是浏览器页面,一定要区分*!*/
- left: 2px;
- bottom: 100px;/*设置属性为bottom时,定位点在页面左下,页面滑动缩放时也能随之滑动缩放*/
- }
- </style>
- </head>
- <body>
- <div class="box6"></div>
- </body>
- </html>
绝对定位以父辈元素作为参考点
分三种:父相子绝,父绝子绝,父固子绝,都是以父辈元素为参考点。
注意:
- “父绝子绝”(即父辈设为绝对定位,子辈设为绝对定位),没有实战意义,做站的时候不会出现父绝子绝。
- 因为绝对定位脱离标准流,影响页面的布局。与之相反,“父相子绝”(即父辈设为相对定位,子辈设为绝对定位),
- 因为父辈设为了相对定位,不脱离标准流,子元素设置绝对定位,仅仅是在当前父辈元素内调整位置信息。
“父相子绝”主要有两种情况:
①爷爷设置相对定位,父亲没有设置定位,儿子设置绝对定位,那么以爷爷的左顶点为参考点来调整位置。
②爷爷设置相对定位,父亲设置相对定位,儿子设置绝对定位,那么以父亲的左顶点为参考点来调整位置。
总而言之就是:
- 父元素设置相对定位,子元素设置绝对定位,此时子元素的参考点为父元素的左顶点。
情况①示例如下:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>爷爷设置相对定位,父亲没有设置定位,儿子设置绝对定位,那么以爷爷的左顶点为参考点来调整位置(父相自绝)</title>
- <style type="text/css">
- .box2{
- width:300px;
- height:200px;
- border: 2px solid pink;
- margin: 100px;
- position: relative;
- padding: 100px;
- }
- .box2-son{
- width: 200px;
- height: 150px;
- background-color: #b2e567;
- }
- span{
- width:100px;
- height: 80px;
- background-color: #428bca;
- position: absolute;
- top:50px;
- left:50px;
- }
- </style>
- </head>
- <body>
- <div class="box2">
- <div class="box2-son">
- <span></span>
- </div>
- </div>
- </body>
- </html>
情况②示例如下:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>绝对定位参考点-以父辈元素作为参考点,父辈相对定位,子辈绝对定位(父相自绝)</title>
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- /*爷爷设置相对定位,父亲设置相对定位,儿子设置绝对定位,那么以父亲的左顶点为参考点来调整位置*/
- *{
- padding:0;
- margin:0;
- }
- .box2{
- width:300px;
- height:200px;
- border: 2px solid pink;
- margin: 100px;
- position: relative;
- padding: 100px;
- }
- .box2-son{
- width: 200px;
- height: 150px;
- background-color: #b2e567;
- position: relative;
- }
- span{
- width:100px;
- height: 80px;
- background-color: #428bca;
- position: absolute;
- top:40px;
- left:50px;
- }
- </style>
- </head>
- <body>
- <div class="box2">
- <div class="box2-son">
- <span></span>
- </div>
- </div>
- </body>
- </html>
总结成一种就是:
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- /*父元素设置相对定位,子元素设置绝对定位,此时子元素的参考点为父元素的左顶点*/
- .box1{
- width:400px;
- height: 300px;
- border: 2px solid palegreen;
- margin: 100px;
- /*父元素设置相对定位*/
- position: relative;
- }
- p{
- width:200px;
- height: 100px;
- background-color:indianred;
- /*子元素设置绝对定位*/
- position: absolute;
- top: 10px;
- left: 20px;
- }
- </style>
3、绝对定位的盒子无视父辈的padding
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>绝对定位参考点-以父辈元素作为参考点,父辈相对定位,子辈绝对定位(父相自绝)</title>
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- *{
- padding:0;
- margin:0;
- }
- .box{
- width:200px;
- height:200px;
- border: 2px solid pink;
- margin: 100px;
- position: relative;
- padding: 30px;
- }
- .box-son{
- width: 100px;
- height: 100px;
- background-color: #b2e567;
- position:absolute;
- top:0;
- left:0;
- }
- </style>
- </head>
- <body>
- <div class="box">
- <div class="box-son">
- </div>
- </div>
- </body>
- </html>
4、绝对定位盒子居中
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>绝对定位参考点-以父辈元素作为参考点,父辈相对定位,子辈绝对定位(父相自绝)</title>
- <style type="text/css">
- *{
- padding:0;
- margin:0;
- }
- /*绝对定位盒子居中*/
- .box{
- width:100%;
- height: 50px;
- background-color: #7d74ff;
- }
- .box-son{
- width: 1000px;
- height:50px;
- background-color: #b3d7ff;
- /*设置绝对定位,使盒子水平居中,必须要写下面三句*/
- position: absolute;
- left:50%;
- margin-left: -500px; /*数值取盒子宽度的一半(取负值,表示向左移动)*/
- }
- </style>
- </head>
- <body>
- <div class="box">
- <div class="box-son">
- </div>
- </div>
- </body>
- </html>
3️⃣ 固定定位
1、何谓固定定位?
固定定位即固定当前的元素不会随着页面滚动而滚动。
2、特性:
- 1、脱标;
- 2、提升层级;
- 3、固定不变,不会随着页面滚动而滚动。
3、参考点:
- 设置固定定位,用top描述时,那么是以浏览器的左上角为参考点,如果用bottom描述,那么以浏览器的左下角为参考点
4、作用:
- 1、返回顶部栏
- 2、固定导航栏
- 3、小广告
5、示例:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>固定定位</title>
- <style type="text/css">
- /* *{}、ul{}和a{}用于页面初始化设置 */
- *{
- padding:0;
- margin:0;
- }
- ul{
- list-style: none;
- }
- a{
- text-decoration: none;
- }
- body{
- /*给body设置导航栏的高度,使下方的图片能够完全显示出来*/
- padding-top:50px;
- }
- p{
- width:200px;
- height:180px;
- background-image: url("./images/lufei.jpg");
- background-repeat: no-repeat;
- border-radius: 5px;
- position: fixed;
- top:100px;
- /*bottom: 100px;*/
- left:80px;
- }
- /* 固定导航栏 */
- .wrap{
- width:100%;
- height:50px;
- background-color: #b3d7ff;
- position:fixed;
- top:0;
- left:0;
- }
- .wrap .nav{
- width:1000px;
- height:50px;
- margin:0 auto;
- background-color:yellowgreen;
- }
- .wrap .nav ul li{
- width:200px;
- height:50px;
- float:left;
- text-align: center;
- }
- .wrap .nav ul li a{
- width: 200px;
- height:50px;
- font-size: 16px;
- font-family: "Microsoft YaHei UI";
- line-height: 50px;
- background-color: #7d74ff;
- display: inline-block;
- }
- .wrap .nav ul li a:hover{
- font-size: 20px;
- color:mediumspringgreen;
- background-color: #5bc0de;
- }
- /*返回顶部栏*/
- .return_top{
- width:40px;
- height: 60px;
- background-color: #428bca;
- font-size: 18px;
- color: white;
- position: fixed;
- right: 60px;
- bottom: 60px;
- padding: 10px;
- border-radius: 6px;
- }
- .return_top a{
- text-decoration: none;
- color:yellowgreen;
- }
- </style>
- </head>
- <body>
- <div class="wrap">
- <div class="nav">
- <ul>
- <li><a href="">目录1</a></li>
- <li><a href="">目录2</a></li>
- <li><a href="">目录3</a></li>
- <li><a href="">目录4</a></li>
- <li><a href="">目录5</a></li>
- </ul>
- </div>
- </div>
- <div>
- <img src="data:images/pic1.jpg" alt="">
- <img src="data:images/pic1.jpg" alt="">
- <img src="data:images/pic1.jpg" alt="">
- <img src="data:images/pic1.jpg" alt="">
- <img src="data:images/pic1.jpg" alt="">
- <img src="data:images/pic1.jpg" alt="">
- <p></p>
- </div>
- <div class="return_top">
- <a href="#">返回顶部</a>
- </div>
- </body>
- </html>
4️⃣ z-index
- 1、z-index值仅表示谁压着谁。数值大的压盖数值小的。
- 2、只有定位了的元素,才能有z-index,也就是说,不管相对定位、绝对定位还是固定定位,
- 都可以使用z-index,而浮动元素不能使用z-index.
- 3、z-indexz值没有单位,就是一个正整数,默认的z-index值为0.
- 4、如果元素都没有z-index值,或者z-index的值一样,那么谁写在HTML之后,
- 谁在上面压着别人,定位了的元素,永远压住没有定位的元素。
- 5、从父现象:父亲怂了,儿子再牛逼也没有。
- <title>z-index实例</title>
- <style type="text/css">
- *{
- padding: 0;
- margin:0;
- }
- .father1{
- width: 300px;
- height: 200px;
- background-color: #b2e567;
- position: relative;
- z-index: 120;
- }
- .son1{
- width:110px;
- height: 100px;
- background-color: #b3d7ff;
- position: absolute;
- top:600px;
- left: 360px;
- z-index: 30;
- }
- .father2{
- width:300px;
- height: 200px;
- background-color: #7d74ff;
- position: relative;
- z-index: 100;
- }
- .son2{
- width: 120px;
- height:120px;
- background-color: burlywood;
- position: absolute;
- top:360px;
- left:360px;
- z-index:20;
- }
- </style>
- </head>
- <body>
- <div class="father1">
- <div class="son1"></div>
- </div>
- <div class="father2">
- <div class="son2"></div>
- </div>
- </body>
- </html>
前端开发之CSS篇四的更多相关文章
- 前端开发之css篇
一.css简介 css(Cascading Style Sheets)层叠样式表,是一种为html文档添加样式的语言,主要有两个功能:渲染和布局.使用css主要关注两个点:查找到标签,属性操作 二.c ...
- 前端开发之CSS篇三
主要内容: 一.CSS布局之浮动 二.清除浮动带来的问题 三.margin塌陷问题和水平居中 四.善用父级的的padding取代子级的margin 五.文本属性和字体 ...
- 前端开发之CSS篇二
主要内容: 一.CSS的继承性和层叠性 二.盒模型 三.padding属性 四.border属性 五.margin属性 六.标准文档流 七.行内元素和块状元素转换 1️⃣ CSS的继承性和层叠性 1 ...
- 前端开发之CSS入门篇
一.CSS介绍和语法 二.CSS引入方式 三.基本选择器 四.高级选择器 五.伪类选择器 六.伪元素选择器 1️⃣ CSS介绍和语法 1. CSS的介绍 (1)为什么需要CSS? 使用css的目的就 ...
- 前端开发之JavaScript篇
一.JavaScript介绍 前端三剑客之JavaScript,简称js,可能是这三个里面最难的一个了.很早以前,市面上流通着三种js版本,为了统一,ECMA(欧洲计算机制造协会)定义了规范的版本, ...
- 前端开发之HTML篇一
主要内容: 一.HTML简介 二.HTML标签 三.HTML文档结构和注释 四.head标签及相关内容 五.body标签及相关内容 1️⃣ HTM ...
- 前端开发之jQuery篇--选择器
主要内容: 1.jQuery简介 2.jQuery文件的引入 3.jQuery选择器 4.jQuery对象与DOM对象的转换 一.jQuery简介 1.介绍 jQuery是一个JavaScript库: ...
- 前端开发之HTML篇二
主要内容: 一.表格标签 -- table 二.表单标签 -- form 三.常用标签属性和分类 四.标签嵌套规则 1️⃣ 表格标签 -- table 表格由<table> 标签来定义. ...
- 前端开发之html篇
一.什么是html? 1.我们说socket网络编程的时候,提到过一个cs模型,就是客户端—服务端模型,前端开发也是基于网络编程,但是这时就应该是bs模型了,是浏览器与服务端的通信. 我们可以模拟一个 ...
随机推荐
- PowerDesigner生成Oracle表名带有引号的解决方法
PowerDesigner生成表名带有引号,如下: /*==============================================================*/ /* Tabl ...
- 《DSP using MATLAB》示例Example 9.7
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 网站使用QQ登录问题小结
关于网站如何使用QQ登陆这个问题就不多说了,很简单,登陆connect.qq.com找到相应的SDK,下载下来,里面会有demo,将相应的appid,appkey和回调地址callback改成自己的就 ...
- python 中的异常处理
refer to: http://www.runoob.com/python/python-exceptions.html http://www.pythondoc.com/pythontutoria ...
- java I/O进程控制,重定向 演示样例代码
java I/O进程控制,重定向 演示样例代码 package org.rui.io.util; import java.io.*; /** * 标准I/O重定向 */ public class Re ...
- poj 1637 Sightseeing tour——最大流+欧拉回路
题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...
- jq 合并json对象
一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 1 var object = $.extend({}, object1, object2); 二,将2的值合 ...
- Java编程打印出1000以内所有的完数
/*如果一个数等 于其所有因子之和,我们就称这个数为"完数" * 例如6的因子为1,2,3, 6=1+2+3, 6就是一一个完数. * 请编程打印出1000以内所有的完数*/ pu ...
- Spring不支持静态注入
在springframework里,我们不能@Autowired一个静态变量,使之成为一个spring bean,例如下面这样: @Autowired private static YourClass ...
- 【BZOJ】1911: [Apio2010]特别行动队(斜率优化dp)
题目 传送门:QWQ 分析 用$ dp[i] $ 表示前 i 个人组成的战斗力之和 然后显然$ dp[i]=Max ( dp[j]+a*(sum[i]-sum[j])^2+b*(sum[i]-sum ...