CSS布局快速入门

最近因为项目需要,不得不重新看看CSS/HTML之类的东西,不看不要紧,一看吓一跳

原来不知道真的是太多,以前从未认真对待过,这次总结了一下学习所得,算是对自己

有个交代,也可能让想了解CSS/HTML布局应用的朋友快速入门:

1. CSS 与HTML元素直接关联,以HTML h1元素为例。CSS定义如下:

[css]  view plain copy
  1. H1 {  
  2. marginauto;  
  3. width:600px;  
  4. font-size:18px;  
  5. font-weightbold;  
  6. }  

2.  CSS与HTML元素的id属性关联,以HTML元素img标签为例, CSS定义如下:

[css]  view plain copy
  1. #image_style{  
  2. marginauto;  
  3. width:450px;  
  4. height:450px;  
  5. background#1F1F1F;  
  6. border-stylesolid;  
  7. border-width5px;  
  8. border-color#0000FF;  
  9. }  
  10. #image_style .sub_style{  
  11. padding25px;  
  12. margin:auto;  
  13. }  

3. CSS与HTML元素的class属性关联,以HTML元素DIV为例,CSS定义如下:

[css]  view plain copy
  1. .header {  
  2. marginauto;  
  3. width600px;  
  4. background#2D2D2F;  
  5. }  

以上三个CSS综合运用结合HTML代码,页面效果如下:

完整的HTML代码如下:

[html]  view plain copy
  1. <html>  
  2. <head>  
  3.     <title>CSS Related to HTML element directly</title>  
  4.     <style type="text/css">  
  5.         .header {  
  6.             margin: auto;  
  7.             width: 600px;  
  8.             background: #FF1F1F;  
  9.         }  
  10.       
  11.         h1 {  
  12.         font-size:18px;  
  13.         font-weight: bold;  
  14.         text-align: center;  
  15.         /*display: inline;*/  
  16.         }  
  17.           
  18.         #image_style{  
  19.             margin: auto;  
  20.             width:450px;  
  21.             height:450px;  
  22.             background: #1F1F1F;  
  23.             border-style: solid;  
  24.             border-width: 5px;  
  25.             border-color: #0000FF;  
  26.         }  
  27.         #image_style .sub_style{  
  28.             padding: 25px;  
  29.             margin:auto;  
  30.         }  
  31.           
  32.     </style>  
  33. </head>  
  34. <body>  
  35.     <div class="header">  
  36.     <h1>My Fist CSS Introduce - Sample Codes</h1>  
  37.     </div>  
  38.     <div id="image_style">  
  39.         <img class="sub_style" src="images/star_stareu.png">  
  40.     </div>  
  41. </body>  
  42. </html>  

4. CSS与DIV元素结合使用实现排版布局

很多常见的博客系统网页布局可以通过CSS + DIV很容易的实现,下面是一个最常用的博

客网页布局CSS+DIV代码解释与介绍, 首先看一下布局效果:


DIV代码如下:

[html]  view plain copy
  1. <div id="container">  
  2.     <div id="header">  
  3.         <label>头区域</label>  
  4.     </div>  
  5.     <div id="leftBar">  
  6.         <label>左侧导航</label>  
  7.     </div>  
  8.     <div id="content">  
  9.         <label>内容</label>  
  10.     </div>  
  11.     <div id="rightBar">  
  12.         <label>右边框</label>  
  13.     </div>  
  14.     <div id="footer">  
  15.         <label><b>尾区域</b></label>  
  16.     </div>  
  17. </div>  
CSS的定义代码如下:

[css]  view plain copy
  1. #container {  
  2.   margin:auto/* IE6 supports them with a full and valid doctype */  
  3.   width800px;  
  4.   /* margin-left: 200px;    */  
  5.   background#ffffff;  
  6. }  
  7.   
  8. #header {  
  9.   height80;  
  10.   background#B0C4DE;  
  11. }  
  12.   
  13. #leftBar {  
  14.   floatleft;   
  15.   width150px;   
  16.   background#DFDF12;   
  17. }  
  18.   
  19. #content {  
  20.   float:left;  
  21.   width:500px;  
  22.   background-color#cdcde6;  
  23. }  
  24.   
  25. #rightBar {   
  26.   float:right;   
  27.   width150px;   
  28.   background#EBEBEB;   
  29. }  
  30.   
  31. #rightBar #zhao_shang {  
  32.     /*height:600px; - comment it */  
  33.     padding20px;  
  34. }  
  35.   
  36. #footer {   
  37.   clear:both;  
  38.   text-aligncenter;  
  39.   background:#DDDDDD;  
  40. }   

5.  CSS注释语法 - CSS注释代码使用如下语法格式/* 代码片段*/

6. 浏览器支持问题

上面的代码在IE6/IE7/IE8中显示时候,HTML页面头必须声明doctype,否则margin: atuo;

不能被IE浏览器识别,doctype声明如下:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
该布局的完整HTML代码如下,copy之后save为html文件可以直接在chrome运行:

[html]  view plain copy
  1. <html>  
  2. <head>  
  3.     <title>CSS Related to HTML element directly</title>  
  4.     <style type="text/css">  
  5.         #container {  
  6.           margin:auto; /* IE6 supports them with a full and valid doctype */  
  7.           width: 800px;  
  8.           /* margin-left: 200px;    */  
  9.           background: #ffffff;  
  10.         }  
  11.           
  12.         #header {  
  13.           height: 80;  
  14.           background: #B0C4DE;  
  15.         }  
  16.           
  17.         #leftBar {  
  18.           float: left;   
  19.           width: 150px;   
  20.           background: #DFDF12;   
  21.         }  
  22.           
  23.         #content {  
  24.           float:left;  
  25.           width:500px;  
  26.           background-color: #cdcde6;  
  27.         }  
  28.           
  29.         #rightBar {   
  30.           float:right;   
  31.           width: 150px;   
  32.           background: #EBEBEB;   
  33.         }  
  34.           
  35.         #rightBar #zhao_shang {  
  36.             /*height:600px; - comment it */  
  37.             padding: 20px;  
  38.         }  
  39.           
  40.         #footer {   
  41.           clear:both;  
  42.           text-align: center;  
  43.           background:#DDDDDD;  
  44.         }   
  45.     </style>  
  46. </head>  
  47. <body>  
  48.     <div id="container">  
  49.         <div id="header">  
  50.             <label>头区域</label>  
  51.         </div>  
  52.         <div id="leftBar">  
  53.             <label>左侧导航</label>  
  54.         </div>  
  55.         <div id="content">  
  56.             <label>内容</label>  
  57.         </div>  
  58.         <div id="rightBar">  
  59.             <label>右边框</label>  
  60.         </div>  
  61.         <div id="footer">  
  62.             <label><b>尾区域</b></label>  
  63.         </div>  
  64.     </div>  
  65. </body>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值