样式文件比js更加灵活和零碎,从IE6时代走过来的一些避免兼容性问题的书写思路和方法。css如果管理不好会比js更加难以维护。后期出现的sass、less、postcss、css module等技术是css的超集,类似typescript。都是为了更好的管理和使用css,我们不涉及这些框架,只使用纯css。
一、样式初始化
为什么要做样式初始化?
原因:浏览器会带有默认样式,每个浏览器自带的默认样式都不一样,甚至IE系列浏览器也都不一样。这样极易引起兼容性问题,并且这类兼容性问题非常难处理。
样式初始化可以理解为,把一张有点脏的画布重新洗白了,以便我接下来画画不受干扰。
css样式初始化有很多种。
最简单粗暴的处理方式:使用*通配符。网站大了会有性能问题。
* {padding: 0; margin: 0;}
雅虎 ```css body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td { margin:0; padding:0; } body { background:#fff; color:#555; font-size:14px; font-family: Verdana, Arial, Helvetica, sans-serif; } td,th,caption { font-size:14px; } h1, h2, h3, h4, h5, h6 { font-weight:normal; font-size:100%; } address, caption, cite, code, dfn, em, strong, th, var { font-style:normal; font-weight:normal;} a { color:#555; text-decoration:none; } a:hover { text-decoration:underline; } img { border:none; } ol,ul,li { list-style:none; } input, textarea, select, button { font:14px Verdana,Helvetica,Arial,sans-serif; } table { border-collapse:collapse; } html {overflow-y: scroll;}
.clearfix:after {content: “.”; display: block; height:0; clear:both; visibility: hidden;} .clearfix { *zoom:1; }
3. 腾讯
```css
body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;padding:0}
body{font:12px"宋体","Arial Narrow",HELVETICA;background:#fff;-webkit-text-size-adjust:100%;}
a{color:#2d374b;text-decoration:none}
a:hover{color:#cd0200;text-decoration:underline}
em{font-style:normal}
li{list-style:none}
img{border:0;vertical-align:middle}
table{border-collapse:collapse;border-spacing:0}
p{word-wrap:break-word}
- 新浪 ```css body,ul,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div{margin:0;padding:0;border:0;} body{background:#fff;color:#333;font-size:12px; margin-top:5px;font-family:”SimSun”,”宋体”,”Arial Narrow”;}
ul,ol{list-style-type:none;} select,input,img,select{vertical-align:middle;}
a{text-decoration:none;} a:link{color:#009;} a:visited{color:#800080;} a:hover,a:active,a:focus{color:#c00;text-decoration:underline;}
5. 淘宝
```css
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td { margin:0; padding:0; }
body, button, input, select, textarea { font:12px/1.5tahoma, arial, \5b8b\4f53; }
h1, h2, h3, h4, h5, h6{ font-size:100%; }
address, cite, dfn, em, var { font-style:normal; }
code, kbd, pre, samp { font-family:couriernew, courier, monospace; }
small{ font-size:12px; }
ul, ol { list-style:none; }
a { text-decoration:none; }
a:hover { text-decoration:underline; }
sup { vertical-align:text-top; }
sub{ vertical-align:text-bottom; }
legend { color:#000; }
fieldset, img { border:0; }
button, input, select, textarea { font-size:100%; }
table { border-collapse:collapse; border-spacing:0; }
网易
html {overflow-y:scroll;}
body {margin:0; padding:29px00; font:12px"\5B8B\4F53",sans-serif;background:#ffffff;}
div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,blockquote,p{padding:0; margin:0;}
table,td,tr,th{font-size:12px;}
li{list-style-type:none;}
img{vertical-align:top;border:0;}
ol,ul {list-style:none;}
h1,h2,h3,h4,h5,h6{font-size:12px; font-weight:normal;}
address,cite,code,em,th {font-weight:normal; font-style:normal;}
顺便给出admin10000.com 的html模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
<head>
<title>网站标题 - Admin10000.com </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<meta name="Author" content="网页作者" />
<meta name="Copyright" content="网站版权" />
<meta name="keywords" content="网站关键字" />
<meta name="description" content="网站描述" />
<link rel="Shortcut Icon" href="网站.ico图标路径" />
<link type="text/css" rel="stylesheet" href="CSS文件路径" />
<script type="text/javascript" src="JS文件路径"></script>
</head>
<body>
</body>
</html>
目前推荐使用的css reset是:normalize.css
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
/* Document
========================================================================== */
/**
* 1. Correct the line height in all browsers.
* 2. Prevent adjustments of font size after orientation changes in iOS.
*/
html {
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers.
*/
body {
margin: 0;
}
/**
* Render the `main` element consistently in IE.
*/
main {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
pre {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* Remove the gray background on active links in IE 10.
*/
a {
background-color: transparent;
}
/**
* 1. Remove the bottom border in Chrome 57-
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
*/
b,
strong {
font-weight: bolder;
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
code,
kbd,
samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font size in all browsers.
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/* Embedded content
========================================================================== */
/**
* Remove the border on images inside links in IE 10.
*/
img {
border-style: none;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers.
* 2. Remove the margin in Firefox and Safari.
*/
button,
input,
optgroup,
select,
textarea {
font-family: inherit; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
* Show the overflow in IE.
* 1. Show the overflow in Edge.
*/
button,
input { /* 1 */
overflow: visible;
}
/**
* Remove the inheritance of text transform in Edge, Firefox, and IE.
* 1. Remove the inheritance of text transform in Firefox.
*/
button,
select { /* 1 */
text-transform: none;
}
/**
* Correct the inability to style clickable types in iOS and Safari.
*/
button,
[type="button"],
[type="reset"],
[type="submit"] {
-webkit-appearance: button;
}
/**
* Remove the inner border and padding in Firefox.
*/
button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
* Restore the focus styles unset by the previous rule.
*/
button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
* Correct the padding in Firefox.
*/
fieldset {
padding: 0.35em 0.75em 0.625em;
}
/**
* 1. Correct the text wrapping in Edge and IE.
* 2. Correct the color inheritance from `fieldset` elements in IE.
* 3. Remove the padding so developers are not caught out when they zero out
* `fieldset` elements in all browsers.
*/
legend {
box-sizing: border-box; /* 1 */
color: inherit; /* 2 */
display: table; /* 1 */
max-width: 100%; /* 1 */
padding: 0; /* 3 */
white-space: normal; /* 1 */
}
/**
* Add the correct vertical alignment in Chrome, Firefox, and Opera.
*/
progress {
vertical-align: baseline;
}
/**
* Remove the default vertical scrollbar in IE 10+.
*/
textarea {
overflow: auto;
}
/**
* 1. Add the correct box sizing in IE 10.
* 2. Remove the padding in IE 10.
*/
[type="checkbox"],
[type="radio"] {
box-sizing: border-box; /* 1 */
padding: 0; /* 2 */
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
*/
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
height: auto;
}
/**
* 1. Correct the odd appearance in Chrome and Safari.
* 2. Correct the outline style in Safari.
*/
[type="search"] {
-webkit-appearance: textfield; /* 1 */
outline-offset: -2px; /* 2 */
}
/**
* Remove the inner padding in Chrome and Safari on macOS.
*/
[type="search"]::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
* 1. Correct the inability to style clickable types in iOS and Safari.
* 2. Change font properties to `inherit` in Safari.
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/* Interactive
========================================================================== */
/*
* Add the correct display in Edge, IE 10+, and Firefox.
*/
details {
display: block;
}
/*
* Add the correct display in all browsers.
*/
summary {
display: list-item;
}
/* Misc
========================================================================== */
/**
* Add the correct display in IE 10+.
*/
template {
display: none;
}
/**
* Add the correct display in IE 10.
*/
[hidden] {
display: none;
}
二、基础公用样式
其实样式初始化就是基础公用样式的一种,但是我们在这里略微区分一下。把初始化和公用样式做个区分。
注意:基础公用样式绝对不是说要全部都用到的才有存在价值;它存在的价值是为了让你更方便的写css,更少的写css代码。
例如:下边的其实都超出了样式初始化的范畴。
/* 雅虎 清除浮动 */
.clearfix:after {content: "."; display: block; height:0; clear:both; visibility: hidden;}
.clearfix { *zoom:1; }
/* 新浪 定义a标签默认样式 */
a{text-decoration:none;}
a:link{color:#009;}
a:visited{color:#800080;}
a:hover,a:active,a:focus{color:#c00;text-decoration:underline;}
/* 淘宝 定义h标签的默认字体大小 */
h1, h2, h3, h4, h5, h6{ font-size:100%; }
/* 网易 定义h标签的字体大小 */
h1,h2,h3,h4,h5,h6{font-size:12px; font-weight:normal;}
还有类似如下的基础公用样式
// 字体颜色
.c9 {
color: #999;
}
.c6 {
color: #666;
}
.c-fff,
.c-white {
color: #fff;
}
.c-blue {
color: @mainColor !important;
}
.c-red {
color: @red !important;
}
.c-yellow {
color: @yellow !important;
}
.c-green {
color: @green !important;
}
// 字重
.bold,
.f600 {
font-weight: bold !important;
}
.normal,
.f400 {
font-weight: normal !important;
}
// 字体尺寸
.fz10 {
font-size: 10;
}
.fz12 {
font-size: 12px;
}
.fz14 {
font-size: 14px;
}
.fz16 {
font-size: 16px;
}
.fz18 {
font-size: 18px;
}
.fz20 {
font-size: 20px;
}
.fz22 {
font-size: 22px;
}
.fz24 {
font-size: 24px;
}
.fz26 {
font-size: 26px;
}
.fz28 {
font-size: 28px;
}
.fz30 {
font-size: 30px;
}
.fz32 {
font-size: 32px;
}
// 背景颜色
.bgc-white,
.bgc-fff {
background-color: #fff;
}
.bgc-gery {
background-color: #f2f2f2;
}
.bgc-blue {
background-color: blue;
}
.bgc-yellow {
background-color: @yellow;
}
.bgc-red {
background-color: @red;
}
.bgc-green {
background-color: @green;
}
// 圆角
.bdr2 {
border-radius: 2px;
}
.bdr4 {
border-radius: 4px;
}
.bdr8 {
border-radius: 8px;
}
.bdr50p {
border-radius: 50%;
}
// 布局方式
.flex {
display: flex;
align-items: center;
}
.zhong {
display: flex;
align-items: center;
justify-content: center;
}
.space-between {
display: flex;
justify-content: space-between;
align-items: center;
}
.space-around {
display: flex;
justify-content: space-around;
align-items: center;
}
.flex-warp {
display: flex;
align-items: center;
flex-wrap: wrap;
}
.flex-col {
display: flex;
flex-direction: column;
align-items: flex-start !important;
}
.flex-end {
display: flex;
justify-content: flex-end;
}
.flex-center {
display: flex;
justify-content: center;
}
.db,
.show {
display: block;
}
.dib {
display: inline-block;
}
.hide {
display: none;
}
.fl,
.pull-left {
float: left;
}
.fr,
.pull-right {
float: right;
}
// margin值
.m-5 {
margin: -5px !important;
}
.mt-5 {
margin-top: -5px !important;
}
.mb-5 {
margin-bottom: -5px !important;
}
.ml-5 {
margin-left: -5px !important;
}
.mr-5 {
margin-right: -5px !important;
}
.m0 {
margin: 0px !important;
}
.mt0 {
margin-top: 0px !important;
}
.mb0 {
margin-bottom: 0px !important;
}
.ml0 {
margin-left: 0px !important;
}
.mr0 {
margin-right: 0px !important;
}
.m5 {
margin: 5px !important;
}
.mt5 {
margin-top: 5px !important;
}
.mb5 {
margin-bottom: 5px !important;
}
.ml5 {
margin-left: 5px !important;
}
.mr5 {
margin-right: 5px !important;
}
.m10 {
margin: 10px !important;
}
.mt10 {
margin-top: 10px !important;
}
.mb10 {
margin-bottom: 10px !important;
}
.ml10 {
margin-left: 10px !important;
}
.mr10 {
margin-right: 10px !important;
}
.m15 {
margin: 15px !important;
}
.mt15 {
margin-top: 15px !important;
}
.mb15 {
margin-bottom: 15px !important;
}
.ml15 {
margin-left: 15px !important;
}
.mr15 {
margin-right: 15px !important;
}
.m20 {
margin: 20px !important;
}
.mt20 {
margin-top: 20px !important;
}
.mb20 {
margin-bottom: 20px !important;
}
.ml20 {
margin-left: 20px !important;
}
.mr20 {
margin-right: 20px !important;
}
.m25 {
margin: 25px !important;
}
.mt25 {
margin-top: 25px !important;
}
.mb25 {
margin-bottom: 25px !important;
}
.ml25 {
margin-left: 25px !important;
}
.mr25 {
margin-right: 25px !important;
}
.m30 {
margin: 30px !important;
}
.mt30 {
margin-top: 30px !important;
}
.mb30 {
margin-bottom: 30px !important;
}
.ml30 {
margin-left: 30px !important;
}
.mr30 {
margin-right: 30px !important;
}
.m50 {
margin: 50px !important;
}
.mt50 {
margin-top: 50px !important;
}
.mb50 {
margin-bottom: 50px !important;
}
.ml50 {
margin-left: 50px !important;
}
.mr50 {
margin-right: 50px !important;
}
// padding值
.p0 {
padding: 0 !important;
}
.pt0 {
padding-top: 0 !important;
}
.pr0 {
padding-right: 0 !important;
}
.pb0 {
padding-bottom: 0 !important;
}
.pl0 {
padding-left: 0 !important;
}
.p5 {
padding: 5px;
}
.pt5 {
padding-top: 5px;
}
.pr5 {
padding-right: 5px;
}
.pb5 {
padding-bottom: 5px;
}
.pl5 {
padding-left: 5px;
}
.p10 {
padding: 10px;
}
.pt10 {
padding-top: 10px;
}
.pr10 {
padding-right: 10px;
}
.pb10 {
padding-bottom: 10px;
}
.pl10 {
padding-left: 10px;
}
.p15 {
padding: 15px;
}
.pt15 {
padding-top: 15px;
}
.pr15 {
padding-right: 15px;
}
.pb15 {
padding-bottom: 15px;
}
.pl15 {
padding-left: 15px;
}
.p20 {
padding: 20px;
box-sizing: border-box;
}
.pt20 {
padding-top: 20px;
}
.pr20 {
padding-right: 20px;
}
.pb20 {
padding-bottom: 20px;
}
.pl20 {
padding-left: 20px;
}
.p30 {
padding: 30px;
box-sizing: border-box;
}
.pt30 {
padding-top: 30px;
}
.pr30 {
padding-right: 30px;
}
.pb30 {
padding-bottom: 30px;
}
.pl30 {
padding-left: 30px;
}
.pb50 {
padding-bottom: 50px;
}
.pl50 {
padding-left: 50px;
}
.pl120 {
padding-left: 120px;
}
.pl150 {
padding-left: 150px;
}
.pt50 {
padding-top: 50px;
}
// 文本线格式
.overline {
text-decoration: overline
}
.line-through {
text-decoration: line-through
}
.underline {
text-decoration: underline
}
.blink {
text-decoration: blink
}
// 字体对齐方式
.tac {
text-align: center;
justify-content: center;
}
.tal {
text-align: left !important;
}
.tar {
text-align: right !important;
}
// 宽度
.w10p {
width: 10%;
}
.w20p {
width: 20%;
}
.w25p {
width: 25%;
}
.w30p {
width: 30%;
}
.w40p {
width: 40%;
}
.w50p {
width: 50%;
}
.w60p {
width: 60%;
}
.w70p {
width: 70%;
}
.w75p {
width: 75%;
}
.w80p {
width: 80%;
}
.w90p {
width: 90%;
}
.w100p {
width: 100%;
}
.w20 {
width: 20px;
}
.w25 {
width: 25px;
}
.w50 {
width: 50px;
}
.w75 {
width: 75px;
}
.w100 {
width: 100px;
}
.w125 {
width: 125px;
}
.w150 {
width: 150px;
}
.w175 {
width: 175px;
}
.w200 {
width: 200px;
}
.w210 {
width: 210px;
}
.w220 {
width: 220px;
}
.w250 {
width: 250px;
}
.w275 {
width: 275px;
}
.w300 {
width: 300px;
}
.w325 {
width: 325px;
}
.w350 {
width: 350px;
}
.w375 {
width: 375px;
}
.w400 {
width: 400px;
}
.w450 {
width: 450px;
}
.w500 {
width: 500px;
}
.w600 {
width: 600px;
}
.w750 {
width: 750px;
}
.w1000 {
width: 1000px;
}
// 高度
.h15 {
height: 15px;
}
.h10 {
height: 10px !important;
}
.h20 {
height: 20px !important;
}
.h25 {
height: 25px !important;
}
.h30 {
height: 30px !important;
}
.h40 {
height: 40px !important;
}
.h50 {
height: 50px !important;
}
.h60 {
height: 60px !important;
}
.h70 {
height: 70px !important;
}
.h75 {
height: 75px !important;
}
.h80 {
height: 80px !important;
}
.h90 {
height: 90px !important;
}
.h100 {
height: 100px !important;
}
.h125 {
height: 125px !important;
}
.h150 {
height: 150px !important;
}
.h10p {
height: 10%;
}
.h20p {
height: 20%;
}
.h30p {
height: 30%;
}
.h40p {
height: 40%;
}
.h50p {
height: 50%;
}
.h60p {
height: 60%;
}
.h70p {
height: 70%;
}
.h80p {
height: 80%;
}
.h90p {
height: 90%;
}
.h100p {
height: 100%;
}
.h10vh {
height: 10vh;
}
.h20vh {
height: 20vh;
}
.h30vh {
height: 30vh;
}
.h40vh {
height: 40vh;
}
.h50vh {
height: 50vh;
}
.h60vh {
height: 60vh;
}
.h70vh {
height: 70vh;
}
.h80vh {
height: 80vh;
}
.h90vh {
height: 90vh;
}
.h100vh {
height: 100vh;
}
.h600 {
height: 600px;
}
.h800 {
height: 800px;
}
// 行高
.lh16 {
line-height: 16px;
}
.lh18 {
line-height: 18px;
}
.lh20 {
line-height: 20px;
}
.lh22 {
line-height: 22px;
}
.lh25 {
line-height: 25px;
}
.lh30 {
line-height: 30px;
}
// 定位
.relative {
position: relative;
}
.point {
cursor: pointer !important;
user-select: none;
}
// 文字竖向排列
.column {
margin: 0 auto;
height: 140px;
writing-mode: vertical-lr;
/从左向右 从右向左是 writing-mode: vertical-rl;/
writing-mode: tb-lr;
/IE浏览器的从左向右 从右向左是 writing-mode: tb-rl;/
}
/* 解决文字超出溢出问题 */
.text-cut {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
作者:wangzx
链接:https://juejin.cn/post/6844904130209644552
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
bootstrap3的全局css样式:https://v3.bootcss.com/css/
assenbly-css:https://github.com/zj1024/assembly-css
参考文档:
- css样式初始化代码:https://www.jianshu.com/p/beabef833bfe
- 关于css reset那些事:https://segmentfault.com/a/1190000003021766
- normalize.css:http://necolas.github.io/normalize.css/
- bootstrap中文文档:https://v3.bootcss.com/