布局解決方案
水平居中布局
<!--方案1冠蒋,兼容性好乾胶,但是代碼冗雜-->
<style>
.parent{
text-align: center;
}
.child{
display: inline-block;
text-align: left;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案2识窿,IE8及以上兼容,但是只設(shè)置了子元素缩宜,易維護-->
<style>
.child{
display: table;
/*table和inline-block一樣锻煌,是的元素的寬度等于內(nèi)容寬度*/
margin: 0 margin;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案3姻蚓,IE9及以上兼容,好處是-->
<style>
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%; /*自身左邊界距離父元素左邊界*/
transform: translateX(-50%); /*再向左移動自身寬度的50%捂龄,css3.0新增屬性*/
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案4跺讯,IE9及以上支持-->
<style>
.parent{
display: flex; /*子元素默認變成了flex-item殉农,即寬度是內(nèi)容寬度*/
justify-content: center;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
垂直居中布局
<!--方案1超凳,IE8及以上-->
<style>
.parent{
display: table-cell;
vertical-align: center;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案2,IE9及以上支持-->
<style>
.parent{
display: relative;
}
.child{
position: abosolute;
top: 50%;
transform: translateY(-50%)
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案3暂雹,IE9及以上支持,好處是只需要設(shè)置parent-->
<style>
.parent{
display: flex;
align-items: center;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
水平垂直都居中的布局
<!--方案1-->
<style>
.parent{
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child{
display: inline-block;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案2杭跪,IE9及以上支持-->
<style>
.parent{
display: relative;
}
.child{
position: abosolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
<!--方案3涧尿,IE9及以上支持,好處是只需要設(shè)置parent-->
<style>
.parent{
display: flex;
align-items: center;
justify-content: center;
}
</style>
<div class="parent">
<div class="child">DEMO</div>
</div>
多列布局
<!--方案1-->
<style>
.left{
float: left;
width: 100px;
}
.right{
margin-left: 120px;
}
</style>
<div>
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
<!--方案2檬贰,兼容性最好-->
<style>
.left{
float: left;
width: 100px;
position: relative; /*由于此種方案右側(cè)的內(nèi)容蓋住了左側(cè)翁涤,為了防止左側(cè)的內(nèi)容無法被選中,需要如此設(shè)置以提高左邊元素的Z軸坐標号阿,提高層級*/
}
.right{
margin-left: 120px;
}
.right-fix{
width: 100%;
float: right;
margin: -100px;
}
</style>
<div>
<div class="left">
<p>left</p>
</div>
<div class="right-fix">
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
</div>
<!--方案3倦西,IE6不支持-->
<style>
.left{
float: left;
width: 100px;
margin: 20px;
}
.right{
overflow: hidden;
}
</style>
<div>
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
<!--方案4赁严,代碼比較多-->
<style>
.parent{
display: table; width: 100%;
table-layout: fixed; /*布局優(yōu)先疼约,提高渲染速度*/
}
.left{
width: 100px;
padding-right: 20px;
}
.left, .right{
display: table-cell;
}
</style>
<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
<!--方案5,css3新增特性劝枣,低版本IE不支持舔腾,而且flex當內(nèi)容太復(fù)雜時性能不太好-->
<style>
.left{
float: left;
width: 100px;
margin-right: 20px;
}
.right{
overflow: hidden;
}
</style>
<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="center">
<p>center</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
多列等分布局
<!--方案1搂擦,兼容性好,但是代碼量也不小扳还,而且魯棒性不好氨距,如果列數(shù)變化,25%這個數(shù)字需要手動改楞遏,有時可能還會出現(xiàn)循環(huán)小數(shù)-->
<style>
.parent{
margin-left: -20px;
}
.column{
float: left;
width: 25%;
padding-left: 20px;
box-sizing: border-box;
}
</style>
<div class="parent">
<div class="column"><p>1</p></div>
<div class="column"><p>2</p></div>
<div class="column"><p>3</p></div>
<div class="column"><p>4</p></div>
</div>
<!--這個布局的精妙之處在于padding和margin重疊且寬度相等橱健,25%剛好是4列等分的的結(jié)果沙廉,而后又將css的box-sizing進行了修改,保證了在4列等分的情況下珊皿,間距也會等分巨税,且最后一個元素不會超出瀏覽器視窗-->
<!--方案2-->
<style>
.parent{
display: table;
width: 100%;
table-layout: fixed;/*設(shè)置這個性質(zhì)時草添,table-cell如果不設(shè)置寬度,則默認等分*/
}
.parent-fixed{
margin-left: -20px;
}
.column{
display: table-cell;
padding-left: 20px;
}
</style>
<div class="parent-fix">
<div class="parent">
<div class="column"><p>1</p></div>
<div class="column"><p>2</p></div>
<div class="column"><p>3</p></div>
<div class="column"><p>4</p></div>
</div>
</div>
<!--方案3抄淑,優(yōu)點是簡便肆资,代碼量少灶芝,但是低版本IE不支持-->
<style>
.parent{
display: flex;
}
.column{
flex: 1;
}
.column+.column{
margin-left: 20px;
}
</style>
<div class="parent-fix">
<div class="parent">
<div class="column"><p>1</p></div>
<div class="column"><p>2</p></div>
<div class="column"><p>3</p></div>
<div class="column"><p>4</p></div>
</div>
</div>
多列等高布局
在多列布局的多種實現(xiàn)方式中夜涕,利用flex和table兩種方式都是默認登高的。
全屏布局
特點:
- 整個頁面始終撐滿瀏覽器的窗口
- 滾動條只出現(xiàn)在內(nèi)容區(qū)
- 瀏覽器大小變化時酸役,只有局部內(nèi)容的大小會隨之變化
Paste_Image.png
<!--方案1簇捍,IE6不支持-->
<style>
html, body, .parent{height: 100%; overflow: hidden;}
.top{position: absolute; top: 0; left: 0; right: 0; height: 100px;}
.left{position: absolute; top: 100px; left: 0; width: 200px; bottom: 50px;}
.right{position: absolute; top: 100px; right: 0; bottom: 50px; overflow: auto;}
.bottom{position: absolute; left: 0; right: 0; bottom: 0; height: 50px; }
</style>
<div>
<div class="top">top</div>
<div class="left">left</div>
<div class="right">
<div class="inner">
right
</div>
</div>
<div class="bottom">bottom</div>
</div>
<!--方案2暑塑,IE9以及以下都不兼容-->
<style>
html, body, .parent{height: 100%; overflow: hidden;}
.parent{display: flex; flex-direction: column;}
.top{position: absolute; top: 0; left: 0; right: 0; height: 100px;}
.left{width: 200px; }
.right{overflow: auto;}
.middle{ position: absolute; flex:1; display: flex;}
.bottom{position: absolute; left: 0; right: 0; bottom: 0; height: 50px; }
</style>
<div>
<div class="top">top</div>
<div class="middle">
<div class="left">left</div>
<div class="right">
<div class="inner">
right
</div>
</div>
</div>
<div class="bottom">bottom</div>
</div>
CSS Reset
css的諸多標簽都有自己默認的屬性事格,如h1標簽的字號搞隐,em標簽的斜體等屬性,各個瀏覽器中對不同的標簽都是默認的樣式逢捺,且不同的瀏覽器可能會有不同的設(shè)置
為了不使用瀏覽器的默認樣式癞季,我們可以引用reset.css這個文件來對所有標簽的屬性進行重置绷柒,這樣可以保證同一種標簽的所有樣式在不同的瀏覽器中的表現(xiàn)都是一樣的。這樣一來伺绽,所有的標簽都失去了樣式嗜湃。
另购披,在現(xiàn)實的開發(fā)中,reset.css中不僅要清除所有的默認樣式程梦,還要定義全局的樣式橘荠,保證所有的頁面有著同樣的行為哥童、風(fēng)格等。
響應(yīng)式
目前匀泊,網(wǎng)頁需要在不同的設(shè)備上瀏覽,如PC揣非,平板躲因,手機等大脉,這些設(shè)備的屏幕大小不同,使用方式不同琐驴,所以在設(shè)計網(wǎng)頁時要注意對于不同設(shè)備的兼容和顯示秤标。
viewport
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!--這條語句設(shè)置了用戶設(shè)備的視窗抛杨,保證用戶看到的內(nèi)容不會隨著設(shè)備屏幕大小的變化而變化,scale就是1.0茁帽,顯示的寬度就是等于設(shè)備寬度屈嗤,并不允許用戶自己縮放-->
css設(shè)置
@media screen and (max-width: 1000px) and (min-width: 320px){
.......
}
/*這個是css的選擇器饶号,表示大括號其中的css內(nèi)容只有在媒體設(shè)備的屏幕寬度>=320px且<=1000px時才會被使用茫船,可以用來做內(nèi)容的*/
/*這樣,不同的設(shè)備上可以使用不同的布局*/
頁面優(yōu)化
- 提高頁面打開速度
- 對搜索引擎涩禀,閱讀器友好
- 代碼優(yōu)化可以提到代碼可讀性然眼,容易維護
如何優(yōu)化
- 減少請求
- 圖片合并,如將小圖標進行合并到一個文件屿岂,減少文件請求數(shù)量
- 見多個css文件進行合并爷怀,將css內(nèi)聯(lián)引入html頁面
- 避免使用@import方式引入css文件
- 減少文件大小
- 通過選擇合適的圖片格式來減小文件大小
- 對圖片進行無損壓縮
- css值縮寫霉撵,如將margin-left/right/top/bottom等四個屬性縮寫為一個margin
- 顏色值最短表示
- css中選擇器合并
- 頁面性能
- 將引入css文件的link標簽寫到<head>標簽中
- js腳本放在頁面底部洪囤,因為js的運行會阻塞請求
- 消耗性能的屬性:expression瘤缩、filter、border-radius锦溪、box-shadow府怯、gradients
- 可讀性牺丙、可維護性
- 代碼規(guī)范
- 代碼語義化
- 盡量避免使用hack