布局系列
Block、inline、inline-block
Block:区块元素
block:
- 独占一行、左右不能够有元素与它并排,排序由上到下
- 宽度默认会填充父容器的可用空间
- 高度以里面的元素进行填充
- 可以设置width和height
- 常见标签
- div、p、h1-h6、ul
inline:内联元素
inline:
- 不独占一行、左右能够有元素与它并排
- 宽度根据它包含内容而定,不能用witch、height
- 常见标签
- span、a、strong
- span、a、strong
inline-block
可以设置高度和宽度、也可以进行并排
例子:超链接例子
a {
display: inline-block;
width: 80px;
height: 40px;
line-height: 40px;
text-align: center;
background-color: black;
color: blanchedalmond;
text-decoration: none;
border-radius: 25px;
}
对齐方法
<head>
<style>
div {
width: 50%;
/* 这两个属性可以将这个block居中 */
margin-left: auto;
margin-right: auto;
/*隐藏元素*/
display:none;
}
span {}
strong {
display: inline-block;
}
div,
strong,
span {
background-color: black;
color: white;
}
body {
/* 此时block只有里面的文字居中对齐,元素本身并没有水平居中 */
/* block本身并没有没有text-align属性,所以继承了父容器的text-align */
text-align: center;
}
</style>
</head>
<body>
<div>Block</div>
<span>Inline</span>
<br>
<strong>inline-block</strong>
</body>
居中
左右居中
上下居中
Position Absolute
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 50%;
/* 上下居中 */
position: absolute;
top: 50%;
left: 50%;
/* 如果只进行上面两部元素只是元素左上方对齐画面中心点 */
/* 向左向上移动 */
transform: translateX(-50%) translateY(-50%);
background-color: black;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div>Block</div>
</body>
Flexbox
Flexbox自带justfy-content(设定主轴)与align-items(设定)两个属性
body {
display: flex;
min-height: 100vh;
justify-content: center;
align-items: center;
}
div {
width: 50%;
background-color: black;
color: white;
text-align: center;
}
效果同上
Display Table
HTML的Table元素有一个vallign的属性,用来设定垂直方向的对齐
通过将其他元素设置为Table来进行使用
body {
display: table;
width: 100%;
min-height: 100vh;
margin: 0;
}
/* 成为表格的存储格 */
.cell {
display: table-cell;
/* 垂直居中 */
vertical-align: middle;
text-align: center;
}
.black {
width: 50%;
background-color: black;
color: white;
text-align: center;
}
- Position Absolute(对齐元素本身)
- Flexbox(对齐元素本身)
- Display Table(对齐元素本身)
盒子模型content、padding、border、margin
content:元素的内容
border:边框
padding:元素与边框的距离(内边距)
-
设定一个值:-top、-left、 -right、-bottom
-
两个值:上下、左右
-
三个值:上、左右、下
-
四个值:顺时针
margin:指其他元素与边框的距离(外边距)
如何分辨什么时候用margin和padding?
如果有背景颜色:padding是背景颜色会覆盖到的地方,margin不会
如果元素是可点击的(如超链接):padding的部分会纳入可点击范围,margin不会
对与inline元素的注意事项
margin:只对左右元素有效
padding:覆盖了上一行与下一行的元素了(边距重叠)
inline属性并不会影响垂直方向的版面布局
inline-block
正常预期
Margin auto:居中block元素
- 当margin-bottom遇上margin-top,就会将两个margin距离重叠,保留距离较大的尺寸
- 如果要不重叠,要在之间添加一个有高度的元素
Box-Sizing
默认是content-box
border-box:在设定包括padding或框线很有用
Position:定位
分类
- static:默认值
- absolute
- relative
- fixed
- sticky
static
static会跟随HTML排版的流程(flow)移动
Absolute
不会跟随HTML排版的流程移动,并没有影响absolute元素的位置
但如果absolute元素所在之容器是有滚动条的话,会随着滚动条移动
两份absolute如果位置相同会进行重叠
如果嵌套的话里面的那一层会跟着外面的那一层进行定位
relative
-
会跟随HTML排版流程去定位
-
还可以再透过top、left、right、bottom去调整位置(比static多的)
-
可以让absolute子晁素根据它的位置去定位(比static多的)
Fixed
fixed会定位到荧幕中的固定位置
如果fixed元素有设定top、left、right、bottom的属性,即使放在relative里面,fixed也会根据页面。即是body去定位
sticky
sticky会在卷动的过程中,当贴到顶部的时候,固定在顶部(top:0)
Grid 网页布局
- Flexbox属于一维的排版方式
- 而Grid则是二维的排版方式:可以一次控制两个方向
grid-template-rows:代表垂直方向的设定
grid-template-colunns:代表水平方向的设定
建议先定义row再定义column,原因在于grid-row
与grid-column
有一个简写的方法,名为grid-area
grid-column: 2 / span 3;
Grid Line(可以以命名划分)
grid- template-areas:区域划分