網(wǎng)頁(yè)布局對(duì)改善網(wǎng)站的外觀非常重要。請(qǐng)慎重設(shè)計(jì)您的網(wǎng)頁(yè)布局。 網(wǎng)站布局大多數(shù)網(wǎng)站會(huì)把內(nèi)容安排到多個(gè)列中(就像雜志或報(bào)紙那樣)。 大多數(shù)網(wǎng)站可以使用 <div> 或者 <table> 元素來(lái)創(chuàng)建多列。css 用于對(duì)元素進(jìn)行定位,或者為頁(yè)面創(chuàng)建背景以及色彩豐富的外觀。 雖然我們可以使用html table 標(biāo)簽來(lái)設(shè)計(jì)出漂亮的布局,但是 table 標(biāo)簽是不建議作為布局工具使用的 - 表格不是布局工具。 html 布局 - 使用 <div> 元素<div> 元素是用于分組 HTML 元素的塊級(jí)元素 下面的范例使用了五個(gè) div 元素來(lái)創(chuàng)建多列布局 <!DOCTYPE html> <Meta charset="utf-8"> <div id="container" style="width:500px"> <div id="header" style="background-color:#FFA500;"> <h1 style="margin-bottom:0;">主要的網(wǎng)頁(yè)標(biāo)題</h1></div> <div id="menu" style="background-color:#FFD700;height:200px;width:100px;float:left;"> <b>菜單</b><br> HTML<br>css<br>JavaScript</div> <div id="content" style="background-color:#EEEEEE;height:200px;width:400px;float:left;"> 內(nèi)容在這里</div> <div id="footer" style="background-color:#FFA500;clear:both;text-align:center;"> 版權(quán) F2er.com</div> </div> 上面的 HTML 代碼會(huì)產(chǎn)生如下結(jié)果 HTML 布局 - 使用表格使用 HTML <table> 標(biāo)簽是創(chuàng)建布局的一種簡(jiǎn)單的方式 大多數(shù)站點(diǎn)可以使用 <div> 或者 <table> 元素來(lái)創(chuàng)建多列 CSS 用于對(duì)元素進(jìn)行定位,或者為頁(yè)面創(chuàng)建背景以及色彩豐富的外觀 雖然我們可以使用 HTML table 標(biāo)簽來(lái)設(shè)計(jì)出漂亮的布局,但是 table 標(biāo)簽是不建議作為布局工具使用的 下面的范例使用三行兩列的表格 - 第一和最后一行使用 colspan 屬性來(lái)橫跨兩列 <!DOCTYPE html> <Meta charset="utf-8"> <table width="500" border="0"> <tr> <td colspan="2" style="background-color:#FFA500;"> <h1>主要的網(wǎng)頁(yè)標(biāo)題</h1> </td> </tr> <tr> <td style="background-color:#FFD700;width:100px;"> <b>菜單</b><br> HTML<br> CSS<br>JavaScript</td> <td style="background-color:#eeeeee;height:200px;width:400px;"> 內(nèi)容在這里</td> </tr> <tr> <td colspan="2" style="background-color:#FFA500;text-align:center;"> 版權(quán)F2er.com</td> </tr> </table> 效果基本同上。 HTML 布局 - 有用的提示使用 CSS 最大的好處是,如果把 CSS 代碼存放到外部樣式表中,那么站點(diǎn)會(huì)更易于維護(hù) 通過(guò)編輯單一的文件,就可以改變所有頁(yè)面的布局 HTML 布局的幾種方式1.浮動(dòng) 2.定位 3.分欄布局 column-count:auto | 整數(shù);---控制欄數(shù) column-width: auto | length;---每欄的寬度 column-gap : length ;---兩欄之間的間距 column-rule : 寬度,線型,顏色;---欄與欄的間隔線 類(lèi)似border,solid | dotted | dashed 實(shí)線 | 點(diǎn)線 | 虛線 column-width和column-count可以讓一個(gè)元素進(jìn)行多列布局 column-gap和column-rule就處在相鄰兩列之間 例子: <div class="con"> <h1>大數(shù)據(jù)下個(gè)人隱私的保護(hù)分析與研究</h1> <div> 一堆內(nèi)容 </div> </div> css .con{ width: 600px; column-count: 3; 分幾欄 column-gap: 10px; 每欄之間的距離 column-rule: 3px dotted red; 欄目之間的線 } .con h1{ -webkit-column-span: all; 標(biāo)題是否跨欄顯示 } 4.彈性布局 優(yōu)點(diǎn): 1 適應(yīng)性強(qiáng),在做不同屏幕分辨率的界面時(shí)非常實(shí)用 2 可以隨意按照寬度、比例劃分元素的寬高 3 可以輕松改變?cè)氐娘@示順序 4 彈性布局實(shí)現(xiàn)快捷,易維護(hù) display:Box;將一個(gè)元素的子元素以彈性布局進(jìn)行布局 Box-orient:horizontal || vertical || inherit 子元素排列方式 Box-direction:normal || reverse || inherit 子元素的排列順序 Box-align:start || end || center 子元素的對(duì)齊方式 (規(guī)定水平框中垂直位置 或 垂直框中水平位置) Box-pack: start || end || center 子元素的對(duì)齊方式(規(guī)定水平框中水平位置 或 垂直框中垂直位置) Box-flex:number;子元素如何分配剩余空間 Box-ordinal-group:number;子元素顯示順序 例子: <style> body,html{ width: 100%; height: 100%; display: -webkit-Box; -webkit-Box-orient:vertical; -webkit-Box-align:center; -webkit-Box-pack:center; } .con{ width: 90%; height: 90%; display: -webkit-Box; -webkit-Box-orient:vertical; border: 1px solid red; } .con .head{ height: 200px; display: -webkit-Box; -webkit-Box-orient:horizontal; } .con .head .logo{ width: 100px; height: 200px; background: pink; } .con .head .logoCon{ height: 200px; -webkit-Box-flex:1; background: green; } .con .content{ -webkit-Box-flex:1; background: orange; display: -webkit-Box; -webkit-Box-orient:horizontal; -webkit-Box-direction:reverse; } .content div{ width: 200px; text-align: center; } .con .footer{ height: 100px; background: blue; } </style> </head> <body> <div class="con"> <div class="head"> <div class="logo"></div> <div class="logoCon"></div> </div> <div class="content"> <div class="con1">111</div> <div class="con2">222</div> <div class="con3">333</div> </div> <div class="footer"> </div> </div> 5.響應(yīng)式布局 一個(gè)網(wǎng)站能夠兼容多個(gè)終端---而不是為每個(gè)終端做一個(gè)特定的版本@media all(用于所有的設(shè)備) || screen (用于電腦屏幕,平板電腦,智能手機(jī)等) and|not|only(三個(gè)關(guān)鍵字可以選) <style media="screen"> @media screen and (max-width:600px){ .con{ background:red; } } @media screen and (min-width:600px) and (max-width:800px){ .con{ background:blue; } } @media screen and (min-width:800px){ .con{ background:green; } } .con{ width: 100%; height: 100px; } </style> </head> <body> <div class="con"> </div> </body> |