CSS文档统筹
我们平常写页面时,都会先去写一个* {margin: 0; padding: 0;},天天写挺累的。
既然每个页面都需要写,为什么不能写一个.CSS文件,把统筹的内容直接写在文件内,在每次使用的时候,直接引入就好。
注意!我们写页面时,一般都是外部样式文件,所以引入顺序如下:
- 先引入公共样式表
reset.css - 再引入
iconfont.css - 最后引入自己写的页面的
.css文件
```css / 去除页面缝隙 / html, body, div, p, h1, h3, h2, h4, h5, h6, input, button, img, ul, li, ol, dl, dt, dd, form, table, thead, tfoot, tbody, fieldset, legend, select, textarea, marquee, iframe, header, nav, main, footer, section, article, aside, hgroup, figure, figcaption, dialog, video, audio { margin: 0; padding: 0; }<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>CSS文档统筹</title><link rel="stylesheet" href="./css/reset.css"></head>
/ 去除页面所有超链接下划线 / a { text-decoration: none; color: #000; }
/ 去除页面所有列表样式 / ul, li, ol { list-style: none; }
/ 取消标签的加粗和倾斜 / h1, h2, h3, h4, h5, h6, b, strong, i, em, var { font-weight: normal; font-style: normal; }
/ 去除input框聚焦线 / input, button { outline: 0; }
/ 解决图片3px问题 / img { vertical-align: top; border: 0; }
/ 页面文字大小常用12px 文字大小有继承性 / body { font-size: 12px;/ 注意 移动端把这行话去掉 / font-family: “微软雅黑”; }
/ 高度塌陷的元素 以后给设置一个class名字叫clearFix即可 / .clearFix::after { content: “”; display: block; clear: both; overflow: hidden; visibility: hidden; height: 0; }
/ 以后 去除边框的元素 直接加一个class名字叫noborder / .noborder { border: 0 !important; }
<a name="sB09z"></a>
# CSS优化
1. 代码优化
- 精简
2. 命名优化
- 结构命名 `.headLeft``.headRight`
- 语义化命名 `.font12``.colorRed``.header``.nav``.footer`
3. 图片优化
- `<img src="" alt="">``alt`即便里面什么都不写,你也加上它,便于搜索引擎爬
4. 超链接优化
- `<a href="">文字</a>`使用**文字**当超链接,**不**要使用**图片**/**视频**/**Flash**去做超链接
5. 页面主题优化
```css
<meta name="description" content="描述 感冒了想要退烧 止咳应该怎么办">
<meta name="keywords" content="关键字 感冒药 退烧 止咳 鼻塞 流鼻涕 头晕">
PR值,友情链接
- 也称为图片整合技术;也叫图片精灵;也叫精灵图;也叫
CSS sprite - 图片整合的意义:
- 核心:是通过更改背景图位置来变化
- 减少
**HTTP**请求次数,加快页面加载速度! - 减少UI小姐姐命名困扰
- 在更改最少的图片位置或路径时,可以获得漂亮的样式
23f3ddf914b1b527d0429a3d713cfe3a.pngnav.pngnav-on.png二维码.png
<!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.0">
<title>Document</title>
<style>
div {
width: 30px;
height: 30px;
/* transition: .3s; */
background: pink url(../images/二维码.png) no-repeat 0 -35px;
}
/* 鼠标滑过 变另一张图 */
div:hover {
/* 变得是什么 是背景位置!!! */
background-position: 0 -86px;
}
section {
width: 30px;
height: 30px;
background: url(../images/二维码.png) -102px -35px;
}
section:hover {
background-position: -102px -86px;
}
header {
height: 78px;
width: 118px;
background: url(../images/nav.png) -370px 0;
}
header:hover {
background-image: url(../images/nav-on.png);
}
</style>
</head>
<body>
<div></div>
<section></section>
<!-- 练习 -->
<header></header>
</body>
</html>
回流与重绘
浏览器内核
Trident代表作:IEGecko代表作:MozillaWebKit代表作:苹果、谷歌旧版本Blink代表作:谷歌、欧朋Presto(Opera前内核 已经废弃)
指针bug
<!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.0">
<title>Document</title>
<style>
div {
width: 400px;
height: 400px;
background: cyan;
/* 指针样式设置 cursor */
/* cursor: pointer;小手 */
/* cursor: move;移动 */
/* cursor: text;文本 */
/* cursor: progress;小箭头+转圈 */
/* cursor: wait;转圈 */
/* cursor: help;箭头+问号 */
cursor: ne-resize;
cursor: s-resize;
}
</style>
</head>
<body>
<div>可以变化莫测</div>
</body>
</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.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
input {
/* 表单对齐不一致 解决方式 浮动 */
float: left;
/* 表单高度不一样 去除他们的边框 如果需要边框 使用p标签设置 */
height: 50px;
border: 0;
background: cyan;
}
div {
width: 30px;
height: 300px;
background: cyan;
/* opacity: 0.5;取值是0~1 */
/* filter: alpha(opacity="50"); 取值范围值0~100 */
}
div:hover {
filter: blur(4px);
}
</style>
</head>
<body>
<p>
<input type="text">
<input type="submit">
</p>
<div>透明度</div>
</body>
</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.0">
<title>Document</title>
<style>
.big{
width: 200px;
height: 400px;
background: rgba(66, 66, 66, 0.3);
}
.small{
width: 100px;
height: 100px;
background: #000;
}
</style>
</head>
<body>
<div class="big">
<div class="small"></div>
</div>
</body>
</html>
元素垂直水平居中
1、位移变换
<!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.0">
<title>Document</title>
<style>
section {
width: 400px;
height: 400px;
background: red;
position: absolute;
left: 50%;
top: 50%;
/* margin-left: 负宽度一半; */
/* margin-top: 负高度一半; */
/* 你还可以写成 transform: translate(-50%,-50%); */
transform: translate(-50%, -50%);/* 基于section自己本身的一半 */
}
</style>
</head>
<body>
<section></section>
</body>
</html>
2、定位
<!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.0">
<title>Document</title>
<style>
section {
width: 300px;
height: 300px;
background: springgreen;
/* 元素垂直水平居中 */
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<section></section>
</body>
</html>
3、弹性盒
<!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.0">
<title>Document</title>
<style>
.big{
width: 600px;
height: 700px;
background: pink;
display: flex;
justify-content: center;
align-items: center;
}
.small{
width: 200px;
height: 200px;
background: yellow;
}
</style>
</head>
<body>
<div class="big">
<div class="small"></div>
</div>
</body>
</html>
4、弹性盒2.0+外边距
<!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.0">
<title>Document</title>
<style>
.big {
width: 600px;
height: 700px;
background: pink;
display: flex;
}
.small {
width: 200px;
height: 200px;
background: yellow;
margin: auto;
}
</style>
</head>
<body>
<div class="big">
<div class="small"></div>
</div>
</body>
</html>
5、弹性盒3.0
<!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.0">
<title>Document</title>
<style>
.big {
width: 600px;
height: 700px;
background: pink;
display: flex;
justify-content: center;
}
.small {
width: 200px;
height: 200px;
background: yellow;
align-self: center;
}
</style>
</head>
<body>
<div class="big">
<div class="small"></div>
</div>
</body>
</html>


