努力學習與總結是對本身能力的提高,也但願能幫助到同窗們。javascript
塊狀格式化上下文(block formatting context)簡稱 BFC
:是頁面上的一個隔離的獨立容器,容器裏面的子元素不會影響到外面的元素。css
如何觸發BFC?html
html
): 最大的BFCposition
設置爲 fixed
或者 absolute
inline-block
、table-block
、 table-caption
overflow
的值不爲 visible
float
的值不爲 none
BFC的定位方案前端
Css選擇器優先級java
margin
屬性爲給定元素設置全部四個(上下左右)方向的外邊距屬性。寬度
計算的block
元素上,(取最大值)
BFC
(如overflow:hidden;如position:absolute等)border
/padding
auto
時。瀏覽器會自動選擇一個合適的margin來應用(自動分配剩餘空間)
text-align: center;
// 設置 左右 margin 爲 auto margin: 0 auto;
line-height
// vertical-align 只對行內元素、表格單元格元素生效 // 指定 行內元素/表格單元元素 基線相對於 塊狀元素基線的位置 .center-table { display: table; } .center-table p { display: table-cell; vertical-align: middle; }
經過 絕對定位 + 負margingit
#main{ position: relative; // ... 略 } #center{ position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin: -50px; }
經過 絕對定位 + margin: auto;
github
#main{ position: relative; // ... 略 } #center{ width: 100px; height: 100px; left: 0; right: 0; top: 0; bottom: 0; margin: auto; position: absolute; }
transform 居中web
#main{ position: relative; // ... 略 } #center{ position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
flex 居中(一)瀏覽器
#main{ display: flex; justify-content: center; align-items: center; }
flex 居中(二)ide
#main{ display: flex; // ... 略 } #center{ margin: auto; }
css選擇器
的解析是從右向左解析
將高版本瀏覽器支持的特性寫在後面。利用瀏覽器的2個CSS解析特性:
div { background: red; background: linear-gradient(90deg, red, yellow) }
.css
的 @supports
來判斷屬性支持狀況// 支持特定屬性的處理 @supports (position:sticky) { div { position:sticky; } } // 不支持特定屬性:用not處理 @supports not (background: linear-gradient(90deg, red, yellow)) { div { background: red; } } // 若是是多個css屬性:用and處理 @supports (display:-webkit-box) and (-webkit-line-clamp:2) and (-webkit-box-orient:vertical) { p { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } }
做爲 @supports 的另外一種形式出現的,咱們可使用 javascript 的方式來得到 CSS 屬性的支持狀況。
CSS.supports('display', 'flex');
必須2個參數, 不然返回false,(目前不支持IE瀏覽器)
modernizr
若是瀏覽器不支持@supports
,能夠經過調用ele.style來判斷屬性支持狀況(庫:Modernizr)
var root = document.documentElement; //HTML for(var key in root.style) { console.log(key); } // 將會打印 // alignContent // alignItems // alignSelf // alignmentBaseline // all // animation // ...
元素可能有 background 屬性,可是不支持具體的 linear-gradinet() 屬性值。這個時候該如何檢測呢?只須要將具體的值賦值給某一元素,再查詢這個屬性值可否被讀取。
var root = document.documentElement; root.style.backgroundImage = 'linear-gradient(90deg, #888, #ccc)'; if(root.style.backgroundImage) { // 支持 } else { // 不支持 }
經過頁面隱藏的元素進行測試。
/** * 用於簡單的 CSS 特性檢測 * @param [String] property 須要檢測的 CSS 屬性名 * @param [String] value 樣式的具體屬性值 * @return [Boolean] 是否經過檢查 */ function cssTest(property, value) { // CSS && CSS.supports // CSS.supports('display', 'flex'); 必須2個參數, 不然返回false // 用於測試的元素,隱藏在頁面上 var ele = document.getElementById('test-display-none'); // 只有一個參數的狀況 if(arguments.length === 1) { if(property in ele.style) { return true; } // 兩個參數的狀況 }else if(arguments.length === 2){ ele.style[property] = value; if(ele.style[property]) { return true; } } return false; }
position: absolute;
static
,那麼相對於視口定位position:fixed
非none
的transform
屬性,那麼相對於該先輩元素定位
浮動元素脫離了文檔流,不能撐開元素。須要清除浮動。
清除浮動的方法
clear: both;
// 全瀏覽器通用的clearfix方案 // 引入了zoom以支持IE6/7 .clearfix:after { display: table; content: " "; clear: both; } .clearfix{ *zoom: 1; } // 全瀏覽器通用的clearfix方案【推薦】 // 引入了zoom以支持IE6/7 // 同時加入:before以解決現代瀏覽器上邊距摺疊的問題 .clearfix:before, .clearfix:after { display: table; content: " "; } .clearfix:after { clear: both; } .clearfix{ *zoom: 1; }
overflow: hidden;
clear: both;
(和僞元素實現原理同樣,不過 low 不少)px
:(pixel 像素的縮寫),相對於顯示器屏幕分辨率em
:相對於父元素的 font-size
rem
:可想成 root-em
,相對於 root(html)的 font-size
vw
:相對視口(viewport)的寬度而定的,長度等於視口寬度的 1/100僞類:僞類選擇元素基於的是當前元素處於的狀態,或者說元素當前所具備的特性,功能和class有些相似,但它是基於文檔以外的抽象,因此叫僞類(:first-child :link :visitive :hover :focus :lang)
僞元素:僞元素控制的內容實際上和元素是相同的,可是它自己只是基於元素的抽象,不存在於文檔中,因此叫僞元素(:first-line :first-letter :befoe :after)