本教程针对熟悉HTML4、初步使用过CSS的人。
CSS的基本结构如下:
<selector>{
<attr1>:<value1>;
<attr2>:<value2>;
<attr3>:<value3>;
......
<attrN>:<valueN>;
}
解释:
这里举个简单的例子:
body{ //对<body>内的元素进行设置
font-size:12px; //设置字体大小为12px,px是pixel的缩写,表示像素
font-family:sans-serif,Georgia; //设置首选字体为sans-serif,如果本机内没安装此字体,则使用Georgia
}
这里只介绍两种最常用的:
1 /** 2 * Eric Meyer‘s Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/) 3 * http://cssreset.com 4 */ 5 html, body, div, span, applet, object, iframe, 6 h1, h2, h3, h4, h5, h6, p, blockquote, pre, 7 a, abbr, acronym, address, big, cite, code, 8 del, dfn, em, img, ins, kbd, q, s, samp, 9 small, strike, strong, sub, sup, tt, var, 10 b, u, i, center, 11 dl, dt, dd, ol, ul, li, 12 fieldset, form, label, legend, 13 table, caption, tbody, tfoot, thead, tr, th, td, 14 article, aside, canvas, details, embed, 15 figure, figcaption, footer, header, hgroup, 16 menu, nav, output, ruby, section, summary, 17 time, mark, audio, video { 18 margin: 0; 19 padding: 0; 20 border: 0; 21 font-size: 100%; 22 font: inherit; 23 vertical-align: baseline; 24 } 25 /* HTML5 display-role reset for older browsers */ 26 article, aside, details, figcaption, figure, 27 footer, header, hgroup, menu, nav, section { 28 display: block; 29 } 30 body { 31 line-height: 1; 32 } 33 ol, ul { 34 list-style: none; 35 } 36 blockquote, q { 37 quotes: none; 38 } 39 blockquote:before, blockquote:after, 40 q:before, q:after { 41 content: ‘‘; 42 content: none; 43 } 44 table { 45 border-collapse: collapse; 46 border-spacing: 0; 47 }
应用:
选择器定义准则:
在不同的框架中(如 JQuery)可能会新增很多其他选择器,但是以上的几个选择器是最基本的。
此处只是实现布局,而不是实现全部效果。
效果:
代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <style type="text/css"> 5 #container{ 6 margin:0 auto; 7 max-width:980px; 8 background:gray; 9 } 10 #header{ 11 background:red; 12 height:100px; 13 } 14 #nav{ 15 background:orange; 16 height:50px; 17 } 18 #banner{ 19 background:blue; 20 height:300px; 21 } 22 #news{ 23 background:green; 24 width:30%; 25 height:100px; 26 float:left; 27 } 28 #focus{ 29 background:yellow; 30 width:30%; 31 height:100px; 32 float:left; 33 } 34 #resources{ 35 background:gray; 36 width:40%; 37 height:100px; 38 float:right; 39 } 40 #footer{ 41 background:purple; 42 height:100px; 43 clear:left; 44 } 45 </style> 46 </head> 47 <body> 48 <div id="container"> 49 <div id="header">header</div> 50 <div id="nav">nav</div> 51 <div id="banner">banner</div> 52 <div id="main"> 53 <div id="news">news</div> 54 <div id="focus">focus</div> 55 <div id="resources">resources</div> 56 </div> 57 <div id="footer">footer</div> 58 </div> 59 </body> 60 </html>
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>无标题文档</title> 6 <style type="text/css"> 7 #child1{ 8 float:left; 9 width:300px; 10 height:300px; 11 background:yellow; 12 } 13 #child2{ 14 float:left; 15 width:300px; 16 height:300px; 17 background:blue; 18 } 19 </style> 20 </head> 21 <body> 22 <div id="father"> 23 <div id="child1"></div> 24 <div id="child2"></div> 25 </div> 26 </body> 27 </html>
效果:
说明:
代码:
1 <!--实现了田字格布局--> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <style type="text/css"> 6 div{ 7 border-style: solid; 8 border-color: #000000; 9 border-width:5px; 10 margin:10px; 11 width:100px; 12 height:100px; 13 } 14 #c1,#c2,#c3,#c4{ 15 float:left; 16 } 17 #c3{ 18 clear:left; 19 } 20 </style> 21 </head> 22 <body> 23 <div id="c1"></div> 24 <div id="c2"></div> 25 <div id="c3"></div> 26 <div id="c4"></div> 27 </body> 28 </html>
如何文字在div中垂直居中?
效果:
代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>实现metro布局</title> 6 <style> 7 html{ 8 font-family:"Microsoft YaHei UI"; 9 color:white; 10 } 11 #container{ 12 width:655px; 13 height:90px; 14 } 15 #d1,#d2,#d3,#d4,#d5,#d6,#d7,#d8,#d9,#d10{ 16 float:left; 17 text-align:center; /*文字水平居中*/ 18 margin:1px; 19 } 20 #d1{ 21 background-color:#CC3333; 22 width:106px; 23 height:58px; 24 line-height:58px; /*将height和line-height设置为一样可以使文字垂直居中*/ 25 font-size:20px; 26 } 27 #d2{ 28 background-color:#0048BE; 29 font-size:25px; 30 width:215px; 31 height:58px; 32 line-height:58px; 33 } 34 #d3{ 35 background-color:#FF9900; 36 font-size:12px; 37 width:107px; 38 height:28px; 39 line-height:28px; 40 } 41 #d4{ 42 background-color:#FF9900; 43 font-size:12px; 44 width:107px; 45 height:28px; 46 line-height:28px; 47 margin-left:0px; 48 } 49 #d5{ 50 background-color:#91009E; 51 font-size:20px; 52 width:106px; 53 height:58px; 54 line-height:58px; 55 } 56 #d6{ 57 background-color:#00839A; 58 font-size:12px; 59 width:106px; 60 height:30px; 61 line-height:30px; 62 } 63 #d7{ 64 background-color:#542FB0; 65 font-size:12px; 66 width:107px; 67 height:30px; 68 line-height:30px; 69 } 70 #d8{ 71 background-color:#FF9900; 72 font-size:12px; 73 width:107px; 74 height:30px; 75 line-height:30px; 76 margin-left:0px; 77 } 78 #d9{ 79 background-color:#0048BE; 80 font-size:25px; 81 width:215px; 82 height:60px; 83 line-height:60px; 84 margin-top:-29px; 85 } 86 #d10{ 87 background-color:#542FB0; 88 font-size:12px; 89 width:106px; 90 height:30px; 91 line-height:30px; 92 } 93 </style> 94 </head> 95 96 <body> 97 <div id="container"> 98 <div id="d1">燕麦食品</div> 99 <div id="d2">自学电脑</div> 100 <div id="d3">51我要自学网</div> 101 <div id="d4">机械设计与制造</div> 102 <div id="d5">汽车年审</div> 103 <div id="d6">自学计算机</div> 104 <div id="d7">入门吉他</div> 105 <div id="d8">图标设计</div> 106 <div id="d9">Javascript框架</div> 107 <div id="d10">木吉他入门教学</div> 108 </div> 109 </body> 110 </html>
效果如下:
分析:
HTML4代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>使用CSS画一个三角形</title> 6 <style type="text/css"> 7 #child{ 8 width:0px; 9 height:0px; 10 border:100px solid red; 11 border-top-color:transparent; 12 border-left-color:transparent; 13 border-right-color:transparent; 14 } 15 </style> 16 </head> 17 <body> 18 <div id="child"></div> 19 </body> 20 </html>
这里补充使用HTML5 Canvas 画一个三角形,canvas技术可以理解为“把页面当作一个画布,你可以在画布上画任何的图形”。canvas的使用请查阅额外资料。
HTML5代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>使用HTML5 Canvas 画一个三角形</title> 6 </head> 7 <body> 8 <canvas id="canvas" width="300" height="300"> 9 该浏览器不支持 canvas api 10 </canvas> 11 </body> 12 <script type="text/javascript"> 13 var canvas = document.getElementById("canvas"); 14 var context = canvas.getContext("2d"); 15 context.fillStyle="red"; //填充颜色为红色 16 context.beginPath(); //开始路径 17 context.moveTo(100,100); //起始点为(100,100) 18 context.lineTo(75,125); //从(100,100)到(75,125)有一条线 19 context.lineTo(125,125); //从(75,125)到(125,125)有一条线 20 context.closePath(); //从(125,125)到(100,100)有一条线 21 context.fill(); //填充 22 </script> 23 </html>
效果如下:
分析:利用CSS中的border属性画两个三角形和一个矩形。
代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>CSS画一棵树</title> 6 <style type="text/css"> 7 #child1{ 8 width:0px; 9 height:0px; 10 border:100px solid green; 11 border-top-color:transparent; 12 border-left-color:transparent; 13 border-right-color:transparent; 14 } 15 #child2{ 16 width:0px; 17 height:0px; 18 border:100px solid green; 19 border-top-style:none; 20 border-top-color:transparent; 21 border-left-color:transparent; 22 border-right-color:transparent; 23 } 24 #child3{ 25 width:80px; 26 height:50px; 27 float:left; 28 } 29 #child4{ 30 float:left; 31 width:0px; 32 height:50px; 33 border:20px solid brown; 34 } 35 </style> 36 </head> 37 <body> 38 <div id="child1"></div> <!--第一片叶子 --> 39 <div id="child2"></div> <!--第二片叶子 --> 40 <div id="child3"></div> <!--透明支撑物,为了将树干放到叶子中间--> 41 <div id="child4"></div> <!--树干--> 42 </body> 43 </html>
效果:
分析:
代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>无标题文档</title> 6 <style> 7 li{ 8 list-style-type:none; 9 background-image:url(icon.png); 10 background-repeat:no-repeat; 11 background-position:0px -272px; 12 padding-left:26px; 13 } 14 </style> 15 </head> 16 17 <body> 18 <ul> 19 <li>国信办将严打网络淫秽色情及低俗信息</li> 20 <li>中央领导看望XXXXXX等老同志</li> 21 <li>春节期间我国将遇强冷空气 局地降温14℃</li> 22 </ul> 23 </body> 24 </html>
关于块级元素、内联元素的区别:
场景:我们需要实现一个导航栏。
问题:相邻 inline-block 之间会有间隔。
效果:
代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>inline-block间隔</title> 6 <style> 7 #nav a{ 8 display:inline-block; //设置inline-block后,相邻会有间隔 9 padding:4px; 10 width: 120px; 11 text-align:center; 12 background:#BEBEBE; 13 text-decoration:none; 14 font-family:‘Microsoft YaHei‘, 微软雅黑; 15 font-weight:bold; 16 font-size:20px; 17 color:white; 18 } 19 20 </style> 21 </head> 22 <body> 23 <div id="nav"> 24 <a href="#">HOME</a> 25 <a href="#">ARTICLES</a> 26 <a href="#">ABOUT</a> 27 </div> 28 </body> 29 </html>
那么怎么去除间隔呢?解决方法是在<style>标签中加入:html{font-size:0px} 即可。
效果:
场景:这里我们实现一个404页面。
效果:
分析:
原理图:
代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <style> 5 html,body{ 6 height:100%; /*高度撑满*/ 7 margin:0px; /*页边不留空白*/ 8 background:gray; 9 } 10 #container{ 11 margin:0 auto; /*水平居中*/ 12 height:100%; /*高度撑满*/ 13 width:500px; 14 15 } 16 #image1{ 17 width:490px; 18 margin:0 auto; 19 vertical-align:middle; 20 display:inline-block; 21 } 22 #dummyspan{ 23 width:0px; 24 height:100%; 25 display:inline-block; 26 vertical-align:middle; 27 } 28 </style> 29 </head> 30 <body> 31 <div id="container"> 32 <span id="dummyspan"></span> <!--插入一个高度撑满,宽度为0的虚拟元素--> 33 <a href="#"><img src="404错误.jpg" id="image1"></a> 34 </div> 35 </body> 36 </html>
效果:
代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>顶部导航</title> 6 <style> 7 html{ 8 height:2000px; /*为了体现导航是固定在顶端的,这里将页面故意设的高一点*/ 9 } 10 *{ 11 font-family: ‘Microsoft YaHei‘, 微软雅黑; 12 font-size:12px; 13 color:#666; 14 } 15 a{ 16 text-decoration:none; 17 color:#666; 18 } 19 #nav{ 20 background-color:#fdfdfd; 21 width: 100%; 22 height: 28px; 23 position:fixed; /*固定布局*/ 24 top:0px; /*固定的具体位置*/ 25 border-bottom: 1px solid #ebebeb; 26 } 27 #navInner{ 28 margin:0 auto; 29 width:960px; 30 height:100%; 31 32 } 33 #left{ 34 float:left; 35 margin-top: 8px; 36 } 37 #mobileIcon{ 38 background-image:url(icon.png); 39 background-repeat:no-repeat; 40 padding-left:12px; 41 margin-right: 11px; 42 } 43 #wap{ 44 margin-right: 10px; 45 } 46 #forIndex{ 47 margin:0px 12px 0px 10px; 48 } 49 #webmap{ 50 margin:0 10px 0 0; 51 52 } 53 #qqHelpLink{ 54 padding-right: 11px; 55 background-image:url(icon.png); 56 background-repeat:no-repeat; 57 background-position:right -115px; 58 } 59 #right{ 60 float:right; 61 } 62 #onekey,#weibo,#qzone,#qmail,#dingyue,#ilike{ 63 background-image:url(loginall_1.5.1.png); 64 background-repeat:no-repeat; 65 height:28px; 66 display:block; 67 float:right; 68 } 69 #onekey{ 70 background-position: left -450px; 71 width:70px; 72 } 73 #weibo{ 74 background-position: right -50px; 75 width:30px; 76 } 77 #qzone{ 78 background-position: right -100px; 79 width:30px; 80 } 81 #qmail{ 82 background-position: right -150px; 83 width:30px; 84 } 85 #dingyue{ 86 background-position: right -549px; 87 width:30px; 88 } 89 #ilike{ 90 background-position: -76px -645px; 91 width:30px; 92 } 93 </style> 94 </head> 95 96 <body> 97 <div id="nav"> 98 <div id="navInner"> 99 <div id="left"> 100 <a href="#" id="mobileIcon">手机新闻客户端</a> 101 <a href="#" id="wap">WAP版</a> 102 | 103 <a href="#" id="forIndex">设为首页</a> 104 <a href="#" id="webmap">网站地图</a> 105 <a href="#" id="qqHelpLink">帮助</a> 106 </div> 107 <div id="right"> 108 <a href="#" id="ilike"></a> 109 <a href="#" id="dingyue"></a> 110 <a href="#" id="qmail"></a> 111 <a href="#" id="qzone"></a> 112 <a href="#" id="weibo"></a> 113 <a href="#" id="onekey"></a> 114 </div> 115 </div> 116 </div> 117 </body> 118 </html>
下面的例子本来使用Bootstrap来实现的,现在我们从零开始实现,这样能够充分回顾我们CSS学习的知识。
原地址:http://runjs.cn/detail/8z2mwcfa
效果:
分析:
代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>无标题文档</title> 6 <style> 7 html,body{ 8 height:100%; 9 width:100%; 10 background-image:url(login_bg.PNG); /*背景*/ 11 font-family:"Microsoft YaHei UI"; /*默认字体是微软雅黑*/ 12 } 13 14 #container{ 15 width:480px; 16 height:100%;; 17 margin:0 auto; 18 vertical-align:middle; 19 } 20 #dummy{ 21 width:0px; 22 height:100%; 23 vertical-align:middle; 24 display:inline-block; 25 } 26 #loginbox{ 27 width:470px; 28 height:230px; 29 display:inline-block; 30 border-radius: 8px; /* 实现圆角边框 */ 31 background:#F5F5F5; 32 } 33 #left{ 34 width:55%; 35 height:230px; 36 float:left; 37 } 38 #right{ 39 border-left: 1px solid #ccc; 40 width:43%; 41 height:230px; 42 float:left; 43 } 44 .nametxt{ 45 width:220px; 46 height:30px; 47 margin-left:20px; 48 margin-bottom:15px; 49 border-radius:5px; 50 border: 1px solid #ccc; 51 } 52 #loginbtn{ 53 width:62px; 54 height:30px; 55 color:white; 56 float:right; 57 margin-right:20px; 58 font-size: 14px; 59 padding: 4px 12px; 60 background-image: linear-gradient(to bottom,#08c,#04c); 61 border-radius: 4px; 62 border: 1px solid #bbb; 63 } 64 #registerbtn{ 65 background-image: linear-gradient(to bottom,#fff,#e6e6e6); 66 padding: 4px 12px; 67 margin-bottom: 0; 68 width:62px; 69 height:30px; 70 font-size: 14px; 71 line-height: 20px; 72 border-radius: 4px; 73 border: 1px solid #bbb; 74 margin-left:8px; 75 } 76 </style> 77 </head> 78 <body> 79 <div id="container"> 80 <span id="dummy"></span> 81 <div id="loginbox"> 82 <form> 83 <div id="left"> 84 <h2 style="font-size:20px;font-weight:normal;padding:10px 10px 10px 20px;">商户登录</h2> 85 <input type="text" class="nametxt"/><br/> 86 <input type="text" class="nametxt"/><br/> 87 <input type="checkbox" style="margin-left:20px"> 下次自动登录 <button id="loginbtn">登录</button> 88 </div> 89 <div id="right"> 90 <h2 style="font-size:20px;font-weight:normal;padding:8px;">没有账户 ?</h2> 91 <div style="font-size:11px;padding:8px;line-height:18px">这里有一段文字啊,很多的文字啊,太多太多的文字了,主要可以介绍介绍注册的好处和公司或者项目概况。。。</div> 92 <button id="registerbtn">注册</button> 93 </div> 94 </form> 95 </div> 96 </div> 97 </body> 98 </html>
[1] 学习CSS很好的教程:http://zh.learnlayout.com/
[2] Metro UI CSS 中文版首页:http://www.w3cplus.com/MetroUICSS/index.php
[3] Bootstrap 中文版首页:http://www.bootcss.com/
[4] 默认的块级元素和内联元素:http://www.cnblogs.com/double-bin/archive/2011/12/19/2293090.html
[5] inline-block与float的区别:http://www.vanseodesign.com/blog/demo/inline-block/
[6] float详解:http://www.w3cplus.com/css/float.html
[7] 实现商品列表:http://blog.teamtreehouse.com/using-inline-block-to-display-a-product-grid-view
[8] 使用vertical-align实现垂直居中:http://www.cnblogs.com/xueming/archive/2012/03/21/VerticalAlign.html
[9] CSS选择器解释:http://www.ruanyifeng.com/blog/2009/03/css_selectors.html
[10] A Beginner‘s Guide to HTML & CSS: http://learn.shayhowe.com/html-css/
[11] An Advanced Guide to HTML & CSS: http://learn.shayhowe.com/advanced-html-css/
[12] Yahoo Pure CSS 框架: http://purecss.io/
[13] CSS3实现圆角:http://www.ruanyifeng.com/blog/2010/12/detailed_explanation_of_css3_rounded_corners.html
[14] 颜色的表示:http://www.dreamdu.com/css/css_colors/
原文:http://www.cnblogs.com/xiazdong/p/3526301.html