一 、基本选择器
基本选择器
元素选择器(就是标签名)
id选择器(属性名就是id)
类选择器(属性名就是class)
1.1元素选择器
元素选择器(标签名选择器)
在head中使用style标签引入
在其中声明元素选择器
html标签{
属性:属性值
}
<style type="text/css">
span{color: red;font-size: 100px}
</style>
1.2 id选择器[常用]
给需要修改样式的html元素添加ID属性标识
在head中使用style标签引入
在起中声明ID选择器
#id{ 属性:属性值 } ps: 特别注意!是 #id
创建id选择器:
<div id="s1">hello,everyone!</div>
<div id="s2">hello,everyone!</div>
<div id="s3">hello,everyone!</div>
根据id选择器进行html文件修饰
<style type="text/css">
#s1{color: red;font-size: 100px}
#s2{color: green;font-size: 100px}
#s3{color: blue;font-size: 100px}
</style>
1.3 class选择器[常用]
- 给需要修改样式的html元素添加class属性标识
- 在head中使用style标签引入
- 在其中声明class选择器
.class名{ 属性:属性值 } ps: 特别注意!!!! 是.class, 别忘了.
创建class选择器:
<div class="s1">hello,everyone!</div>
<div class="s2">hello,everyone!</div>
<div class="s3">hello,everyone!</div>
根据id选择器进行html文件修饰:
<style type="text/css">
.s1{color: purple;font-size: 100px}
.s2{color: pink;font-size: 100px}
.s3{color: yellow;font-size: 100px}
</style>
二 、属性选择器
- 根据元素的属性及属性值来选择元素
- 在head中使用style标签引入在其中声明
htm标签[属性='属性值']{ css属性:css属性值; } html标签[属性]{ css属性:css属性值; }
body内容:
<form name="login" action="#" method="get">
<font size="3">用户名:</font>
<input type="text" name="username" value="zhangsan"><br>
<font size="3">密码:</font>
<input type="password" name="password" value="123456"><br/>
<input type="submit" value="登录">
</form>
head中书写:
<style type="text/css">
input[type='text'] {
background-color: pink;
}
input[type='password'] {
background-color: yellow;
}
font[size] {
color: green
}
a[href] {
color: blue;
}
</style>
三 、CSS属性
3.1文字属性
| 属性名 | 描述 | 取值 |
|---|---|---|
| font-size | 设置字体大小(像素/百分比) | 数值 |
| font-family | 设置字体样式 | 默体 宋体 楷体等 |
| font-style | 设置斜体样式 | normal正常;italic斜体; |
| font-weight | 粗体样式 | 100~900数值;bold;bolder |
3.2文本属性
| 属性名 | 描述 | 取值 |
|---|---|---|
| color | 十六进制;表示颜色的英文单词; | 设置文本颜色 |
| text-indent | 缩进元素中文本的首行 | 5px缩进5像素;20%缩进父容器宽度的20% |
| text-decoration | 文本的装饰线 | none;underline;overline;blink;line-through |
| text-align | 文本水平对齐方式 | left;right;center |
| word-spacing | 英语单词之间的间隔,得有空格 | normal;固定值;像素 |
| line-height | 设置文本的行高 | normal;固定值;像素 |
3.3背景属性
| 属性名 | 描述 | 取值 |
|---|---|---|
| background-color | 设置背景色 | 16进制;用于表示颜色的英语单词; |
| background-image | 设置背景图片 | url('图片路径') |
| background-repeat | 设置背景图的平铺方向 | repeat-y;repeat-x;repeat;no-repeat; |
| background-position | 改变图象在背景中的位置 | top;bottom;left;right ; center; |
| background-size | 设置图片大小 | 像素;百分比 |
3.4列表属性
| 属性名 | 描述 | 取值 |
|---|---|---|
| list-style-type | 改变列表的标识类型 | disc等 |
| list-style-image | 用图像表示标识 | url("图片地址") |
3.5尺寸属性
width:设置元素的宽度,单位px%
height:设置元素的高度,单位px%
3.6显示属性
显示属性dispaly,以下是常用取值
none:不显示,即隐藏了
bloke:块级显示,占一行
inline:行级显示,占部分
3.7定位属性
使用的是css属性position,属性值有
relative(相对)
absolute(绝对)
fixed(固定)
使用了定位属性后,才可以使用top,bottom,left,right这些属性
四 、css盒子模型
主要作用为了布局,就是调整位置,根据相邻调整使页面看起来更美观
盒子模型图
- 关于内外边距的解释
- 内边距:内边距是指元素到边框的距离
- 外边距:外边距是指边框到浏览器页面的距离
4.1 边框相关属性
| 属性名 | 描述 | 取值 |
|---|---|---|
| border-style | 设置边框的样式 | solid;double;dashed;dotted等 |
| border-color | 设置边框颜色 | 16进制;用于表示颜色的英文; |
| border-width | 设置边框的粗细 | 数值 |
| border | 同时设置粗细,颜色,尺寸 |
4.2 外边距相关属性
margin: 外间距,边框和边框外层的元素的距离
| 属性名 | 描述 | 取值 |
|---|---|---|
| margin | 四个方向的距离 | top;right;bottom;lrft |
| margin-top | 上间距 | 数值 |
| margin-bottom | 下间距 | 数值 |
| margin-left | 左间距 | 数值 |
| margin-right | 右间距 | 数值 |
4.3 内边距相关属性
padding: 内间距,元素内容和边框之间的距离(top right bottom left)
| 属性值 | 描述 | 取值 |
|---|---|---|
| padding-left | 左间距 | 数值 |
| padding-right | 右间距 | 数值 |
| padding-top | 上间距 | 数值 |
| padding-bottom | 下间距 | 数值 |
| padding | 四个方向的距离 | left;right;top;bottom |