這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)CSS三欄布局中如何進(jìn)行中間固定寬度兩邊自適應(yīng)寬度,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
西雙版納ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!今天早上在阿當(dāng)大俠的編寫高質(zhì)量前端代碼群中與幾位朋友一起探討和學(xué)習(xí)了一種用div+css進(jìn)行的三列(三欄)布局,而且是中間固定左右兩邊自適應(yīng)寬度,聽起來蠻有意思的。因?yàn)橐郧爸皇桥龅竭^,左右兩列固定而中間自適應(yīng)的運(yùn)用。于是思考一下馬上敲起了鍵盤自己實(shí)戰(zhàn)了一下,接著與大家一起測(cè)試,還是通過了各瀏覽器的考驗(yàn),為了方便自己以后好查閱,也想讓不知道的朋友一起學(xué)習(xí)一下這樣的布局,特整理了一下代碼,貼上來與大家一起分享和學(xué)習(xí)。
在講這種布局之前,我還想和大家一起回想一下三列布局中的另一種,就是左右兩列固定,中間自適應(yīng)寬度。這種布局方法,網(wǎng)上問問G爸和度娘一定會(huì)有一大堆,但我還是要重復(fù)說說,方便自己今后查閱,記性太差,沒辦法。別的先不說了,就開始進(jìn)入主題吧。對(duì)于兩邊固定中間一列自適應(yīng)的布局方法,我最早使用的是絕對(duì)定位法。先看代碼
代碼如下:
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"left"</span>></span>左邊欄<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"right"</span>></span>右邊欄<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"main"</span>></span>主內(nèi)容<span class="tag"></<span class="title" >div</span>></span>
比如說,我左右兩列都是220px,中間寬度自適應(yīng),那么我們使用絕對(duì)定位實(shí)現(xiàn)的方法是這樣的
代碼如下:
<span class="tag">html</span>,<span class="tag">body</span> <span class="rules">{
<span class="rule"><span class="attribute" >margin</span>:<span class="value"><span class="number" >0</span>;</span></span>
<span class="rule"><span class="attribute" >padding</span>:<span class="value"><span class="number" >0</span>;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >100</span>%;</span></span>
<span class="rule">}</span></span>
<span class="id" >#left</span>,
<span class="id" >#right</span> <span class="rules">{
<span class="rule"><span class="attribute" >position</span>:<span class="value"> absolute;</span></span>
<span class="rule"><span class="attribute" >top</span>:<span class="value"><span class="number" >0</span>;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >220</span>px;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >100</span>%;</span></span>
<span class="rule">}</span></span>
<span class="id" >#left</span> <span class="rules">{
<span class="rule"><span class="attribute" >left</span>:<span class="value"><span class="number" >0</span>;</span></span>
<span class="rule">}</span></span>
<span class="id" >#right</span> <span class="rules">{
<span class="rule"><span class="attribute" >right</span>:<span class="value"><span class="number" >0</span>;</span></span>
<span class="rule">}</span></span>
<span class="id" >#main</span> <span class="rules">{
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >230</span>px;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >100</span>%;</span></span>
<span class="rule">}</span></span>
這種方法是最簡(jiǎn)單,也是麻煩最多的,如果中間欄含有最小寬度限制,或是含有寬度的內(nèi)部元素,當(dāng)瀏覽器寬度小到一定程度,會(huì)發(fā)生層重疊的情況。我個(gè)人現(xiàn)在不在建議使用這種布局。
第二種方法采用的是浮動(dòng)布局
這種方法和上面的絕對(duì)定位方法很相似,只不過這里采用的是浮動(dòng),而不是絕對(duì)定位,先來看其html代碼
代碼如下:
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"left"</span>></span>left <span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"right"</span>></span>right<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"main"</span>></span>mian<span class="tag"></<span class="title" >div</span>></span>
這種方法我利用的就是浮動(dòng)原理,左右定寬度分別進(jìn)行左浮動(dòng)和右浮動(dòng),此時(shí)主內(nèi)容列(中間列沒有定度)主會(huì)自動(dòng)插入到左右兩列的中間,最要注意的一點(diǎn)是,中間列一定要放在左右兩列的后面,如上面的html代碼所示,下面我們一起來看看其css樣式是怎么實(shí)現(xiàn)的
代碼如下:
<span class="id" >#left</span>,
<span class="id" >#right</span> <span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >220</span>px;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >200</span>px;</span></span>
<span class="rule"><span class="attribute" >background</span>:<span class="value"> blue;</span></span>
<span class="rule">}</span></span>
<span class="id" >#right</span><span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> right;</span></span>
<span class="rule">}</span></span>
<span class="id" >#main</span> <span class="rules">{
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >230</span>px;</span></span>
<span class="rule"><span class="attribute" >background</span>:<span class="value"> red;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >200</span>px;</span></span>
<span class="rule">}</span></span>
是不是好簡(jiǎn)單一種方法呀,大家可以在本地機(jī)子上動(dòng)手測(cè)試一下,效果如下所示:
第三種方法:負(fù)的margin
使用這種方法就稍微復(fù)雜了一些了,使用的是負(fù)的margin值,而且html標(biāo)簽也增加了,先來看其代碼吧
代碼如下:
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"main"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"mainContainer"</span>></span>main content<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"left"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"leftContainer"</span> <span class="attribute" >class</span>=<span class="value" >"inner"</span>></span>left content<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"right"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"rightContainer"</span> <span class="attribute" >class</span>=<span class="value" >"inner"</span>></span>right<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
從上面的Html代碼中我們可以明顯得看出,在main,left,right三個(gè)div內(nèi)部我都增加了一個(gè)div,那么他們起了什么樣的作用呢,大家從下在的CSS中可以明顯的體會(huì)出來。
代碼如下:
<span class="id" >#main</span> <span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >100</span>%;</span></span>
<span class="rule">}</span></span>
<span class="id" >#mainContainer</span> <span class="rules">{
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >230</span>px;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >200</span>px;</span></span>
<span class="rule"><span class="attribute" >background</span>:<span class="value"> green;</span></span>
<span class="rule">}</span></span>
<span class="id" >#left</span> <span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >margin-left</span>:<span class="value"> -<span class="number" >100</span>%;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >230</span>px
}</span></span></span>
<span class="id" >#right</span> <span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >margin-left</span>:<span class="value"> -<span class="number" >230</span>px;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >230</span>px;</span></span>
<span class="rule">}</span></span>
<span class="id" >#left</span> <span class="class">.inner</span>,
<span class="id" >#right</span> <span class="class">.inner</span> <span class="rules">{
<span class="rule"><span class="attribute" >background</span>:<span class="value"> orange;</span></span>
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >10</span>px;</span></span>
<span class="rule"><span class="attribute" >height</span>:<span class="value"> <span class="number" >200</span>px;</span></span>
<span class="rule">}</span></span>
簡(jiǎn)單的說一下其實(shí)現(xiàn)原理,這種方法布局,主要運(yùn)用的是負(fù)的margin值。首先在div#main中我定了一個(gè)100%寬度并進(jìn)行左浮動(dòng),并且主內(nèi)容是放在其內(nèi)層div#mainContainer中,并在這個(gè)主內(nèi)容層中需要進(jìn)行一個(gè)margin-left和margin-right設(shè)置,并且這兩個(gè)值是很有講究的,并不是可以隨便設(shè)置的,這兩個(gè)值需要等于左右兩列的寬度。我們此處是230px。左欄和右欄都使用負(fù)的margin值加上左浮動(dòng)來布局,左欄是左浮動(dòng)并加了一個(gè)“margin-left:-100%”,這是因?yàn)閐iv#left前面有一個(gè)div#main,并且其寬度為100%,這樣一來在左欄定這個(gè)margin-left: -100%;剛好使左邊欄定位到頁面的最左邊;而右欄也進(jìn)行左浮動(dòng),但其定義的“margin-left”也是負(fù)值,并且等于其自身的寬度230px;最后在div#left,div#right中加上一個(gè)div.inner是為了更好的控制邊欄與主內(nèi)容列之間的間距。比如說此例的10px。大家可以看看其效果是不是和第二種方法一樣。
上面啰嗦完了常見的布局方法,接著進(jìn)我們一起來看另外一種三列布局中間固定寬度,兩邊自適應(yīng)寬度。對(duì)于我來說,這是一種很少碰到的布局方法,不知道大家有何體會(huì),那么下面我們一起來看這種布局方法的實(shí)現(xiàn)過程,同樣先來看html代碼:
代碼如下:
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"left"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"inner"</span>></span>this is left sidebar content<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"main"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"inner"</span>></span>this is main content<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >id</span>=<span class="value" >"right"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"inner"</span>></span>this is right siderbar content<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
這種方法也是借助于負(fù)的margin來實(shí)現(xiàn)的,首先我們?cè)谥虚g列定好固定值,因?yàn)榇酥凳遣粫?huì)在改變的,接著對(duì)其進(jìn)行左浮動(dòng);那么關(guān)鍵地主是在左右邊欄設(shè)置地方,這種方法是將其都進(jìn)行50%的寬度設(shè)置,并加上中負(fù)的左邊距,此負(fù)的左邊距最理想的值是中間欄寬度的一半加上1px,比如說此例中是"540px/2+1"也就是說他們都有一個(gè)"margin-left: -271px",這樣一來,左右邊欄內(nèi)容無法正常顯示,那是因?yàn)閷?duì)他們進(jìn)行了負(fù)的左邊距操作,現(xiàn)在只需要在左右邊欄的內(nèi)層div.inner將其拉回來,就OK了,大家可以看下在的代碼:
代碼如下:
<span class="id" >#left</span>,
<span class="id" >#right</span> <span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >0</span> <span class="number" >0</span> -<span class="number" >271</span>px;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >50</span>%;</span></span>
<span class="rule">}</span></span>
<span class="id" >#main</span> <span class="rules">{
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >540</span>px;</span></span>
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >background</span>:<span class="value"> green;</span></span>
<span class="rule">}</span></span>
<span class="class">.inner</span> <span class="rules">{
<span class="rule"><span class="attribute" >padding</span>:<span class="value"> <span class="number" >20</span>px;</span></span>
<span class="rule">}</span></span>
<span class="id" >#left</span> <span class="class">.inner</span>,
<span class="id" >#right</span> <span class="class">.inner</span> <span class="rules">{
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >0</span> <span class="number" >0</span> <span class="number" >271</span>px;</span></span>
<span class="rule"><span class="attribute" >background</span>:<span class="value"> orange;</span></span>
<span class="rule">}</span></span>
具體效果如下:
這種方法如果在ie下會(huì)存在布局混亂的bug,你可以將div#right和div#left中的width值稍作修改:
代碼如下:
<span class="id" >#left</span>,
<span class="id" >#right</span> <span class="rules">{
<span class="rule"><span class="attribute" >float</span>:<span class="value"> left;</span></span>
<span class="rule"><span class="attribute" >margin</span>:<span class="value"> <span class="number" >0</span> <span class="number" >0</span> <span class="number" >0</span> -<span class="number" >271</span>px;</span></span>
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >50</span>%;</span></span>
<span class="rule">*<span class="attribute" >width</span>:<span class="value"> <span class="number" >49.9</span>%;</span></span>
<span class="rule">}</span></span>
這樣一來,在ie下也就安全了。
實(shí)現(xiàn)這種效果的方法可能有很多,希望大家有更好的方法能一起分享一起學(xué)習(xí)。
2012年09月26日更新——CSS3 Flexbox
前面介紹了三種方法,實(shí)現(xiàn)L1F2L3布局效果,也就是第一列和第三列自適應(yīng)寬度,中間一列固定寬度。今天在給大家推薦一種CSS3實(shí)現(xiàn)這種布局的方法,采用CSS3的Flexbox,這種方法 不足之處就是只能在部分瀏覽器中使用,詳細(xì)的請(qǐng)看下面代碼:
HTML結(jié)構(gòu)
代碼如下:
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"grid"</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"col fluid"</span>></span>
...
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"col fixed"</span>></span>
...
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"><<span class="title" >div</span> <span class="attribute" >class</span>=<span class="value" >"col fluid"</span>></span>
...
<span class="tag"></<span class="title" >div</span>></span>
<span class="tag"></<span class="title" >div</span>></span>
CSS代碼
代碼如下:
<span class="class">.grid</span> <span class="rules">{
<span class="rule"><span class="attribute" >display</span>:<span class="value"> -webkit-flex;</span></span>
<span class="rule"><span class="attribute" >display</span>:<span class="value"> -moz-flex;</span></span>
<span class="rule"><span class="attribute" >display</span>:<span class="value"> -o-flex;</span></span>
<span class="rule"><span class="attribute" >display</span>:<span class="value"> -ms-flex;</span></span>
<span class="rule"><span class="attribute" >display</span>:<span class="value"> flex;</span></span>
<span class="rule">}</span></span>
<span class="class">.col</span> <span class="rules">{
<span class="rule"><span class="attribute" >padding</span>:<span class="value"> <span class="number" >30</span>px;</span></span>
<span class="rule">}</span></span>
<span class="class">.fluid</span> <span class="rules">{
<span class="rule"><span class="attribute" >flex</span>:<span class="value"> <span class="number" >1</span>;</span></span>
<span class="rule">}</span></span>
<span class="class">.fixed</span> <span class="rules">{
<span class="rule"><span class="attribute" >width</span>:<span class="value"> <span class="number" >400</span>px;</span></span>
<span class="rule">}</span></span>
效果如下面的demo所示:
上述就是小編為大家分享的CSS三欄布局中如何進(jìn)行中間固定寬度兩邊自適應(yīng)寬度了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:CSS三欄布局中如何進(jìn)行中間固定寬度兩邊自適應(yīng)寬度-創(chuàng)新互聯(lián)
分享地址:http://www.rwnh.cn/article42/csoohc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、關(guān)鍵詞優(yōu)化、網(wǎng)站收錄、服務(wù)器托管、全網(wǎng)營(yíng)銷推廣、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容