前端三大件裏面, 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?