权值等级的定义
第一等:代表内联样式,如: style=””,权值为1000。
第二等:代表ID选择器,如:#content,权值为100。
第三等:代表类,伪类和属性选择器,如.content,权值为10。
第四等:代表类型选择器和伪元素选择器,如div p,权值为1。
Ps:通用选择器(*),子选择器(>)和相邻同胞选择器(+)并不在这四个等级中,所以他们的权值都为0,!important 优先级最高,万不得已的情况下才用。
权重计算
#content div#main-content h2=2*100+2*1=202 #content #main-content>h2=2*100+1=201 body #content div[id="main-content"] h2=1*100+1*10+3*1=113 #main-content div.paragraph h2=1*100+1*10+2*1=112 #main-content [class="paragraph"] h2=1*100+1*10+1*1=111 div#main-content div.paragraph h2.first=1*100+2*10+3*1=123
优先规则
权值大的样式会覆盖权值小的样式,上面例子的样式会采用权值最大201的样式,当你乱用!important,特别是后期修改样式的时候,是不是有种心力憔悴电费感觉?
当css前后样式项的权值一样,后面的样式会覆盖前面的样式。
Css 概念
命名风格规范
1 css文件命名:统一为小写的英文字母(尽量少用拼音),如:index.css。
当名字需要组合拼写时,可以在单词间加中杠线(不要用下划线:容易写错)。如:member-report.css。(推荐)
或者统一为驼峰式拼写。如:MemberReport.css (项目启动前统一风格)。
2 样式名(html的class名):在让人看懂的前提下,尽量语义化或简写。尽量少用拼音,和无语义的缩写 .bt .bd 等; 风格可以统一为小写字母,如:content。当名字需要组合时,可以采用css文件名的规范。
样式的继承
文本样式
color,font-family, font-size, font-style, font-variant, font-weight, font, letter-spacing, line-height,text-align, text-indent, texttransform,word-spacing
列表相关属性
list-style-image, list-style-position, list-style-type, list-style,
像素化 css
渲染
就是浏览器把HTML代码以css定义的规则显示在浏览器窗口的过程
浏览器对页面呈现的处理流程
回流
当页面的布局发生变化时,浏览器会回过头来重新渲染,这就是页面为什么会慢的一个原因,当一个点的变 化影响了布局,这就会使得要倒回去重新渲染,这个倒回去的过程称为 reflow(回流)。
当页面布局和几何属性改变时就需要回流。下述情况会发生浏览器回流
var s = document.body.style; s.padding = "2px"; // 回流+重绘 s.border = "1px solid red"; // 再一次 回流+重绘 s.color = "blue"; // 再一次重绘 s.backgroundColor = "#ccc"; // 再一次 重绘 s.fontSize = "14px"; // 再一次 回流+重绘 // 添加node,再一次 回流+重绘 document.body.appendChild(document.createTextNode('abc!')); 回流比重绘的代价要更高,回流的花销跟render tree有多少节点需要重新构建有关系,假设你直接操作body,比如在body最前面插入1个元素,会导致整个render tree回流,这样代价当然会比较高,但如果是指body后面插入1个元素,则不会影响前面元素的回流。
reflow问题也是可以优化的,减少reflow是很有必要的,比如给图片设定好宽度和高度,这样就可以把图片的占位面积预定好。
重绘
一些元素需要更新属性,而这些属性只是影响元素的外观,风格(景颜色,文字颜色,边框颜色)而不会影响布局,浏览器就会repaint。repaint的速度明显比reflow的速度快。
为了帮助小伙伴们更好的学习WEB前端,技术学派整理了WEB前端的相关学习视频及学习路线图。
关注“技术学派”后,评论转发文章,私信回复:WEB前端
CSS样式表是一个序列通用字符集,传输和存储过程中,这些字符必须由支持 US-ASCII(例如 UTF-8, ISO 8859-x, SHIFT JIS 等)字符编码方式编译
When a style sheet is embedded in another document, such as in the STYLE element or "style" attribute of HTML, the style sheet shares the character encoding of the whole document.
当样式出现在其它文档,如 HTML 的 STYLE 标签或标签属性 "style",样式的编码由文档的决定。
When a style sheet resides in a separate file, user agents must observe the following priorities when determining a style sheet's character encoding (from highest priority to lowest):
An HTTP "charset" parameter in a "Content-Type" field (or similar parameters in other protocols)BOM and/or [@charset ]()or other metadata from the linking mechanism (if any)charset of referring style sheet or document (if any)Assume UTF-8
文档外链样式表的编码可以由以下各项按照由高到低的优先级顺序决定:
Authors using an [@charset ]() rule must place the rule at the very beginning of the style sheet, preceded by no characters. (If a byte order mark is appropriate for the encoding used, it may precede the [@charset ]() rule.)
[@charset ]() must be written literally, i.e., the 10 characters '[@charset ]() "' (lowercase, no backslash escapes), followed by the encoding name, followed by '";'.
推荐:
@charset "UTF-8";
.jdc{}
不推荐:
/**
* @desc File Info
* @author Author Name
* @date 2015-10-10
*/
/* @charset规则不在文件首行首个字符开始 */
@charset "UTF-8";
.jdc{}
@CHARSET "UTF-8";
/* @charset规则没有用小写 */
.jdc{}
/* 无@charset规则 */
.jdc{}
更多关于样式编码:CSS style sheet representation
样式书写一般有两种:一种是紧凑格式 (Compact)
.jdc{ display: block;width: 50px;}
一种是展开格式(Expanded)
.jdc{
display: block;
width: 50px;
}
团队约定
统一使用展开格式书写样式
样式选择器,属性名,属性值关键字全部使用小写字母书写,属性字符串允许使用大小写。
/* 推荐 */
.jdc{
display:block;
}
/* 不推荐 */
.JDC{
DISPLAY:BLOCK;
}
/* 推荐 */
.jdc {}
.jdc li {}
.jdc li p{}
/* 不推荐 */
*{}
#jdc {}
.jdc div{}
统一使用四个空格进行代码缩进,使得各编辑器表现一致(各编辑器有相关配置)
.jdc {
width: 100%;
height: 100%;
}
每个属性声明末尾都要加分号;
.jdc {
width: 100%;
height: 100%;
}
左括号与类名之间一个空格,冒号与属性值之间一个空格
推荐:
.jdc {
width: 100%;
}
不推荐:
.jdc{
width:100%;
}
逗号分隔的取值,逗号之后一个空格
推荐:
.jdc {
box-shadow: 1px 1px 1px #333, 2px 2px 2px #ccc;
}
不推荐:
.jdc {
box-shadow: 1px 1px 1px #333,2px 2px 2px #ccc;
}
为单个css选择器或新申明开启新行
推荐:
.jdc,
.jdc_logo,
.jdc_hd {
color: #ff0;
}
.nav{
color: #fff;
}
不推荐:
.jdc,jdc_logo,.jdc_hd {
color: #ff0;
}.nav{
color: #fff;
}
颜色值 rgb() rgba() hsl() hsla() rect() 中不需有空格,且取值不要带有不必要的 0
推荐:
.jdc {
color: rgba(255,255,255,.5);
}
不推荐:
.jdc {
color: rgba( 255, 255, 255, 0.5 );
}
属性值十六进制数值能用简写的尽量用简写
推荐:
.jdc {
color: #fff;
}
不推荐:
.jdc {
color: #ffffff;
}
不要为 0 指明单位
推荐:
.jdc {
margin: 0 10px;
}
不推荐:
.jdc {
margin: 0px 10px;
}
css属性值需要用到引号时,统一使用单引号
/* 推荐 */
.jdc {
font-family: 'Hiragino Sans GB';
}
/* 不推荐 */
.jdc {
font-family: "Hiragino Sans GB";
}
建议遵循以下顺序:
.jdc {
display: block;
position: relative;
float: left;
width: 100px;
height: 100px;
margin: 0 10px;
padding: 20px 0;
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
color: #333;
background: rgba(0,0,0,.5);
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
mozilla官方属性顺序推荐
CSS3 浏览器私有前缀在前,标准前缀在后
.jdc {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
更多关于浏览器私有前辍写法:#Vendor-specific extensions
Google Code Guide
者:sunshine小小倩
转发链接:https://juejin.im/post/592d4a5b0ce463006b43b6da
*请认真填写需求信息,我们会在24小时内与您取得联系。