CSS布局相关概要
一、文档流
运用css布局首先要具备一些概念上的知识,文档流的概念充斥着布局的整个过程。浏览器渲染页面是有先后顺序的,其顺序是至上而下,根据HTML的文档结构进行渲染。
二、div+css
耳熟能详的div+css布局指的是:仅用容器标签div配合css样式进行布局。以往的table布局是不推荐使用的。
三、display: none / block/ inline / inline-block
设置元素的显示方式,以上为常见的取值。这是布局中非常重要的概念
block块元素:独自占据一行的元素,可控制宽高。如 div p h1~h6 ...
inline内联元素:不占据一行,,不能控制宽高,需要内容撑开。如 a span ...
inline-block: 内联的块元素,不占据一整行,但是可以控制宽高
CSS:
.box1{
display: block;
width: 100px;
height: 100px;
background-color: #aaa;
}
.box2{
display: inline;
width:100px;
height:100px;
background-color: #f45;
}
.box3{
display: inline-block;
width: 100px;
height: 100px;
background-color: #567;
}
HTML:
<div class="box1">容器一</div>
<div class="box2">容器二</div>
<div class="box3">容器三</div>

可以看出,inline和inline-block都不会独自占据一行,且inline无法控制宽高。
好了,以上就是布局需要熟悉掌握的概念,接下来介绍布局的具体方法和需要用到的属性。
四、浮动布局float:浮动的目的是使元素脱离文档流,按照自己的意愿进行布局。
1.取值:none,left,right。浮动只能向左或者向右
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
display: block;
width: 100px;
height: 100px;
margin-bottom: 5px;
background-color: #000;
}
div.box2{
background-color: #f34;
float: left;
}
div.box3{
background-color: #cd3;
float: right;
}
</style>
</head>
<body>
<div class="box1">容器一</div>
<div class="box2">容器二</div>
<div class="box3">容器三</div>
</body>
</html>
效果展示

可以看到,容器一仍然是不浮动的块元素,独自占据一行,而容器二和容器三分别向左右浮动,并出现在同一行,display:block,说明
2.浮动元素已经脱离文档流。
我们稍微更改上述代码,设置容器一浮动,二和三不浮动。
CSS:
div{
display: block;
width: 100px;
height: 100px;
margin-bottom: 5px;
background-color: #000;
}
div.box1{float: left;opacity:0.5;} /*容器一浮动,并设置了透明度*/
div.box2{background-color: #f34;}
div.box3{ackground-color: #cd3;text-align:right} /*将文本向右对齐*/
结果如下:

可以看到,设置了黑色背景的容器一变成了另外一种颜色,容器只剩下两个,为什么呢??其实原因是容器一设置了浮动,它原本的位置就不占用文档流的空间,后面的元素就会紧接着补上,所以容器二去到了容器一原本的位置,我们看到的容器一背景色实际上是容器一和二层叠所得的颜色,而文字“容器二”则围绕在浮动元素周围,不能补位。
结论:3.标准浏览器中,浮动元素脱离文档流,它后面的元素会占据浮动元素原本的位置。
4.文字内容会围绕在浮动元素周围
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{ width: 100px;
height: 100px;
opacity:0.5;
background-color: #000;
float: left;
}
</style>
</head>
<body>
<div class="box1"></div>你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊
</body>
</html>
效果演示

五、清除浮动clear
1.取值:left,right,both
其常用方法是添加一个div.clear标签,在需要的清除浮动的地方加入此标签
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>清除浮动</title>
<style>
.box{
width: 50px;
height: 50px;
background-color: #f57;
margin: 5px;
float: left;
}
.clear{
clear: both;
}
</style>
</head>
<body> <div class="box"></div>
<div class="box"></div> <div class="clear"></div> <!-- 清除浮动 --> <div class="box"></div>
<div class="box"></div>
<div class="box"></div> <div class="clear"></div> <!-- 清除浮动 --> <div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div> <div class="clear"></div> <!-- 清除浮动 --> <div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</body>
</html>
效果演示

六、position定位
1.取值:static 默认值,无定位
absolute 绝对定位,脱离文档流。若父元素无定位,则以body坐标原点进行定位;若position:relative,则以父元素左上角(原点)进行定位。
relative 相对定位,不脱离文档流。相对自己原来的位置进行定位。
fixed 固定定位,脱离文档流。相对浏览器窗口进行定位。
2.3种定位方式都是用top bottom left right进行定位
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/*html,
body{
height: 100%;
}
body{
background-color: #808;
}
.box{
position:absolute;
width: 100px;
height: 100px;
top: 10px;
left: 10px;
background-color: #f5f00c;
}*/
.box1{
width: 100px;
height: 100px;
position: relative;
top:50px;
left: 30px;
background-color: #ccc00c;
}
</style>
</head>
<body> <!-- <div class="box"></div> -->
<div class="box1"></div>
</body>
</html>
效果演示

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>absolute定位的两种情况</title>
<style>
.box,
.son{
position:absolute;
width: 100px;
height: 100px;
top: 10px;
left: 10px;
background-color: #f5f00c;
}
.father{
width: 150px;
height: 150px;
background-color: #ccc;
position: relative;
top: 150px;
left: 20px;
}
</style>
</head>
<body> <div class="box"></div>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
演示结果

3.z-index:规定元素的层叠关系 ,即在平面建立一个z轴,数值越大离用户越近,层叠在上,数值越小的层叠在下。元素必须有position属性时z-index才生效
CSS:
.box{
width: 100px;
height: 100px; }
.box:first-child{
background-color: #5ceb3f;
position: absolute;
top: 0px;
left: 0px;
z-index:1;
}
.box:last-child{
background-color: #808080;
position: absolute;
top: 50px;
left: 50px;
z-index:999;
}
HTML:
<div class="box"></div>
<div class="box"></div>
效果演示
z-index:1;

z-index:999;
CSS布局相关概要的更多相关文章
- amazeui笔记-CSS 布局相关
CSS 等分网格: 说明:.am-avg-sm-2 数字表示几等分 会将子元素 <li>的宽度设置为 50%. 只能用于 <ul> / <ol> 结构 辅助类: ...
- css布局相关:涉及到常见页面样式难点
一.display:table用法 Table:display:tableBody:table-row-group;Tr: table-row;Td: table-cell https://www.c ...
- 深入css布局篇(3)完结 — margin问题与格式化上下文
深入css布局(3) - margin问题与格式化上下文 在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深入学习一下 ...
- 深入css布局篇(2) — 定位与浮动
深入css布局(2) - 定位与浮动 在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深入学习一下css布局相关的知识 ...
- 深入css布局篇(1) — 盒模型 & 元素分类
深入css布局(1)-- 盒模型 & 元素分类 " 在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深 ...
- div+css布局记扎
实际开发网站过程中边碰壁边积累了一些div+css布局相关的小技巧,在这里做一些整理与大家一起探讨.本文章将间歇性更新. 1.div+css布局综述 div+css布局个人观点就是“盒子套盒子”的关系 ...
- amazeui学习笔记--css(布局相关1)--网格Grid
amazeui学习笔记--css(布局相关1)--网格Grid 一.总结 基本使用 1.div+class布局:amaze里面采取的就是div+class的布局方式 <div class=&q ...
- amazeui学习笔记--css(布局相关3)--辅助类Utility
amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...
- amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid
amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid 一.总结 1.与grid区别:网格中:am-g + am-u-xx-n 等分网格中只有一个: am-avg-sm-4(在u ...
随机推荐
- Python统计字符串中的中英文字符、数字空格,特殊字符
# -*- coding:utf8 -*- import string from collections import namedtuple def str_count(s): '''找出字符串中的中 ...
- 20145311 《Java程序设计》第2周学习总结
20145311 <Java程序设计>第2周学习总结 教材学习内容总结 3.1Java的类型分为基本类型(Primitive type)和类类型(Class type)基本类型: *整数: ...
- windows10如何安装cpu版本tensorflow
1.获取anaconda https://repo.continuum.io/archive/Anaconda3-2018.12-Windows-x86_64.exe (这个版本内置python3.7 ...
- 利用IntelliJ IDEA创建第一个Groovy工程
因为某些原因,需要学习一下Groovy.关于Groovy的入门教程请看这篇文章http://www.ibm.com/developerworks/cn/education/java/j-groovy/ ...
- Combobox绑定泛型字典时提示“复杂的 DataBinding 接受 IList 或 IListSource 作为数据源”的解决方法
一般情况下我们会将 DataTable 或 DataView 绑定到 Combobox 控件上,这时候进行数据绑定是没有问题的,因为DataTable 和 DataView 都继承了 IList 接口 ...
- Pandas 练习题
1. 使用 pandas 中的函数,下载上证综指过去一段时间的数据,进行数据探索. 上证综指,全称是上海证券综合指数,是以上证所挂牌上市的全部股票为计算范围,以发行量为权数的加权综合股价指数.这一指数 ...
- 上海仪电Azure Stack技术深入浅出系列1:谈Azure Stack在私有云/混合云生态中的定位
2.2 Azure Stack Azure Stack到2017年7月才提供GA版本,但目前还是可以通过技术预览版了解该技术.Azure Stack本质上是核心Azure服务的一个私有实例. Micr ...
- JAVA异常处理机制分析(上)
过去曾有一段时间关于java的异常处理机制曾经让我吃尽苦头,异常机制看似简单,原理,用法也仅仅如此,但是,用起来或是在使用一些框架的时候总会因为使用不当,造成灾难性后果. jdk异常处理机制 ...
- 桌面以及任务栏的所有浏览器,被加上了 hao.360.cn的网址
桌面以及任务栏的所有浏览器,被加上了hao.360.cn的网址 也不知道是安装了什么软件,中了360的招. 桌面以及任务栏的所有浏览器,被加上了hao.360.cn的网址. 这种东西,肯定是该死的36 ...
- SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析
SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...