HTML/CSS 的layout
01_文档流
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 100px;
background-color: yellow;
}
.box2{
width: 100px;
background-color: red;
}
</style>
</head>
<body>
<!--
文档流(normal flow)
- 网页是一个多层的结构,一层摞着一层
- 通过CSS可以分别为每一层来设置样式
- 作为用户来讲只能看到最顶上一层
- 这些层中,最底下一层称为文档流,文档流是网页的基础
我们所创建的元素默认都是在文档流中进行排列
- 对于我们来元素主要有两个状态
在文件流中
不在文件流中
元素在文档流中有什么特点
- 块元素
- 块元素会在页面中独占一行
- 默认宽度是父元素的全部(会把父元素撑满)
- 默认高度是被内容撑开(子元素)
- 行内元素
- 行内元素不会独占页面的一行,只占自身大小
- 行内元素在页面中由左向右排列(书写习惯一致)
- 行内元素的默认宽高都是被内容撑开
-->
<div class="box1">I am box1</div>
<div class="box2"> I am box2</div>
<span>I am span1</span>
<span>I am span2</span>
</body>
</html>
02_盒模型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
/*
内容区(content),元素的所有子元素和文本内容都在内容区中排列
内容区的大小由width和height两个属性设置
width 设置内容区的宽度
height 设置内容区的高度
*/
width: 200px;
height: 200px;
background-color: #bfa;
/*
边框(border),边框属于盒子边缘,边框里边属于盒子内部,出了边框都是盒子的外部
边框的大小会影响整个盒子的大小
要设置边框,需要至少设置三个样式:
边框的宽度:border-width
边框的颜色:border-color
边框的样式:border-style
*/
border-color: red;
border-width: 10px;
border-style: solid;
}
</style>
</head>
<body>
<!--
盒模型,盒子模型,框模型(box model)
- CSS将页面中的所有元素都设置为了一个矩形的盒子
- 将元素设置为矩形的盒子后,对页面的布局就变成将不同的盒子摆放到不同的位置
- 没一个盒子都由以下几个部分组成
内容区(content)
内边框(padding)
边框(border)
外边框(margin)
-->
<div class="box1"></div>
</body>
</html>
03_盒子的边框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
background-color: #bfa;
width: 200px;
height: 200px;
/*
边框的宽度 border-width
边框的颜色 border-color
边框的样式 border-style
*/
/*
border-width:10px
默认值: 一般都是3个像素
border-width可以用来指定四个方向的边框的宽度
值的情况
四个值:上 右 下 左
三个值:上 左右 下
两个值: 上下 左右
一个值:上下左右
除了border-width还有一组 border-xxx-width
xxx可以是top right bottom left
用来单独指定某一个边的宽度
/* */
border-width: 10px;
/*
border-color用来指定边框的颜色,同样可以分别指定四个边的边框
规则和border-width一样
border-color也可以省略不写,如果省略不写则自动使用color的颜色值
*/
/* border-color: orange red yellow green;
/* */
/* border-style 指定边框的样式
sold 表示实线
dotted 点状虚线
dashed 虚线
double 双实线
border-style的默认值是none 表示没有边框
*/
}
/*
border简写属性,通过该属性可以同时设置边框所有的样式,并且没有顺序要求
除了border以外还有四个 border-xxx
border-top
border-right
border-bottom
border-left
*/
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
04_盒子模型—内边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
border: 10px orange solid;
padding-top: 100px;
padding-right: 100px;
padding-bottom: 100px;
padding-left: 100px;
}
/*
内边距(padding)
- 内容区和边框之间的距离是内边框
- 一共有四个方向的内边距
padding-top
padding-right
padding-bottom
padding-left
-内边距的设置会影响到盒子的大小
- 背景颜色会延伸到内边距上
一共盒子的可见框的大小,有内容区 内边框 和边框共同决定
所以在计算盒子大小时,需要将这三个区域加到一起计算
padding 内边距的简写属性,可以同时指定四个方向的内边距
规则和border-width一样
*/
.inner{
width: 100%;
height: 100%;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box1">
<div class="inner"></div>
</div>
</body>
</html>
05_盒子模型—外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
border: 10px red solid;
margin-top: 100px;
margin-left: 100px;
/*
外边距(margin)
- 外边距不会影响盒子可见框的大小
- 但是外边距会影响盒子的位置
- 一共有四个方向的外边距
margin-top
- 上外边距,设置一个正值,元素会向下移动
margin-right
margin-bottom
-下外边距,设置一个正值,其下边的元素会向下移动
margin-left
-左外边距,设置一个正值,元素hui'xiang
margin 也可以设置负值,如果是负值则元素会向相反的方向移动
- margin 的简写属性
margin 可以同时设置四个方向的外边距,用法和padding一样
- margin 会影响到盒子实际占用空间
*/
}
.box2{
width: 100px;
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
06_盒子的水平布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.outer{
width: 800px;
height: 200px;
border: 10px red solid;
}
.inner{
width: auto;
height: 200px;
background-color: #bfa;
margin-left: 200px;
/*
元素的水平方向的布局:
元素在其父元素中水平方向的位置由以下几个属性共同决定
margin-left
border-left
padding-left
width
padding-right
border-ringht
margin-right
一个元素在其父元素中,水平布局必须要满足以下等式
margin-left +border-left+padding-left+width+padding-right+border-ringht+margin-right=父元素内容区的宽度
- 以上等式必须满足,如果想加结果使等式不成立,则称为过渡约束,则等式会自动调整
- 调整的情况:
如果这七个值中没有为 auto 的情况,则浏览器会自动调整margin-right 值以使等式满足
- 这七个值中有三个值可设置为auto
width
margin-left
margin-right
- 如果某个值为auto,则会自动调整为auto的那个纸以使等式成立
- 如果将一个宽度和一个外边距设置为auto,则宽度会调整到最大,设置为auto的外边距会自动为0
- 如果将三个值都设置为auto ,则外边距都是0,宽度最大
- 如果将两个外边距设置为auto,宽度固定值,则会将外边距设置为相同的值
所以我们经常利用这个特点来使一个元素在其父元素中水平居中
示例:
width:xxxpx;
margin:0 auto;
*/
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>
07_垂直方向的布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=], initial-scale=1.0">
<title>Document</title>
<style>
.outer{
width: 200px;
background-color: #bfa;
height: 600px;
overflow: hidden;
overflow: scroll;
/*
默认情况下父元素的高度被内容撑开
子元素在父元素的内容区中排列的
如果子元素的大小超过了父元素,则子元素会从父元素中溢出
使用overflow 属性来设置父元素如何处理溢出的子元素
可选值:
visible 默认值 子元素会从父元素中溢出,在父元素外部的位置显示
hidden 溢出内容将会被裁剪不会显示
scroll 生成两个滚动条,通过滚动条来查看完整的内容
auto 根据需要生成滚动条
overflow-x:单独处理水平方向
overflow-y:单独处理垂直方向
*/
}
.inner{
width: 100px;
height: 700px;
background-color: yellow;
margin-bottom: 100px;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>
08_外边距的折叠
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1,.box2{
width: 200px;
height: 200px;
font-size: 100px;
}
.box1{
background-color: #bfa;
/* 设置一个下外边距 */
margin-bottom: 100px;
}
/*
垂直外边距的重叠(折叠)
- 相邻的垂直方向外边距会发生重叠现象
- 兄弟元素
- 兄弟元素间的相邻垂直外边距会取两者之间的较大值(两者都是正值时)
- 特殊情况:
如果相邻的外边距一正一负,则去两者的和
如果相邻的外边距都是负值,则取两者中绝对值较大的
- 兄弟元素之间的外边距的重叠,对于开发是有利的,所以我们不需要进行处理
- 父子元素
- 父子元素间相邻外边距,子元素的会传递给父元素(上外边距)
- 父子外边距的折叠会影响到页面的布局,必须要进行处理
*/
.box2{
/* 设置一个上外边距 */
background-color: orange;
margin-top: 100px;
}
.box3{
width: 200px;
height: 100px;
background-color: #bfa;
padding-top: 100px;
}
.box4{
width: 100px;
height: 100px;
background-color: orange;
/* margin-top: 100px; */
}
</style>
</head>
<body>
<div class="box3">
<div class="box4"></div>
</div>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
09_行内元素的折叠
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.s1{
background-color: yellow;
/*
行内元素的盒模型
- 行内元素不支持设置宽度和高度
- 行内元素可以设置padding,但是垂直方向padding不会影响页面的布局
- 行内元素可以设置border,垂直方向的border不会影响页面的布局
- 行内元素可以设置margin,垂直方向的margin不会影响布局
*/
/* width: 100px;
height: 100px; */
/* padding: 100px; */
margin: 100px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
a{
/*
display 用来设置元素显示的类型
可选值:
inline 将元素设置为行内元素
block 将元素设置为块元素
inline-block 将元素设置为行内块元素
行内块,既可以设置宽度和高度又不会独占一行
table 将元素设置为一个表格
none 元素不在页面中显示
visibility 用来设置元素的显示状态
可选值:
visible 默认值,元素在页面中正常显示
hidden 元素在页面中隐藏 不显示 ,但是依然占据页面的位置
*/
width: 100px;
height: 100px;
display: block;
visibility: hidden;
background-color: #bfa;
}
</style>
</head>
<body>
<a href="javascript:">超链接</a>
<span class="s1">我是span</span>
<div class="box1"></div>
</body>
</html>
10_默认样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>默认样式</title>
<!--
重置样式表:专门用来对浏览器的样式进行重置的
reset.css 直接去除了浏览器的默认样式
normalize.css 对默认样式进行统一
-->
<style>
/*
默认样式
- 通常情况,浏览器都会为元素设置一些默认样式
- 默认样式的存在会影响到页面的布局
通常情况下编写网页时必须要去除浏览器的默认样式(PC端的页面)
*/
.box1{
width: 100px;
height: 100px;
border: 1px solid black;
}
/* body{
margin: 0;
} */
/* ul{
margin: 0;
padding: 0; */
/*
去除项目符号
*/
/* list-style: none;
} */
/* p{
margin: 0;
} */
/* *{
margin: 0;
padding: 0;
} */
</style>
</head>
<body>
<div class="box1"></div>
<p>我是一个段落</p>
<p>我是一个段落</p>
<p>我是一个段落</p>
<ul>
<li>列表项1</li>
<li>列表项2</li>
<li>列表项3</li>
</ul>
</body>
</html>
11_文件的尺寸
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 100px;
height: 100px;
background-color: #bfa;
padding: 10px;
border: 10px red solid;
/*
默认情况下,盒子的可见框由内容区、内边框和边框共同决定
box-sizing 用来设置盒子尺寸的计算方式(设置width和height的作用)
可选值:
content-box 默认值,宽度和高度用来设置内容区的大小
border-box 宽度和高度用来设置整个盒子可见框的大小
width 和 height 指的是内容区 和内边距 和 边框的总大小
*/
box-sizing: content-box;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
12_轮廓和圆角
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
/*
box-shadow 用来设置元素的阴影效果,阴影不会影响页面的布局
第一个值 水平偏移量 设置阴影的水平位置 正值向右移动 负值向左移动
第二个值 垂直偏移量 设置阴影的垂直位置 正值向下移动 负值向上移动
第三个值 阴影的模糊半径
第四个值 阴影的颜色
*/
box-shadow:10px 10px 50px rgba(0, 0, 0,.3);
/*
outline 用来设置元素的轮廓线,用法和border一样
轮廓和边框不同的点,就是轮廓不会影响到可见框的大小
*/
}
/* .box1:hover{
outline: 10px red solid;
} */
.box2{
width: 200px;
height: 200px;
background-color: orange;
/* border-radius: 用来设置圆角 圆角设置的圆的半径大小; */
/* border-top-left-radius: 左上角; */
/* border-top-right-radius: */
/* border-bottom-left-radius: */
/* border-bottom-right-radius: 右下角 */
/* border-radius: 可以分别指定四个角的圆角
四个值 左上 右上 右下 左下
三个值 左上 右上/左下 右下
两个值 左上/右下 右上/左下
*/
/* border-top-left-radius:100px 50px; */
border-radius: 20px / 40px;
}
</style>
</head>
<body>
<!-- <div class="box1"></div>
<span>hello</span> -->
<div class="box2"></div>
</body>
</html>
13_重置样式表
/* 清除内外边距 /
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, / structural elements 结构元素 /
dl, dt, dd, ul, ol, li, / list elements 列表元素 /
pre, / text formatting elements 文本格式元素 /
fieldset, lengend, button, input, textarea, / form elements 表单元素 /
th, td { / table elements 表格元素 */
margin: 0;
padding: 0;
}
/* 设置默认字体 /
body,
button, input, select, textarea { / for ie /
/font: 12px/1 Tahoma, Helvetica, Arial, “宋体”, sans-serif;/
font: 12px/1 Tahoma, Helvetica, Arial, “\5b8b\4f53”, sans-serif; / 用 ascii 字符表示,使得在任何编码下都无问题 */
}
h1 { font-size: 18px; /* 18px / 12px = 1.5 */ }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
h4, h5, h6 { font-size: 100%; }
address, cite, dfn, em, var { font-style: normal; } /* 将斜体扶正 /
code, kbd, pre, samp, tt { font-family: “Courier New”, Courier, monospace; } / 统一等宽字体 /
small { font-size: 12px; } / 小于 12px 的中文很难阅读,让 small 正常化 */
/* 重置列表元素 */
ul, ol { list-style: none; }
/* 重置文本格式元素 */
a { text-decoration: none; }
a:hover { text-decoration: underline; }
abbr[title], acronym[title] { /* 注:1.ie6 不支持 abbr; 2.这里用了属性选择符,ie6 下无效果 */
border-bottom: 1px dotted;
cursor: help;
}
q:before, q:after { content: ‘’; }
/* 重置表单元素 /
legend { color: #000; } / for ie6 /
fieldset, img { border: none; } / img 搭车:让链接里的 img 无边框 /
/ 注:optgroup 无法扶正 /
button, input, select, textarea {
font-size: 100%; / 使得表单元素在 ie 下能继承字体大小 */
}
/* 重置表格元素 */
table {
border-collapse: collapse;
border-spacing: 0;
}
/* 重置 hr */
hr {
border: none;
height: 1px;
}