CSS篇(伪元素选择器、CSS相关属性(文字、背景)、CSS盒子模型、浮动、定位) 第四十七天 2018.12.10
CSS篇
伪类选择器
伪类选择器(通过状态区分,标签是否点击过,访问过和未访问状态不一样)
浏览器上点击过的标签,下次打开浏览器时间和没点击过得标签颜色不一样,网站不存在无跳转成功时间连接不变
可以通过伪类标签控制上面颜色变化
hover使用最多,input偶尔使用,其他基本不使用
/* 未访问的链接 */
a:link {
color: #FF0000
}
/* 已访问的链接 */
a:visited {
color: #00FF00
}
/* 鼠标移动到链接上 */
a:hover {
color: #FF00FF
}
/* 选定的链接 */
a:active {
color: #0000FF
}
/*input输入框获取焦点时样式*/
input:focus {
outline: none;
background-color: #eee;
}
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>伪类选择器示例</title> <style> /*连接没有被点击过,用link控制*/ a:link { color: burlywood; } /*连接被点击过,用visited控制*/ a:visited { color: green; } /*鼠标移上去,会触发hover状态,移开时状态恢复*/ a:hover { color: blueviolet; } /*被选定,移上去不变色,点下去变色*/ a:active { color: deeppink; } #d1 { color: grey; } #d1:hover { color: green; } /*input获取光标时*/ /*outline为外边的边框,background为背景色,点击获取光标时改变背景色*/ input:focus { outline: 0; background-color: deeppink; } </style> </head> <body> <a href="http://www.sogo.com">搜狗</a> <a href="http://www.sohu.com">搜狐</a> <div id="d1">我是div标签</div> <input type="text"> </body> </html>
伪元素选择器
首字设置特殊样式(first-letter)
p:first-letter {
font-size: 48px;
color: red;
}
通过CSS添加内容(before)
/*在每个<p>元素之前插入内容*/
p:before {
content:"*";
color:red;
}
通过CSS添加内容(after)
/*在每个<p>元素之后插入内容*/
p:after {
content:"[?]";
color:blue;
}
before和after多用于清除浮动。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>伪元素选择器示例</title> <style> p:first-letter { # 第一个字 font-size: 48px; # 字体大小 color: red; # 字体颜色 } # 通过CSS给HTML添加内容---->原则上不好,不过添加方便 .c1:before { # 通过类选择器,来找到标签,before在内部前面 content: "*"; # 通过CSS添加内容 color: red; } .c1:after { content: "[?]"; # 超链接---->用JS绑定事件跳转 color: blue; } p { # p标签内字体 font-size: 30px; } </style> </head> <body> <p> 在苍茫的大海上,狂风卷积着乌云. 在苍茫的大海上,狂风卷积着乌云. 在苍茫的大海上,狂风卷积着乌云. 在苍茫的大海上,狂风卷积着乌云. 在苍茫的大海上,狂风卷积着乌云. 在苍茫的大海上,狂风卷积着乌云.在苍茫的大海上,狂风卷积着乌云. </p> <p class="c1">在苍茫的大海上,狂风卷积着乌云.</p> <p class="c1">在苍茫的大海上,狂风卷积着乌云.</p> <p class="c1">在苍茫的大海上,狂风卷积着乌云.</p> </body> </html>
CSS属性相关(修改样式)
宽和高(块级标签才能设置)
width属性可以为元素设置宽度。
height属性可以为元素设置高度。
块级标签才能设置宽度,内联标签的宽度由内容来决定。
字体属性
字体样式(font-family)
Microsoft Yahei(微软雅黑)
body {
font-family: "Microsoft Yahei", "微软雅黑", "Arial", sans-serif
} # 依次向后选择默认字体(第一个电脑无(或浏览器不支持),向后推移一位,再无再后推,最后为电脑默认字体)
字体大小(font-size)
* {
font-size: 14px; # 14像素、字体大小与字体样式一般写在一起
}
如果设置成inherit表示继承父元素的字体大小值。
字重粗细(font-weight)
字体颜色(color)

.c2 {
color: red; # 直接设置颜色
color: rgb(255,0,0); # 用RGB设置颜色
color: #b5282f;
# 16进制设置 #开始XXXXXX 前两位对应RGB第一个数值,中间两位对应RGB的第二位,最后两位对应RGB的第三位
/*颜色加透明度*/
color: rgba(255,0,0,0.3) # 透明度取值范围0.1-1
}
文字属性
文字对齐(text-align)

文字装饰(text-decoration)
常用的为去掉a标签默认的自划线
a {
text-decoration: none;
}
首行缩进(text-indent)
p {
text-indent: 10px;
}
背景属性
/*背景颜色*/
background-color: red;
/*背景图片*/
background-image: url('1.jpg');
/*
背景重复
repeat(默认):背景图片平铺排满整个网页
repeat-x:背景图片只在水平方向上平铺
repeat-y:背景图片只在垂直方向上平铺
no-repeat:背景图片不平铺
*/
background-repeat: no-repeat;
/*背景位置*/
background-position: right top;
/*background-position: 200px 200px;*/
建议简写
background:#ffffff url('1.png') no-repeat right top;
使用背景图片的一个常见案例就是很多网站会把很多小图标放在一张图片上,然后根据位置去显示图片。减少频繁的图片请求。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>滚动背景图示例</title> <style> * { margin: 0; } .box { width: 100%; height: 500px; background: url("https://www.luffycity.com/static/img/width-bank.1c9d1b0.png") no-repeat center center; background-attachment: fixed; } .d1 { height: 500px; background-color: tomato; } .d2 { height: 500px; background-color: steelblue; } .d3 { height: 500px; background-color: mediumorchid; } </style> </head> <body> <div class="d1"></div> <div class="box"></div> <div class="d2"></div> <div class="d3"></div> </body> </html>
边框
边框属性
- border-width(宽度)
- border-style(样式---->实线、虚线等)
- border-color(颜色)
#c1 {
border-width: 2px;
border-style: solid;
border-color: red;
}
建议简写
#c1 {
border: 2px solid red;
}
边框样式(border-style)

默认为统一设置边框,但是还可以单独为某一个边框设置样式
#c1 {
border-top-style:dotted;
border-top-color: red;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:none;
}
圆角边框(border-radius)---->圆头像
将border-radius设置为长或高的一半即可得到一个圆形。
display属性
visibility:hidden: 可以隐藏某个元素,但隐藏的元素仍需占用与未隐藏之前一样的空间。也就是说,该元素虽然被隐藏了,但仍然会影响布局。
display:none: 可以隐藏某个元素,且隐藏的元素不会占用任何空间。也就是说,该元素不但被隐藏了,而且该元素原本占用的空间也会从页面布局中消失。
CSS盒子模型(重要)
margin外边距
.c1 {
margin-top:5px;
margin-right:10px;
margin-bottom:15px;
margin-left:20px;
}
建议简写
.c1 {
margin: 5px 10px 15px 20px;
}
顺序:上右下左
常见居中:
.c1 {
margin: 0 auto;
}
padding内填充
.c1 {
padding-top: 5px;
padding-right: 10px;
padding-bottom: 15px;
padding-left: 20px;
}
建议简写
.c1 {
padding: 5px 10px 15px 20px;
}
顺序:上右下左
补充padding的常用简写方式:
- 提供一个,用于四边;
- 提供两个,第一个用于上-下,第二个用于左-右;
- 如果提供三个,第一个用于上,第二个用于左-右,第三个用于下;
- 提供四个参数值,将按上-右-下-左的顺序作用于四边;
浮动(float)
在 CSS 中,任何元素都可以浮动。
浮动元素会生成一个块级框,而不论它本身是何种元素。
关于浮动的两个特点:
- 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
- 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。
left:向左浮动 right:向右浮动 none:默认值,不浮动
清除浮动(clear)
清除浮动的副作用(父标签塌陷问题)
主要有三种方式:
- 固定高度
- 伪元素清除法
- overflow:hidden
伪元素清除法(使用较多):
.clearfix:after {
content: "";
display: block;
clear: both;
}
溢出属性(overflow)
圆形头像
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>圆形的头像示例</title>
<style>
* {
margin: 0;
padding: 0;
background-color: #eeeeee;
}
.header-img {
width: 150px;
height: 150px;
border: 3px solid white;
border-radius: 50%;
overflow: hidden;
}
.header-img>img {
max-width: 100%;
}
</style>
</head>
<body>
<div class="header-img">
<img src="https://q1mi.github.io/Blog/asset/img/head_img.jpg" alt="">
</div>
</body>
</html>
定位(position)
默认值,不定位(static)
相对定位(relative)
相对定位是相对于该元素在文档流中的原始位置,即以自己原始位置为参照物。有趣的是,即使设定了元素的相对定位以及偏移值,元素还占有着原来的位置,即占据文档流空间。对象遵循正常文档流,但将依据top,right,bottom,left等属性在正常文档流中偏移位置。而其层叠通过z-index属性定义。
注意:position:relative的一个主要用法:方便绝对定位元素找到参照物。
绝对定位(absolute)
定义:设置为绝对定位的元素框从文档流完全删除,并相对于最近的已定位祖先元素定位,如果元素没有已定位的祖先元素,那么它的位置相对于最初的包含块(即body元素)。元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样。元素定位后生成一个块级框,而不论原来它在正常流中生成何种类型的框。
重点:如果父级设置了position属性,例如position:relative;,那么子元素就会以父级的左上角为原始点进行定位。这样能很好的解决自适应网站的标签偏离问题,即父级为自适应的,那我子元素就设置position:absolute;父元素设置position:relative;,然后Top、Right、Bottom、Left用百分比宽度表示。
另外,对象脱离正常文档流,使用top,right,bottom,left等属性进行绝对定位。而其层叠通过z-index属性定义。
固定定位(fixed)
fixed:对象脱离正常文档流,使用top,right,bottom,left等属性以窗口为参考点进行定位,当出现滚动条时,对象不会随着滚动。而其层叠通过z-index属性 定义。 注意点: 一个元素若设置了 position:absolute | fixed; 则该元素就不能设置float。这 是一个常识性的知识点,因为这是两个不同的流,一个是浮动流,另一个是“定位流”。但是 relative 却可以。因为它原本所占的空间仍然占据文档流。 在理论上,被设置为fixed的元素会被定位于浏览器窗口的一个指定坐标,不论窗口是否滚动,它都会固定在这个位置。
<!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"> <title>返回顶部示例</title> <style> * { margin: 0; } .d1 { height: 1000px; background-color: #eeee; } .scrollTop { background-color: darkgrey; padding: 10px; text-align: center; position: fixed; right: 10px; bottom: 20px; } </style> </head> <body> <div class="d1">111</div> <div class="scrollTop">返回顶部</div> </body> </html>
层叠顺序(z-index)
#i2 {
z-index: 999;
}
设置对象的层叠顺序
- z-index 值表示谁压着谁,数值大的压盖住数值小的,
- 只有定位了的元素,才能有z-index,也就是说,不管相对定位,绝对定位,固定定位,都可以使用z-index,而浮动元素不能使用z-index
- z-index值没有单位,就是一个正整数,默认的z-index值为0如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面压着别人,定位了元素,永远压住没有定位的元素。
- 从父现象:父亲怂了,儿子再牛逼也没用
<!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"> <title>自定义模态框</title> <style> .cover { background-color: rgba(0,0,0,0.65); position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 998; } .modal { background-color: white; position: fixed; width: 600px; height: 400px; left: 50%; top: 50%; margin: -200px 0 0 -300px; z-index: 1000; } </style> </head> <body> <div class="cover"></div> <div class="modal"></div> </body> </html>
透明度(opacity)
用来定义透明效果。取值范围是0~1,0是完全透明,1是完全不透明。
浙公网安备 33010602011771号