CSS入门
格式
选择器 { 属性1:值1; 属性2:值2; }
div{
width: 50%; height: 100px; background-color: red;
}
单位
px —> 像素(pixel)、百分比(%)
基本样式
width(宽)、height(高)、background-color(背景色)
长度单位:
1、 px:像素
2、 %:百分比 (外容器为100px,内容器设置为50%,则是50px
CSS注释 :
/* CSS注释的内容 */
学习网站
https://www.w3cschool.cn/css/、https://www.w3cschool.cn/cssref/
一、css的三大样式
1、内联样式(也叫行间样式、行内样式)
在html标签上添加style属性来实现
举例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
</head>
<body>
<div style="width: 200px; height: 200px; background-color: red;">这是一个div块</div>
</body>
</html>
浏览器上的显示效果:
2、内部样式
在<style>标签内添加的样式
注: 内部样式的 优点 是可以 复用代码
举例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<style>
div{
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<!-- <div style="width: 200px; height: 200px; background-color: red;">这是一个div块</div> -->
<div>这是一个div块</div>
</body>
</html>
浏览器上的显示效果:
3、外部样式
引入一个单独的CSS文件(common.css文件)
引入方法:
1)通过link标签引入外部资源,rel属性指定资源跟页面的关系,href属性表示资源的地址;
<head>
<link rel="stylesheet" type="text/css" href="xxx.css" />
</head>
2)通过 @import 来引入外部样式(注:这种方式有很多问题,不建议使用)
<style>
@import url("./common.css");
</style>
关于link与import的区别可见博客:https://www.cnblogs.com/my–sunshine/p/6872224.html
3)link标签知识点扩展:
link属性对应的值如下:
二、css的颜色表示法
可参考:https://www.w3school.com.cn/cssref/css_colors.asp (了解颜色)
https://www.w3school.com.cn/cssref/css_colors_legal.asp(颜色值)
https://www.w3school.com.cn/cssref/css_colornames.asp(颜色名)
https://www.w3school.com.cn/cssref/css_colorsfull.asp(颜色16j进制)
1、单词表示法
red、orange、yellow、green、lime、blue、purple、black、white(红橙黄绿青蓝紫黑白)
特殊的颜色:transparent(透明色)
2、16进制表示法
其他更为丰富的颜色请参考:https://www.w3school.com.cn/tags/html_ref_colornames.asp
3、RGB三原色表示法
RGB 颜色值是这样规定的:rgb(red, green, blue)。每个参数 (red、green 以及 blue) 定义颜色的强度,可以是介于 0 与 255 之间的整数,或者是百分比值(从 0% 到 100%)。
举例说,rgb(0,0,255) 值显示为蓝色,这是因为 blue 参数被设置为最高值(255),而其他被设置为 0。
4、RGBA表示法
RGBA 颜色值是 RGB 颜色值的扩展,带有一个 alpha 通道 - 它规定了对象的不透明度。
RGBA 颜色值是这样规定的:rgba(red, green, blue, alpha)。alpha 参数是介于 0.0(完全透明)与 1.0(完全不透明)的数字。
background-color:rgba(255,0,0,0.5);
三、css背景样式
-
background-color :背景颜色
-
background-image :背景图片
url(背景地址)
默认:会水平垂直都铺满背景图 -
background-repeat : 背景图片的平铺方式
repeat-x : x轴平铺,在水平方向重复
repeat-y : y轴平铺,在垂直方向重复
repeat : x轴、y轴都进行平铺( 默认值 )
no-repeat : 都不平铺,背景图仅显示一次 -
background-postion : 背景图片的位置
x | y: number(px、%) | 单词
x : left、center、right
y : top、center、bottom -
background-attachment : 背景图随滚动条的移动方式
scroll: 背景图按照当前元素的位置进行偏移( 默认值 )
fixed : 背景图按照浏览器进行偏移
可参考:https://www.w3school.com.cn/cssref/index.asp#background
四、css边框样式
-
border-style :边框样式
solid:实线
dashed:虚线
dotted:点线 -
border-width :边框大小(1px)
thin:细
medium:中等粗细
thick:粗 -
border-color : 边框颜色
-
border : 一行设置多个(border:5px solid red;)
-
border-left-style: 设置单边的(border-left-style: dashed)
可参考:https://www.w3school.com.cn/cssref/index.asp#border
五、css字体样式
CSS 字体样式包括 字体类型、大小、粗细、风格(如斜体)和颜色。
1、font-family: 字体类型
英文字体: Arial、Time New Roman
中文字体: 微软宋体(Microsoft YaHei)、宋体(SimSun)
body{
font-family: PingFangSC-Regular, Verdana, Arial, 微软雅黑, 宋体;
font-size: 14px;
}
2、font-size: 字体大小
像素: 12px、14px、16px ( 一般为偶数)
百分比: 50%、20%、100%
英文: xx-small(最小)、x-small(较小)、small、medium、large、x-large(较大)、xx-large(最大)
相较于默认大小的对比
h1 {font-size:250%;}
h2 {font-size:large;;}
p {font-size: 14px;}
3、font-weight: 字体粗细
数字: 100、200、… 、900
英文: normal(相当于100-500)、bold(相当于600-900)、lighter(更细)、bolder(更粗)
p{font-weight:normal;}
p{font-weight:bold;}
p{font-weight:900;}
4、font-style: 字体风格
- normal - 文本正常显示
- italic - 文本斜体显示
- oblique - 文本倾斜显示, 用的少
p{font-style:normal;}
p{font-style:italic;}
p{font-style:oblique;}
italic 和 oblique 的区别
- italic 带有倾斜属性的字体的才可以设置倾斜操作(如果某些字体不带有倾斜属性,即使设置了也不起作用)
- oblique 没有倾斜属性的字体也可以设置倾斜操作
5、color: 字体颜色
p { color: red; }
可参考:https://www.w3school.com.cn/cssref/index.asp#font
六、css文本样式
CSS 字体样式包括 文本装饰和文本大小写(针对英文)、文本缩进、文本对齐、文本行高、文本间距与英文折行等
1、文本装饰(text-decoration)
- underline - 下划线
- overline - 上划线
- line-through - 中划线
- blink - 闪烁
- none
p {
text-decoration: line-through;
text-transform: uppercase;
}
p.one {
text-decoration: underline;
}
2、文本变换(针对英文)(text-transform)
- uppercase - 大写
- lowercase - 小写
- capitalize - 首字母大写
- none
span.lower {
text-transform: lowercase;
}
span.upper {
text-transform: uppercase;
}
span.capitalize {
text-transform: capitalize;
}
3、文本缩进(text-indent)
- 数字: 10px
- 百分比: 20%
p {
text-indent: 50px;
}
4、文本对齐(text-align)
- left - 左对齐
- right - 右对齐
- center - 居中
- justify - 两端对齐
h1 { text-align:center }
h2 { text-align:left }
h3 { text-align:right }
5、文本行高(line-height)
- 数字: px | scale()
- 百分比: 20%
p.small { line-height: 2; } // scale = 2 , 也可以设置为小数1.5等
p.big { line-height: 200%; }
6、文本间距
- letter-spacing 字符间距 ( normal | 2px )
- word-spacing 单词间距 ( normal | 2px )
h1 {letter-spacing:2px; }
h2 {letter-spacing:-3px; }
p { word-spacing:30px; }
7、英文折行
div {
word-break:break-all; // 强制换行
word-wrap:break-word; // 部分换行,对于末尾出现很长的一些单词时,可能存在一些空白区域
}
可参考:https://www.w3cschool.cn/css/html-css-text.html
https://www.w3school.com.cn/cssref/index.asp#text
七、css复合样式
复合样式有利于简化代码,写法是通过空格方式实现的。
注意:background、border无需考虑写的顺序,但是font需要考虑(至少需要两个值:size family )。
font的写法顺序:
weight style size family
style weight size family
weight style size/line-height family
举例:
div {
background: red url(./img/dog.png) no-repeat 20px center
/*border: 2px red solid;*/
border-right: red solid 20px;
font: bold italic 30px/100px 宋体;
}
复合样式写法注意事项:
- 一个CSS属性只控制一种样式,叫做单一样式
- 一个CSS属性控制多种样式,叫做复合样式
- 尽量不要两种样式混合写,如果非要混合写,那么一定要先写复合样式再写单一样式