css layout/fixed03: 2 column left navigation
2012-08-22 21:58 youxin 阅读(347) 评论(0) 编辑 收藏 举报<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>CSSeasy.com example page</title> <style type="text/css"> /* You are allowed to do whatever you want with this layout. Though I would be pleased if you placed a link on your site to csseasy.com or to profit42.com (best "blog about hacking" ever). Donations are also welcome: paypal@profit42.com (or follow the donation button on csseasy.com) */ body { width:750px; margin:0 auto; margin-top:30px; } /* ----- HEADER ----- */ #header { width:750px; height:150px; background-color:#333333; } /* FAUX LAYOUT WRAPPER */ 可以不要 /* This div is optional: When you delete it the navigation bar and the content block won't have the same size when one of them has a larger height then the other. I recommend you to leave this faux layout wrapper here. IMPORTANT: In order to make this work you have to download http://csseasy.com/layouts/fixed/background_3.gif (copy past in your address bar) and place it in the same folder as this html file. DON'T HOTLINK to this file. */ #wrapper{ width:750px; background:url(background_3.gif) repeat-y; /* WARNING: IF YOU CHANGE THE COLORS OF THE LAYOUT YOU HAVE TO CHANGE THIS IMAGE! */ overflow:auto; /* USE FIREFOX GUYS! WE ONLY NEED THIS LINE BECAUSE INTERNET EXPLODER ISN'T READING CODE PROPERLY */ margin-top:10px; } /* ----- NAVIGATION ----- */ #navigation { width:200px; min-height:500px; /* for modern browsers */ height:auto !important; /* for modern browsers */ height:500px; /* for IE5.x and IE6 */ background-color:#333333; float:left; 让这个div浮动到左边,后面的块元素可以在这行继续显示,不要float:left,后面的块元素会另起行。
} /* ----- MAIN CONTENT ----- */ #content { margin-left:210px; width:540px; min-height:500px; /* for modern browsers */ height:auto !important; /* for modern browsers */ height:500px; /* for IE5.x and IE6 */ background-color:#333333; margin-top:10px; } /* ----- FOOTER ----- */ #footer { margin-top:10px; width:750px; height:100px; background-color:#333333; } </style> </head> <body> <div id="header"> </div> <div id="wrapper"> 这个可以不要 <div id="navigation"> </div> <div id="content"> </div> </div> <div id="footer"> </div> </body> </html>
显示如下:
http://www.csseasy.com/layouts/fixed/2column_left.html
设置了Min-height为什么还要设置
height:auto !important; /* for modern browsers */ height:500px; /* for IE5.x and IE6 */
Cross-Browser Min Height
div {
min-height:500px;
height:auto!important;
height:500px;
}
This works because (thankfully?) IE treats "height" how "min-height" is supposed to be treated.
ie对待“height”,就像现代浏览器对待“min-heigh"那样。
First, let's define the difference between height
and min-height
. Height is a fixed height. "I want it this height, darn it! This height -- not more not less." Min-height is, as the name indicates, a fixed minimum height. "I want it to be at least this height, darn it! This height, possibly more, but certainly not less." An !important
declaration can be likened to an override. It allows you to declare an overriding property, which will be considered taking precedence even if there are other rules with the same specificity declaring something else.
Standards compliant browsers (which now, for this exercise, also includes IE7) behave just that way. IE6, on the other hand, does not understand min-height
at all. And, furthermore, it treats height
as if it were min-height
. Additionally, IE6 ignores !important
.
Now, let's look at each declaration individually.
min-height: 500px;
This declares the minimum desired height to 500px.
height: auto !important;
Let the actual final height auto-adjust (while obeying the declared min-height, of course). Even though auto
is the default for any element's height, we want to declare it because of the following rule.
height: 500px;
Declare the fixed height as 500px.
Now, why does this work? Well, standards compliant browsers will see:
min-height: 500px;
height: auto;
IE6, on the other hand, due to ignoring !important
and not understanding min-height
, will see:
height: 500px;
Due to the incorrect implementation in IE6, in all browsers we now get an element which is at least 500px tall, but will stretch to accommodate whatever additional height is necessary.
是为了与ie兼容,可以不要。
为什么navigation和content有相同的margin-top;我们只设置了content的margin-top;
下面的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> body{ margin:0; padding:0; } #content1 { float:left; width:300px; height:200px; background:lime; } #content2 { width:350px; height:200px; background:red; margin-left:320px; margin-top:50px; } </style> </head> <body> <div id="content1"></div> <div id="content2"></div> </body> </html>
我们可以看到第一个浮动的div与第二个div有相同的margin-top:50px;
如果我们再设置第一个div的margin-top:100px;
第一个div的margin-top变成了:150px; 第二个还是50。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通