【Web前端】CSS(二)——常用样式、盒模型与弹性布局
CSS属性有很多,可以参考文档:CSS 参考手册
1.字体属性
1.1 设置字体
在 CSS 中使用 font-family 可以设置字体
p {
font-family: '微软雅黑';
font-family: 'Microsoft YaHei';
}
- 字体名称可以使用中文,但不建议
- 多个字体之间使用逗号分隔(从左到右查找字体,如果都找不到则会使用默认字体)
- 如果字体名有空格,使用引号包裹
- 建议使用常见字体,否则兼容性不好
<style>
.font-family .one {
font-family: 'Microsoft YaHei';
}
.font-family .two {
font-family: '宋体';
}
</style>
<div class="font-family">
<div class="one">
这是微软雅黑
</div>
<div class="two">
这是宋体
</div>
</div>

1.2 大小
p {
font-size: 20px;
}
- 不同浏览器的默认字号不同,最好给一个明确值
- 可以给 body 标签使用 font-size
- 单位 px 不能省略
- 标题标签需要单独指定大小
实际上它设置的是字体中字符框的高度;实际的字符字型可能比这些框高或矮
<style>
.font-size .one {
font-size: 40px;
}
.font-size .two {
font-size: 20px;
}
</style>
<div class="font-size">
<div class="one">
大大大
</div>
<div class="two">
小小小
</div>
</div>

1.3 粗细
p {
font-weight: bold;
font-weight: 700;
}
- 可以使用数字表示粗细
- 700 == bold,400是不变粗 == normal
- 取值范围是 100 -> 900
<style>
.font-weight .one {
font-weight: 900;
}
.font-weight .two {
font-weight: 200;
}
</style>
<div class="font-weight">
<div class="one">
粗粗粗
</div>
<div class="two">
细细细
</div>
</div>

1.4 文字样式
/* 设置倾斜 */
font-style: italic;
/* 取消倾斜 */
font-style: normal;
2.文本属性
2.1 文本颜色
认识 RGB
我们的显示器是由很多“像素”构成的,每个像素视为一个点,这个点就能反映出一个具体的颜色
我们使用R(red),G(green),B(blue)的方式表示颜色(色光三原色),三种颜色按照不同的比例搭配,就能混合出各种五彩斑斓的效果
计算机中针对 R、G、B 三个分量,分别用一个字节表示(8个比特位,表示的范围是 0-255,十六进制表示为 00-FF)
数值越大,表示该分量的颜色就越浓,255,255,255 表示白色,0,0,0 表示黑色
设置文本颜色
color: red;
color: #ff0000;
color: rgb(255, 00, 00);
鼠标悬停在 vscode 的颜色上,会出现颜色选择器,可以手动调整颜色
color属性值的写法:
- 预定义的颜色值
- [最常用]十六进制形式
- RGB 方式
十六进制表示颜色,如果两两相同就可以用一个来表示: #ff00ff -> #f0f
2.2 文本对齐
控制文字水平方向的对齐
不光能控制文本对齐,也能控制图片等元素居中或者靠右
text-align: [值];
- center 居中对齐
- left 左对齐
- right 右对齐
<style>
.font-align .one {
text-align: left;
}
.font-align .two {
text-align: center;
}
.font-align .three {
text-align: right;
}
</style>
<div class="font-align">
<div class="one">
左对齐
</div>
<div class="two">
居中对齐
</div>
<div class="three">
右对齐
</div>
</div>

2.3 文本装饰
text-decoration: [值];
常用取值:
- underline 下划线(常用)
- none 啥都没有,可以用来给 a 标签去掉下划线
- overline 上划线(不常用)
- line-through 删除线(不常用)
<style>
.font-decoration .one {
text-decoration: none;
}
.font-decoration .two {
text-decoration: underline;
}
.font-decoration .three {
text-decoration: overline;
}
.font-decoration .four {
text-decoration: line-through;
}
</style>
<div class="font-decoration">
<div class="one">
啥都没有
</div>
<div class="two">
下划线
</div>
<div class="three">
上划线
</div>
<div class="four">
删除线
</div>
</div>

2.4 文本缩进
控制段落的首行缩进(其他行不影响)
text-indent: [值];
- 单位可以使用 px 或者 em
- 使用 em 作为单位更好,1个 em 就是当前元素的文字大小
- 缩进可以是负的,表示向左缩进
<style>
.text-indent .one {
text-indent: 2em;
}
.text-indent .two {
text-indent: -2em;
}
</style>
<div class="text-indent">
<div class="one">
正常缩进
</div>
<div class="two">
反向缩进
</div>
</div>

2.5 行高
行高指的是上下文本之间的基线距离
HTML中展示文字涉及到这几个基准线
- 顶线
- 中线
- 基线(相当于英语四线格的倒数第二条线)
- 底线
内容区:底线和顶线包裹的区域,即下图深灰色背景区域
基线之间的距离 = 顶线间距离 = 底线间距离 = 中线间距离
line-height: [值];
<style>
.line-height .one {
line-height: 200px;
}
</style>
<div class="line-height">
<div>
上一行
</div>
<div class="one">
中间行
</div>
<div>
下一行
</div>
</div>
默认行高:
行高设置为200px:
注意1:行高 = 上边距 + 下边距 + 字体大小
上下边距是相等的,此处字体大小是16px,行高40px,上下边距分别是12px
<style>
.line-height .one {
line-height: 40px;
font-size: 16px;
}
</style>
<div class="line-height">
<div>
上一行
</div>
<div class="one">
中间行
</div>
<div>
下一行
</div>
</div>
注意2:行高也可以取 normal 等值
这个取决于浏览器的实现,chrome 上 normal 为 21px
注意3:行高与元素等高度就可以实现文字垂直居中
<style>
.line-height .one {
line-height: 100px;
height: 100px;
}
</style>
<div class="line-height">
<div>
上一行
</div>
<div class="one">
中间行
</div>
<div>
下一行
</div>
</div>

3.背景属性
3.1 背景颜色
background-color: [指定颜色];
默认是 transparent (透明)的,可以通过设置颜色的方式修改
<style>
body {
background-color: #66ccff;
}
.bgc .one {
background-color: red;
}
.bgc .two {
background-color: green;
}
.bgc .three {
background-color: transparent;
}
</style>
<div class="bgc">
<div class="one">红色背景</div>
<div class="two">绿色背景</div>
<div class="three">透明背景</div>
</div>

3.2 背景图片
background-image: url(...);
比 image 更方便控制位置(图片在盒子中的位置)
注意:
- url不要遗漏
- url可以是绝对路径,也可以是相对路径
- url可以加引号也可以不加
<style>
.bgi .one {
background-image: url(img/background-image.jpg);
}
</style>
<div class="bgi">
<div class="one">背景图片</div>
</div>

这里设置背景图片后只有一条,是因为div太小,可以给div设置 width 和 height 来调整大小
3.3 背景平铺
background-repeat: [平铺方式]
重要取值:
- repeat 平铺
- no-repeat 不平铺
- repeat-x 水平平铺
- repeat-y 垂直平铺
默认是repeat
背景颜色和背景图片可以同时存在,背景图片在背景颜色的上方
<style>
.bgr .one {
background-image: url(img/cat.png);
height: 300px;
background-repeat: no-repeat;
}
.bgr .two {
background-image: url(img/cat.png);
height: 300px;
background-repeat: repeat-x;
}
.bgr .three {
background-image: url(img/cat.png);
height: 300px;
background-repeat: repeat-y;
}
</style>
<div class="bgr">
<div class="one">不平铺</div>
<div class="two">水平平铺</div>
<div class="three">垂直平铺</div>
</div>

3.4 背景位置
background-position: x y;
修改图片的位置
参数有三种风格:
- 方位名词:top,lrft,right,bottom
- 精确单位:坐标或者百分比(以左上角为原点)
- 混合单位:同时包含方位名词和精确单位
<style>
.bgp .one {
background-image: url(img/cat.png);
height: 500px;
background-repeat: no-repeat;
background-position: center;
}
</style>
<div class="bgp">
<div class="one">背景居中</div>
</div>

注意:
- 如果参数的两个值都是方位名词,则前后顺序无关(top,left,right,bottom等效)
- 如果只指定了一个方位名词,则第二个默认居中
- 如果参数是精确值,则第一个值是 x,第二个值是 y
- 如果参数是精确值且只给了一个数值,则该数值一定是 x 坐标,另一个默认垂直居中
- 如果参数是混合单位,则第一个值一定为 x ,第二个值为 y
3.5 背景尺寸
background-size: length|percentage|cover|contain;
- 可以填具体的数值:如 40px 60px,表示宽度为 40px,高度为 60px
- 也可以填百分比:按照父元素的尺寸设置
- cover:把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。背景图像的某些部分也许无法显示在背景定位区域中
- contain:把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域
<style>
.bgp .one {
width: 500px;
height: 300px;
background-image: url(img/cat.png);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
</style>
<div class="bgp">
<div class="one">背景尺寸cover</div>
</div>

<style>
.bgp .one {
width: 500px;
height: 300px;
background-image: url(img/cat.png);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
</style>
<div class="bgp">
<div class="one">背景尺寸contain</div>
</div>

4.圆角矩形
通过 border-radius 使边框带圆角效果
4.1 基本用法
border-radius: length;
length 是内切圆的半径,数值越大,弧线越强烈
length也可以使用百分比
<style>
div {
width: 200px;
height: 100px;
border: 2px solid #66ccff;
border-radius: 10px;
}
</style>

<style>
div {
width: 200px;
height: 100px;
border: 2px solid #66ccff;
border-radius: 30px;
}
</style>

4.2 生成圆形
让 border-radius 的值为正方形宽度的一半即可
<style>
div {
width: 100px;
height: 100px;
border: 2px solid #66ccff;
border-radius: 50px;
}
</style>

4.3 生成胶囊形
让 border-radius 的值为矩形高度的一半即可
<style>
div {
width: 200px;
height: 100px;
border: 2px solid #66ccff;
border-radius: 50px;
}
</style>

4.4 展开写法
border-radius 是一个复合写法,实际上可以对四个叫分别设置
border-radius: 2em;
等价于
border-top-left-radius: 2em;
border-top-right-radius: 2em;
border-bottom-right-radius: 2em;
border-bottom-left-radius: 2em;
border-radius: 20px 40px 20px 40px;
等价于(从左上角开始按照顺时针排列)
border-top-left-radius: 20px;
border-top-right-radius: 40px;
border-bottom-right-radius: 20px;
border-bottom-left-radius: 40px;
5.元素的显示模式
在 CSS 中,HTML的标签显示模式有很多
此处重点介绍两个
- 块级元素
- 行内元素
5.1 块级元素
常见的元素
h1 - h6
p
div
ul
ol
li
···
特点:
- 独占一行
- 高度、宽度、内外边距、行高都可以控制
- 宽度默认是父级元素宽度的 100%(和父级元素一样宽)
- 是一个容器(盒子),里面可以放块级元素和行内元素
<style>
.demo1 .parent {
width: 500px;
height: 500px;
background-color: green;
}
.demo1 .child {
/* 不写width:默认和父元素一样宽 */
/* 不写height:默认为0 */
background-color: red;
}
</style>
<div class="demo1">
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
</div>

将 .demo1 .child 中的 height 设置为 200px
注意:
- 文字类的元素内不能使用块级元素
- p 标签主要用于存放文字,内部不能放块级元素,尤其是 div
5.2 行内元素
常见的元素
a
strong
b
em
i
del
s
ins
u
span
特点:
- 不独占一行,一行可以显示多个
- 设置高度、宽度、行高无效
- 左右外边距有效(上下无效)内边距有效
- 默认宽度就是本身的内容
- 行内元素只能容纳文本和其他行内元素,不能放块级元素
<style>
.demo2 span {
width: 200px;
height: 200px;
background-color: red;
}
</style>
<div class="demo2">
<span>child1</span>
<span>child2</span>
<span>child3</span>
</div>

注意:
- a 标签中不能再放 a 标签
- a 标签内可以放块级元素,但更建议先把 a 标签转换成块级元素
5.3 块级元素和行内元素的区别
- 块级元素独占一行,行内元素不独占一行
- 块级元素可以设置宽高,行内元素不能设置宽高
- 块级元素四个方向都能设置内外边距,行内元素垂直方向不能设置
5.4 改变显示模式
使用 display 属性可以修改元素的显示模式
可以把 div 等变成行内元素,也可以把 a, span 等变成块级元素
- display: block 改成块级元素(最常用)
- display: inline 改成行内元素(很少用)
- display: inline-block 改成行内块元素
<style>
a {
display: block;
}
</style>
<a href="#">链接1</a>
<a href="#">链接2</a>

6.盒模型
每个 HTML 元素相当于一个矩形的“盒子”
这个盒子由这几个部分组成
- 边框 border
- 内容 content
- 内边距 padding
- 外边距 margin

6.1 边框
基础属性
- 粗细:border-width
- 样式:border-style 默认没边框,solid实线边框,dashed虚线边框,dotted点线边框
- 颜色:border-color
<div>test</div>
div {
width: 500px;
height: 250px;
border-width: 10px;
border-style: solid;
border-color: green;
}
支持简写,没有顺序要求
border: 1px solid red;
可以改四个方向的任意边框
border-top/bottom/left/right
边框会撑大盒子
可以看到,width,height 是 500250,但最终整个盒子的大小是 520270,边框 10 个像素相当于扩大了大小、
通过 box-sizing 属性可以修改浏览器的行为,使边框不再撑大盒子
*为通配符选择器
* {
box-sizing: border-box;
}

6.2 内边距
padding设置内容与边框之间的距离
基础写法
默认内容是顶着边框来放置的,用 padding 来控制这个距离
可以给四个方向都加上边距
- padding-top
- padding-bottom
- padding-left
- padding-right
<div>test</div>
div {
width: 300px;
height: 200px;
}

加上 padding 后
div {
width: 300px;
height: 200px;
padding-top: 5px;
padding-left: 10px;
}

此时可以看到带了一个绿色的内边框
注意:
- 整个盒子的大小从原来的 300 * 200 -> 310 * 205,说明内边距也会影响到盒子大小(撑大盒子)
使用box-sizing: border-box属性也可以使内边距不再撑大盒子(和border类似)
复合写法
可以把多个方向的 padding 合并到一起
padding: 5px; 表示四个方向都是 5px
padding: 5px 10px; 表示上下内边距是 5px,左右内边距是 10px
padding: 5px 10px 20px; 表示上边距 5px,左右内边距为 10px,下内边距是 20px
padding: 5px 10px 20px 30px; 从上内边距开始顺时针旋转
6.3 外边距
基础写法
控制盒子和盒子之间的距离
可以给四个方向都加上边距
- margin-top
- margin-bottom
- margin-left
- margin-right
复合写法
规则同 padding
margin: 5px; 表示四个方向都是 5px
margin: 5px 10px; 表示上下内边距是 5px,左右内边距是 10px
margin: 5px 10px 20px; 表示上边距 5px,左右内边距为 10px,下内边距是 20px
margin: 5px 10px 20px 30px; 从上内边距开始顺时针旋转
块级元素水平居中
前提:
- 指定宽度(如果不指定宽度,默认和父元素一致)
- 把水平 margin 设为 auto
三种写法均可
margin-left: auto; margin-right: auto;
margin: auto;
margin: 0 auto;
注意:
这个水平居中方式和 text-align 不一样
margin: auto; 是给块级元素用的
text-align: center; 是让行内元素或者行内块元素居中的
另外,对于垂直居中,不能使用“上下 margin 为 auto 的方式”
去除浏览器默认样式
浏览器会给元素加上一些默认的样式, 尤其是内外边距。不同浏览器的默认样式存在差别。
为了保证代码在不同的浏览器上都能按照统一的样式显示,往往我们会去除浏览器默认样式。
使用通配符选择器即可完成这件事情。
* {
box-sizing: border-box;
}
7.弹性布局
在前端中为了实现页面的布局效果而使用的技术手段
创建一个div,内部包含三个span
<style>
div {
width: 700px;
height: 700px;
background-color: red;
}
span {
width: 100px;
height: 100px;
background-color: green;
}
</style>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
此时看到的效果为:
当我们给 div 加上 display: flex; 之后,效果为:
此时可以看到,span有了高度
再给 div 加上 justify-content: space-around; 此时效果为:
此时可以看到这些 span 在水平方向均匀分布
把 justify-content: space-around; 改为 justify-content: flex-end; 可以看到此时三个元素在右侧显示了
flex 布局基本概念
flex 是 flexible box 的缩写,意思是“弹性盒子”
任何一个 html 元素都可以指定为 display: flex; 来完成弹性布局
flex 布局的本质是给父盒子添加 display: flex; 属性,来控制子盒子的位置和排列方式
基础概念:
- 被设置为
display: flex;属性的元素,成为 flex container - 它的所有子元素成为该容器的成员,称为 flex item
- flex item 可以纵向排列,也可以横向排列,称为 flex direction(主轴)

注意:
当父级元素设置为 display: flex; 时,子元素的 float, clear, vertical-align都会失效
常用属性
justify-content
设置主轴上的子元素排列方式
属性取值:
| 值 | 描述 |
|---|---|
| flex-start | 默认值,项目位于容器的开头 |
| flex-end | 项目位于容器的结尾 |
| center | 项目位于容器的中央 |
| space-between | 项目在行与行之间留有间隙 |
| space-around | 项目在行之前,行之间和行之后留有空间 |
代码示例
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
<style>
div {
width: 100%;
height: 150%;
background-color: red;
display: flex;
}
span {
width: 100px;
height: 100px;
background-color: green;
}
</style>
未指定 justify-content 时, 默认按照从左到右的方向布局

设置 justify-content: flex-end , 此时元素都排列到右侧了

设置 justify-content: center , 此时元素居中排列

设置 justify-content: space-around;
平分了剩余空间.

设置 justify-content: space-between;
先两边元素贴近边缘, 再平分剩余空间

aligh-items
设置侧轴上的元素排列方式
| 值 | 描述 |
|---|---|
| stretch | 默认值,行拉伸以占据剩余空间 |
| center | 朝着弹性容器的中央对行打包 |
| flex-start | 朝着弹性容器的开头对行打包 |
| flex-end | 朝着弹性容器的结尾对行打包 |
| space-between | 行均匀分布在弹性容器中 |
| space-around | 行均匀分布在弹性容器中,两段各占一半 |
| 取值和 justify-content 差不多 |
理解 stretch (拉伸):
这个是 align-content 的默认值. 意思是如果子元素没有被显式指定高度, 那么就会填充满父元素的高度.
形如:
<div>
<span>1</span>s
<span>2</span>
<span>3</span>
</div>
<style>
div {
width: 500px;
height: 500px;
background-color: red;
display: flex;
justify-content: space-around;
}
div span {
width: 150px;
height: 100px;
background-color: green;
}
</style>

可以使用 align-items 实现垂直居中
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
<style>
div {
width: 500px;
height: 500px;
background-color: red;
display: flex;
justify-content: space-around;
align-items: center;
}
div span {
width: 150px;
height: 100px;
background-color: green;
}
</style>

注意:
align-items 只能针对单行元素来实现,如果有多行元素,就需要使用 item-contents
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐

所有评论(0)