1. 程式人生 > >CSS 兩列布局---左側固定,右側自適應

CSS 兩列布局---左側固定,右側自適應

前端一個小面試題總結如下:
1、方法一,高度100%,左側float,右側自動寬度(會自動佔滿剩餘寬度)。
2、方法二,絕對定位absolute,使用方法一的HTML結構,左側絕對定位,拉出文件流,高度100%並設定一定寬度400px。右側自動寬度(將佔100%)並設定margin-left:400px。
3、方法三, 右側寬度與高度均100%,左側部分float,使左側部分被包含到右側部分,如下面的HTML結構。
以下是程式碼結構:
至於其他的方法就不在寫了,道理都是一樣的

<!DOCTYPE html>
<html>
    <head>
        <meta
charset="UTF-8">
<title></title> </head> <style> html{ /*相容firefox的div高度100%*/ height: 100% } .body{ height: 100%; } .left{ float: left; width: 400px; height: 100%; /*解決IE6的3px-Bug*/
/*IE6下當浮動元素與非浮動元素相鄰時,3px的Bug就會出現,它會偏移3畫素。*/ _margin-right: -3px; background-color:crimson; border: 1px solid burlywood; }
.right{ width: auto; height: 100%; background-color: forestgreen; border
: 1px solid burlywood
; }
</style> <body> <div class="left"> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> </div> <div class="right"> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> hello,親愛的小一姑娘 <br /> </div> </body> </html>