頁面架構

布局解決方案

水平居中布局

<!--方案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及以上兼容,但是只設置了子元素,易維護-->
<style>
  .child{
    display: table;
    /*table和inline-block一樣,是的元素的寬度等于內容寬度*/
    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,即寬度是內容寬度*/
    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及以上支持,好處是只需要設置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及以上支持,好處是只需要設置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; /*由于此種方案右側的內容蓋住了左側,為了防止左側的內容無法被選中,需要如此設置以提高左邊元素的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當內容太復雜時性能不太好-->
<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,兼容性好,但是代碼量也不小,而且魯棒性不好,如果列數變化,25%這個數字需要手動改,有時可能還會出現循環(huán)小數-->
<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列等分的的結果,而后又將css的box-sizing進行了修改,保證了在4列等分的情況下,間距也會等分,且最后一個元素不會超出瀏覽器視窗-->
<!--方案2-->
<style>
  .parent{
    display: table;
    width: 100%;
    table-layout: fixed;/*設置這個性質時,table-cell如果不設置寬度,則默認等分*/
}
  .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>

多列等高布局

在多列布局的多種實現方式中,利用flex和table兩種方式都是默認登高的。

全屏布局

特點:

  • 整個頁面始終撐滿瀏覽器的窗口
  • 滾動條只出現在內容區(qū)
  • 瀏覽器大小變化時,只有局部內容的大小會隨之變化
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標簽的斜體等屬性,各個瀏覽器中對不同的標簽都是默認的樣式,且不同的瀏覽器可能會有不同的設置
為了不使用瀏覽器的默認樣式,我們可以引用reset.css這個文件來對所有標簽的屬性進行重置,這樣可以保證同一種標簽的所有樣式在不同的瀏覽器中的表現都是一樣的。這樣一來,所有的標簽都失去了樣式。
另,在現實的開發(fā)中,reset.css中不僅要清除所有的默認樣式,還要定義全局的樣式,保證所有的頁面有著同樣的行為、風格等。

響應式

目前,網頁需要在不同的設備上瀏覽,如PC,平板,手機等,這些設備的屏幕大小不同,使用方式不同,所以在設計網頁時要注意對于不同設備的兼容和顯示。

viewport

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!--這條語句設置了用戶設備的視窗,保證用戶看到的內容不會隨著設備屏幕大小的變化而變化,scale就是1.0,顯示的寬度就是等于設備寬度,并不允許用戶自己縮放-->

css設置

@media screen and (max-width: 1000px) and (min-width: 320px){
    .......
}
/*這個是css的選擇器,表示大括號其中的css內容只有在媒體設備的屏幕寬度>=320px且<=1000px時才會被使用,可以用來做內容的*/
/*這樣,不同的設備上可以使用不同的布局*/

頁面優(yōu)化

  1. 提高頁面打開速度
  2. 對搜索引擎,閱讀器友好
  3. 代碼優(yōu)化可以提到代碼可讀性,容易維護

如何優(yōu)化

  1. 減少請求
  • 圖片合并,如將小圖標進行合并到一個文件,減少文件請求數量
  • 見多個css文件進行合并,將css內聯引入html頁面
  • 避免使用@import方式引入css文件
  1. 減少文件大小
  • 通過選擇合適的圖片格式來減小文件大小
  • 對圖片進行無損壓縮
  • css值縮寫,如將margin-left/right/top/bottom等四個屬性縮寫為一個margin
  • 顏色值最短表示
  • css中選擇器合并
  1. 頁面性能
  • 將引入css文件的link標簽寫到<head>標簽中
  • js腳本放在頁面底部,因為js的運行會阻塞請求
  • 消耗性能的屬性:expression、filter、border-radius、box-shadow、gradients
  1. 可讀性、可維護性
  • 代碼規(guī)范
  • 代碼語義化
  • 盡量避免使用hack
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 14,122評論 1 92
  • 1.Web標準 Web標準是由一系列標準組合而成。一個網頁主要由三部分組成:結構層、表現層和行為層。對應的標準也分...
    sissi110閱讀 1,371評論 0 4
  • CSS reset ** 1、placeholder 顏色重置 ** 2、 input 右測 小叉刪除 ** 3、...
    ddai_Q閱讀 627評論 1 2
  • 本文參加#未完待續(xù),就要表白#活動,本人承諾,內容為原創(chuàng),且為在其他平臺發(fā)表過。 你的名字,就這樣悄無...
    天天向上的小白白閱讀 302評論 0 0
  • 蝸居深山小小一隅 獨自背著竹簍下田勞作; 獨自挽起褲管下河撈魚; 獨自提著鐵鍬打點花草; 獨自升起炊煙點火做飯; ...
    南肆櫻語閱讀 306評論 0 3

友情鏈接更多精彩內容