前端三大件里面, CSS是博主最喜欢的话题了,在此分享一下那些常考的CSS面试知识点,正文以下:javascript
When a browser displays a document, it must combine the document's content with its style information. It processes the document in two stages:
css
There are 4 ways to associate styles with your HTML document. Most commonly used methods are inline CSS and External CSShtml
<style>
element<link>
elements<link href=" " rel="stylesheet" />
?The "rel" in <link> is standing for the relationship between the HTML document and the externally linked file. "rel" has values like stylesheet, index, section, help, bookmark, next, prev, copyright, etc..前端
<link>
VS @import
由于浏览器的兼容问题,不一样浏览器对有些标签的默认值是不一样的,若是没对CSS初始化每每会出现浏览器之间的页面显示差别。固然,初始化样式会对SEO有必定的影响,但鱼和熊掌不可兼得,但力求影响最小的状况下初始化。java
Resetting removing all the native provided by browsers. (Reset重置浏览器的css默认属性 浏览器的品种不一样,样式不一样,而后重置,让他们统一)web
Normalizing is just a correction of some common bugs.面试
Selectors can be defined in various ways as following:浏览器
全部Pseudo-class&Pseudo-element以下:bash
Pseudo-element不占用dom元素节点,:before和:after经常使用于清除浮动, 具体作法就是给浮动元素的父元素定义伪类:
服务器
<div class="father"> <div class="f"> 我是浮动的 <div> <div> <style> .father:after{ clear:both; content:''; height:0; visibility:hidden; display:block; } </style>复制代码
:active |
the moment it is clicked |
: visited |
the user has already visited |
inline elements do not break the flow. margin/ padding will push other elements horizontally not vertically. Moreover, inline elements ignore the height and width.
block elements break the flow and don't sit inline. they are usually container like div, section and also text p, h1. its height and width can be adjusted.
inline-block will be similar to inline and will go with the flow of the page. Only differences are this will take height and width.
可继承的样式: font-size font-family, color, visibility.
不可继承的样式:border padding margin width height display
有两种BOX model:
标准模型和IE模型区别:
W3c Box model: ele'width = content width;
IE box model: ele'width=content width +padding width+ border width;
CSS如何设置这两种模型?
box-sizing: content-box; //默认
box-sizing: border-box;
Js如何设置获取盒模型对应的宽和高?
dom节点.style.width/height (only get the "embedd style" width/height)
dom节点.currentStyle.width/height
dom节点.getComputedStyle(dom).width/height
dom节点.getBoundingClientRect().width/height
区别以下:
Block Formatting Context: 是一个独立的渲染区域,让处于 BFC 内部的元素与外部的元素相互隔离,使内外元素的定位不会相互影响。
1. 解决边距重叠问题
当元素都设置了margin边距时,margin将取最大值。 为了避免让边距重叠,能够给子元素加一个父元素,并设置该元素为BFC:
<div> <p> xxxxxxxxx </p> <div style="overflow:hidden;"> <p> yyyyyyyyy </p> </div> </div>复制代码
2. 解决面积重合问题 (利用BFC不会和float重叠的特性)
<section id="layout"> <div class='left'> </div> <div class='right'> <div> </section> <style> #layout{background-color:steelblue;} #layout .left{float:left;width:100px; height:100px; background-color:tomato;} #layout .right{height:120px; background-color:yellow;overflow:hidden} </style>复制代码
3. 解决清除浮动(清除浮动的原理)
<section id='float-test'> <div class='float'> I am a float </div> </section> <style> #float-test{background-color:steelblue;overflow:blue} .float{float:left,font-size:30px} </style>复制代码
its behavior like the margin of blocks is combined into a single margin whose size is the largest of the individual margins. And floating and absolutely positioned elements never collapse.
下面绿色部分表明margin
if there is no border, padding, inline content, height to separate a block's margin-top from its margin-bottom, then its top and bottom margins collapse.
两个竖直方向上相邻的外边距都是正数,折叠结果是他们二者之间较大的值
两个竖直方向上相邻的外边距都是负数,折叠结果是他们二者之间绝对值较大的值
两个竖直方向上相邻的外边距一正一负,折叠结果是他们二者相加的和
1. Add new Div below the "float element". And the div with class with "clear: both" ( 给已经浮动的元素的后面添加一个带class="clear"且空的div, classe类是这样写的:.class{clear:both; height:0; line-height:0; font-size:0;} )
<div class="outer"> <div class='div1'>1</div> <div class='div2'>2</div> <div class='div3'>3</div> <div class='clear'></div> <div> <style> .outer{border:1px solid #ccc; background-color:#fc9; color:#fff;} .div1{width:80px;height:80px;background-color:red;float:left;} .div2{width:80px;height:80px;background-color:blue;float:left;} .div3{width:80px;height:80px;background-color:sienna;float:left;} .clear{clear:both;height:0;line-height:0;font-size:0;} </style>复制代码
效果图以下:
2. Define a class with the "overflow : auto", then add this class to the "floated" element's parent element. (建立父级BFC: 给已浮动的元素的父级添加over-flow类, 其中over-flow是这样写的 .over-flow{overflow:auto; zoom:1})
<div class="outer over-flow"> <div class='div1'>1</div> <div class='div2'>2</div> <div class='div3'>3</div> <div> <style> .outer{border:1px solid #ccc; background-color:#fc9; color:#fff;} .div1{width:80px;height:80px;background-color:red;float:left;} .div2{width:80px;height:80px;background-color:blue;float:left;} .div3{width:80px;height:80px;background-color:sienna;float:left;} .over-flow{overflow:auto; zoom:1} </style>复制代码
使用overflow属性来清除浮动有一点须要注意,overflow属性共有三个属性值:hidden, auto, visible. 咱们可使用hidden和auto来清除浮动,但最好不要用visible值。
3. Add the pseudo element to the "floated" element's parent element;
<div class="outer clearFix"> <div class='div1'>1</div> <div class='div2'>2</div> <div class='div3'>3</div> <div> <style> .outer{border:1px solid #ccc; background-color:#fc9; color:#fff;} .div1{width:80px;height:80px;background-color:red;float:left;} .div2{width:80px;height:80px;background-color:blue;float:left;} .div3{width:80px;height:80px;background-color:sienna;float:left;} .clearFix:after{ content:'', display:block; height:0; clear:both; visibility:hidden; } .clearFix{zoom:1} </style>复制代码
A CSS hack applies CSS in one or more specific browser versions while that same CSS will be ignored by other browsers.
* html .sidebar{ margin-left: 5px;}复制代码
this is "star html hack" for "only-IE6 hack". ie6能识别*html .class{}
CSS hack有三种表现形式,css属性前缀法、选择器前缀法以及ie条件注释法(即头部引用if ie)。实际项目中css hack大部分是针对ie浏览器不一样版本之间的表现差别而引入的。
1.属性前缀法:
例:ie6能识别下划线“_”和星号“*”,ie7能识别星号“*”(以上版本并不支持),但不能识别下划线“_”,ie6~ie10都认识“\9”,可是其余浏览器不能支持
2.选择器前缀法(选择器hack)
例:ie6能识别*html .class{},ie7能识别*+html .class{} 或者*:first-child+html .class{};
3.ie条件注释法:
针对全部ie(ie10+已经再也不支持条件注释):<!--[if IE]>ie浏览器显示的内容<![endif]-->,
针对ie6及如下版本:<!--[if lte IE 6]>只在ie6及如下显示的内容<![endif]-->.
这类hack不只针对css生效,对写在判断语句里面的全部代码都会生效。
css hack书写顺序:
通常将适用范围广,能识别能力强的css定义在前面。由于写在后面代码若是被识别会覆盖前面识别的。
1. display isn't an inherited attribute. visibility is an inherited attribute.
(后代元素的visibility属性若存在则不会继承,若不存在则继承父元素visibility的值,意味着:父元素的visibility为hidden可是子元素的visibility为visible则子元素依旧可见,子元素visibility不存在则子元素不可见。而元素的display属性设为none其后整棵子树都不可见)
2. display: none removes the element from the normal layout flow and allow other elements to fill in. visibility: hidden tag is rendered, it takes space in the normal flow but doesn't show it.
3. display: none causes DOM reflow, but visibility: hidden doesn't.
ele: nth-child(n) 选择器有两个必要条件: 1. 必须是ele元素。 2. 必须是处于第n个子元素的位置;不然,选不到元素的
ele: nth-of-type(n)选择器只有一个条件: 第n个ele元素
em: |
is relative to the font size of its direct or nearest parent |
rem: |
is only relative to the HTML (root) font-size |
Static position:
Relative Position:
Absolute position:
Fixed position:
"Open Graph" is a technology invented by Facebook that allows the developer to control what content shows up when a page is shared on Facebook.
we can use Open Graph via Meta Tags.
The types of tags used should represent the content of the page. like:
<meta property='og:title' content='about our ompany' /> <meta property='og:image' content='http://.....'/>复制代码
1. use media queries inside <style>
tag
@media screen and (max-width:600px){ .class{background:#ccc} }复制代码
2. use media queries inside <link> tag to include different css file:
<link rel='stylesheet' type='text/css' href='style.css' media='screen and (min-width:400px)'>复制代码
it depends on what you are trying to do.
px is the absolute length unit. px is not related to the size of the screen or its resolution.
em maintains relative size. we can have responsive fonts. 1em is equal to the current font-size of the element or the browser default. if u sent font-size to 16px then 1em = 16px. em is cascade
% sets font-size relative to the font size of the body. Hence, we have to set font-size of the body to a reasonable size. this is easy to use and does cascade. for example, if parent font-size is 20px and child font-size is 50%. child would be 10px.
pt(points) are traditionally used in print. 1pt = 1/72 inch and it is fixed-size unit.
CSS sprites are merging multiple images into a single image.(Css 精灵 把一堆小的图片整合到一张大的图片上,减轻服务器对图片的请求数量)
we can use "background-position" property to locate any parts in a sprites image.
<div> <div class='overlay'> </div> <div class='modal'> This is a modal.. </div> </div> <style> .overlay{ background:#ccc; position:fixed; top:0; left:0; opacity:0.7; height:100%; width:100%; z-index:9; } .modal{ background-color:bisque; position:fixed; top:50%; left:50%; hieght:35%; width:60%; transform:translate(-50%,-50%); z-index:10; } </style>复制代码
What's Sass?
cores feature of Saas?