CSS position &居中(水平,垂直)
css position是个很重要的知识点:
知乎Header部分:

知乎Header-inner部分:

position属性值:
fixed:生成绝对定位的元素,相对浏览器窗口进行定位(位置可通过:left,right,top,bottom改变);与文档流无关,不占据空间(可能与其它元素重叠)
relative:生成相对定位的元素(相对于元素正常位置)(left,right,top,bottom);relative的元素经常用作absolute的元素的容器块;原先占据的空间依然保留
absolute:生成绝对定位的元素(相对第一个已定位的父元素进行定位;若没有则相对<html>)(left,right,top,bottom);与文档流无关,不占据空间(可能与其它元素重叠)
static:默认值。没有定位,元素出现在正常的文件流中(left,right,top,bottom,z-index无效!)
inherit:继承从父元素的position值
fixed示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>cascading style sheet</title>
<style>
#test{
width: 500px;
height: 100px;
position: fixed;
top: 0px;
left: 500px;
border: 1px solid burlywood;
background-color: #F2F2F2;
z-index: 2;
}
.test1{
margin-top: 100px;
}
.test1, .test2, .test3, .test4, .test5{
width: 200px;
height: 300px;
border: 1px solid black;
position: relative;
left: 500px;
background-color: gray;
}
</style>
</head>
<body>
<div id="test"></div>
<div class="test1">1</div>
<div class="test2">2</div>
<div class="test3">3</div>
<div class="test4">4</div>
<div class="test5">5</div> </body>
</html>
#test部分始终固定在上方,不发生移动。
relative示例:(原先占据的空间依然保留!)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>position:relative</title>
<style>
.test1, .test2, .test3{
width: 200px;
height: 200px;
border: 1px solid orange;
}
.test2{
border-color: black;
position: relative;
top:-100px;
left: 10px;
}
</style>
</head>
<body>
<div class="test1">this is part 1</div>
<div class="test2">this is part 2</div>
<div class="test3">this is part 3</div>
</body>
</html>
运行结果:(箭头所指这部分区域空间依然保留!!!)

凡是可能发生重叠的position属性,均能使用z-index!
- 没有指定z-index:代码后面的在上面(“后来者居上”);
- z-index越大的在上面!
注意:当使用position或者float属性时,通常<body>要预设定义margin和padding。这样可以避免在不同的浏览器中出现差异!
如果省略<!DOCTYPE>声明,IE8及以下版本会在右侧增加17px的外边距!这似乎是为了滚动条预留的空间!所以,请始终设置<!DOCTYPE>声明!!!
布局之水平居中:
1.元素居中对齐(比如<div>):
使用margin:0 auto;(上下margin为0,左右自动分配(居中!))!注意:此方法元素需设置width属性(除了width:100%)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>居中</title>
<style>
/* 预设置样式 */
body{
margin: 0;
padding: 0;
}
.test1{
border: 1px solid gold;
width: 150px;
height: 150px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="test1">this is test1</div>
</body>
</html>
2.图像居中对齐(先设置display:block;再margin:0 auto);
3.文本居中对齐(text-align:center;)
4.左右对齐:(①position:absolute;再设置left或right属性值②float:left;或者float:right)
布局之垂直居中:
方法一:line-height:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>居中</title>
<style>
/* 预设置样式 */
body{
margin: 0;
padding: 0;
}
.test1{
border: 1px solid gold;
width: 150px;
height: 150px;
line-height: 150px;
}
</style>
</head>
<body>
<div class="test1">this is test1</div>
</body>
</html>
这个方法有一个缺陷,适用于:“只有一行话”。原理:就是块元素(div)高度有多高,(行高)line-height就有多高!
不止一行话:

方法二:使用padding:
这种方法不会有上面line-height的“缺陷”,原理:好像就是"撑开"来一样!(个人比较推荐!)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>居中</title>
<style>
/* 预设置样式 */
body{
margin: 0;
padding: 0;
}
.test1{
border: 1px solid gold;
width: 200px;
padding: 100px 0px;
text-align: center;
}
</style>
</head>
<body>
<div class="test1">this is test1 this is test1 this is test1 this is test1 this is test1 </div>
</body>
</html>
方法三:使用display:flex;align-items:center;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>居中</title>
<style>
/* 预设置样式 */
body{
margin: 0;
padding: 0;
}
.test1{
border: 1px solid gold;
width: 200px;
height: 200px;
display: flex;
align-items: center;
text-align: center;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="test1">this is test1 this is test1 this is test1 this is test1 this is test1 </div>
</body>
</html>
方法四:使用position和transform
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>居中</title>
<style>
/* 预设置样式 */
body{
margin: 0;
padding: 0;
}
.test1{
border: 1px solid gold;
width: 200px;
height: 200px;
position: relative;
}
.test1 p{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="test1"> <p>this is test1</p> </div>
</body>
</html>
CSS position &居中(水平,垂直)的更多相关文章
- 解读CSS布局之-水平垂直居
对一个元素水平垂直居中,在我们的工作中是会经常遇到的,也是CSS布局中很重要的一部分,本文就来讲讲CSS水平垂直居中的一些方法.由于我们大搜车的日常工作中已经不再需要理会低版本IE,所以本文所贴出的方 ...
- CSS 基础 例子 水平 & 垂直对齐
一.元素居中对齐 margin:auto 水平居中对齐一个元素(如 <div>),即div本身在容器中的对齐,用margin:auto,而且,需要设置 width 属性(或者设置 100% ...
- CSS 中的各种居中 (水平、垂直)
导读: CSS 的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中.根据父.子元素的高度是否清楚,又会使得不同的居中用不同方法.本文就其中一些情况做下简单说明,以作笔记之用,仅供大 ...
- CSS 各类 块级元素 行级元素 水平 垂直 居中问题
元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...
- css3-7 如何让页面元素水平垂直都居中(元素定位要用css定位属性)
css3-7 如何让页面元素水平垂直都居中(元素定位要用css定位属性) 一.总结 一句话总结:元素定位要用css定位属性,而且一般脱离文档流更加好操作.先设置为绝对定位,上左都50%,然后margi ...
- CSS实现水平垂直同时居中的5种思路
× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 ...
- CSS实现水平垂直同时居中的6种思路
前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的6种思路 水平对齐+行高 [思路一]text-align + line-height实现单行文本水平垂直居中 <style ...
- (前端)面试300问之(2)CSS元素居中【水平、垂直、2者同时居中】
一 仅水平居中 1 行内元素 1)给父元素添加 text-align:center 即可 <div class="parent"> <span class=&qu ...
- CSS 布局 - 水平 & 垂直对齐
CSS 布局 - 水平 & 垂直对齐 一.元素居中对齐 要水平居中对齐一个元素(如 <div>), 可以使用 margin: auto;. 设置到元素的宽度将防止它溢出到容器的边缘 ...
随机推荐
- 内部排序->插入排序->希尔排序
文字描述 希尔排序又称缩小增量排序,也属于插入排序类,但在时间效率上较之前的插入排序有较大的改进. 从之前的直接插入排序的分析得知,时间复杂度为n*n, 有如下两个特点: (1)如果待排序记录本身就是 ...
- python各种模块,迭代器,生成器
从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能) 本质就是.py结尾的python文件(文件名:test.py,对应的模块名就是test) 包:用来从逻辑上组织模块的,本质就是一个目 ...
- Maven项目Update Project后JRE System Library自动变回1.5解决办法
最近在搭建Spring Boot项目<一步步搭建 Spring Boot maven 框架的工程>的时候,虽然设置JRE System Library为1.8,但是,当我 用 Maven ...
- vue指令v-html中使用过滤器filters功能
Vue 2.0 不再支持在 v-html 中使用过滤器 解决方法: 1:全局方法(推荐) 2:computed 属性 3:$options.filters(推荐) 1:使用全局方法: 可以在 Vue ...
- 腾讯互动课堂(Tencent Interact Class,TIC)SDK 词汇表
词汇表 https://cloud.tencent.com/document/product/266/11732 封装格式 封装格式(Format)是将已经编码压缩好的视频流和音频流按照一定的格式规范 ...
- python-面向对象-07_继承
继承 目标 单继承 多继承 面向对象三大特性 封装 根据 职责 将 属性 和 方法 封装 到一个抽象的 类 中 继承 实现代码的重用,相同的代码不需要重复的编写 多态 不同的对象调用相同的方法,产生不 ...
- int(1)和int(11)是否有区别?
MySQL类型关键字后面的括号内指定整数值的显示宽度(例如,INT(11)).该可选显示宽度规定用于显示宽度小于指定的列宽度的值时从左侧填满宽度.显示宽度并不限制可以在列内保存的值的范围,也不限制超过 ...
- double类型相减有小数误差
如 :19.9-9.9=9.9999999999999,而不是10double相减会转换成二进制,因double有效位数为 16位这就会出现存储小数位数不够的情况,这种情况下就会出现误差 //两个Do ...
- Number (int float bool complex)--》int 整型、二进制整型、八进制整型、十六进制整型
# ### Number (int float bool complex) # (1) int 整型 (正整数 0 负整数) intvar = 15 print(intvar) intvar = 0 ...
- [django]drf知识点梳理-搜索
什么是搜索? 譬如http://127.0.0.1:8000/User/?username=maotai-0 可以检索出想要的. 自己实现原始的搜索 重写下get_queryset方法 class U ...