今天有位朋友一早从妙味课堂转来一个有关于CSS布局的面试题,需要解决,花了点时间写了几个DEMO,放上来与大家分享受。那么我们在看DEMO之前一起先来看看这个面试题的具体要求吧:
仔细分析试题要求,要达到效果其实也并不是太难,只是给人感觉像有点蛋疼的问题一样。但是你仔细看后你会觉得不是那么回事:
上面简单的分析了一下实现过程,那么最终关键之处应该是就是“让你的代码要能同时实现两点,其一就是左边固定,右边自适应的布局;其二就是实现两列等高的布局”,如果这两个功能完成,那么你也就可以交作业了。那么下面我们就先来看看这两 点是如合实现:
这样的布局,其实不是难点,我想很多同学都有实现过,那么我就在此稍微介绍两种常用的方法:
方法一:浮动布局
这种方法我采用的是左边浮动,右边加上一个margin-left值,让他实现左边固定,右边自适应的布局效果
HTML Markup
<div id="left">Left sidebar</div> <div id="content">Main Content</div>
CSS Code
<style type="text/css"> *{ margin: 0; padding: 0; } #left { float: left; width: 220px; background-color: green; } #content { background-color: orange; margin-left: 220px;/*==等于左边栏宽度==*/ } </style>
上面这种实现方法最关键之处就是自适应宽度一栏“div#content”的“margin-left”值要等于固定宽度一栏的宽度值,大家可以点击查看上面代码的DEMO
方法二:浮动和负边距实现
这个方法采用的是浮动和负边距来实现左边固定宽度右边自适应宽度的布局效果,大家可以仔细对比一下上面那种实现方法,看看两者有什么区别:
HTML Markup
<div id="left"> Left Sidebar </div> <div id="content"> <div id="contentInner"> Main Content </div> </div>
CSS Code
*{ margin: 0; padding: 0; } #left { background-color: green; float: left; width: 220px; margin-right: -100%; } #content { float: left; width: 100%; } #contentInner { margin-left: 220px;/*==等于左边栏宽度值==*/ background-color: orange; }
这种方法看上去要稍微麻烦一点,不过也是非常常见的一种方法,大家可以看看他的DEMO效果。感觉一下,和前面的DEMO有什么不同之处。
我在这里就只展示这两种方法,大家肯定还有别的实现方法,我就不在多说了,因为我们今天要说的不是这个问题。上面完成了试题的第一种效果,那么大家就要想办法来实现第二条要求——两列等高布局。这一点也是本次面试题至关重要的一点,如果你要是不清楚如何实现等高布局的话,我建议您先阅读本站的《八种创建等高列布局》,里面详细介绍了八种等高布局的方法,并附有相关代码,而且我们后面的运用中也使用了其中的方法。
现在关键的两点都完成了,那么我们就需要实现第三条要求,实现最小高度的设置,这个方法很简单:
min-height: 200px; height: auto !important; height: 200px;
上面的代码就轻松的帮我们实现了跨浏览器的最小高度设置问题。这样一来,我们可以交作业了,也完面了这个面试题的考试。为了让大家更能形象的了解,我在这里为大家准备了五种不同的实现方法:
方法一:
别的不多说,直接上代码,或者参考在线DEMO,下面所有的DEMO都有HTML和CSS代码,感兴趣的同学自己慢慢看吧。
HTML Markup
<div id="container"> <div id="wrapper"> <div id="sidebar">Left Sidebar</div> <div id="main">Main Content</div> </div> </div>
CSS Code
<style type="text/css"> * { margin: 0; padding: 0; } html { height: auto; } body { margin: 0; padding: 0; } #container { background: #ffe3a6; } #wrapper { display: inline-block; border-left: 200px solid #d4c376;/*==此值等于左边栏的宽度值==*/ position: relative; vertical-align: bottom; } #sidebar { float: left; width: 200px; margin-left: -200px;/*==此值等于左边栏的宽度值==*/ position: relative; } #main { float: left; } #maing, #sidebar{ min-height: 200px; height: auto !important; height: 200px; } </style>
查看在线DEMO。
方法二
HTML Markup
<div id="container"> <div id="left" class="aside">Left Sidebar</div> <div id="content" class="section">Main Content</div> </div>
CSS Code
<style type="text/css"> *{margin: 0;padding: 0;} #container { overflow: hidden; } #left { background: #ccc; float: left; width: 200px; margin-bottom: -99999px; padding-bottom: 99999px; } #content { background: #eee; margin-left: 200px;/*==此值等于左边栏的宽度值==*/ margin-bottom: -99999px; padding-bottom: 99999px; } #left, #content { min-height: 200px; height: auto !important; height: 200px; } </style>
查看在线的DEMO。
方法三:
HTML Markup
<div id="container"> <div id="content">Main Content</div> <div id="sidebar">Left Sidebar</div> </div>
CSS Code
*{margin: 0;padding: 0;} #container{ background-color:#0ff; overflow:hidden; padding-left:220px; /* 宽度大小等与边栏宽度大小*/ } * html #container{ height:1%; /* So IE plays nice */ } #content{ background-color:#0ff; width:100%; border-left:220px solid #f00;/* 宽度大小等与边栏宽度大小*/ margin-left:-220px;/* 宽度大小等与边栏宽度大小*/ float:right; } #sidebar{ background-color:#f00; width:220px; float:right; margin-left:-220px;/* 宽度大小等与边栏宽度大小*/ } #content, #sidebar { min-height: 200px; height: auto !important; height: 200px; }
查看在线DEMO效果。
方法四:
HTML Markup
<div id="container2"> <div id="container1"> <div id="col1">Left Sidebar</div> <div id="col2">Main Content</div> </div> </div>
CSS Code
*{padding: 0;margin:0;} #container2 { float: left; width: 100%; background: orange; position: relative; overflow: hidden; } #container1 { float: left; width: 100%; background: green; position: relative; lef