<!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> <style type="text/css"> /* 选择器: 选择器的作用就是找到对应的数据进行样式化。 1.标签选择器: 就是找到所有指定的标签进行样式化。 格式: 标签名{ 样式1;样式2.... } 例子: div{ color:#F00; font-size:24px; } 2. 类选择器: 使用类选择器首先要给html标签指定对应的class属性值。 格式: .class的属性值{ 样式1;样式2... } 例子: .two{ background-color:#0F0; color:#F00; font-size:24px; } 类选择器要注意的事项: 1. html元素的class属性值一定不能以数字开头. 2. 类选择器的样式是要优先于标签选择器的样式。 3. ID选择器: 使用ID选择器首先要给html元素添加一个id的属性值。 ID选择器的格式: #id属性值{ 样式1;样式2... } id选择器要注意的事项: 1. ID选择器的样式优先级是最高的,优先于类选择器与标签选择器。 2. ID的属性值也是不能以数字开头的。 3. ID的属性值在一个html页面中只能出现一次。 4. 交集选择器: 就是对选择器1中的选择器2里面的数据进行样式化。 选择器1 选择器2{ 样式1,样式2.... } 例子: .two span{ background-color:#999; font-size:24px; } 5. 并集选择器: 对指定的选择器进行统一的样式化。 格式: 选择器1,选择器2..{ 样式1;样式2... } span,a{ border-style:solid; border-color:#F00; } 6. 通过选择器: *{ 样式1;样式2... } */ *{ text-decoration:line-through; background-color:#CCC; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div id="one" class="two">这个是<span>第一个div标签</span>...</div> <div id="one" class="two">这个是<span>第二个div标签</span>...</div> <span>这个是一个span标签</span><br/> <a class="two" href="#">新闻标题</a> </body> </html>
伪类选选择器
<!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> <style type="text/css"> /* 伪类选择器:伪类选择器就是对元素处于某种状态下进行样式的。 注意: 1. a:hover 必须被置于 a:link 和 a:visited 之后 2. a:active 必须被置于 a:hover 之后 */ a:link{color:#F00} /* 没有被点击过:红色 */ a:visited{color:#0F0} /* 已经被访问过的样式:绿色 */ a:hover{color:#00F;} /* 鼠标经过的状态:蓝 */ a:active{color:#FF0;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <a href="#">百度</a> </body> </html>
应用实例(鼠标经过单元格就会变色)
<!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> </head> <style type="text/css" > table{ background-color:#CCC; border-collapse:collapse; border:3px; } tr:hover{ background-color:#06F; } </style> <body> <table border="1px" width="400px" height="300px" align="center" > <tr> <th>姓名</th> <th>成绩</th> <th>人品</th> </tr> <tr> <td>张三</td> <td>98</td> <td>差</td> </tr> <tr> <td>李四</td> <td>50</td> <td>极好</td> </tr> <tr> <td>综合测评</td> <td colspan="2">不错</td> </tr> </table> </body> </html>
常用CSS样式
背景、文本
<style type="text/css"> /*操作背景的属性 */ body{ /*background-color:#CCC; 设置背景颜色*/ background-image:url(2.jpg); background-repeat:no-repeat; /* 设置背图片是否要重复 */ background-position:370px 100px; /* 设置背景图片的位置, 第一个参数是左上角的左边距, 第二个参数是左上角的上边距 */ } /* 操作文本的样式 */ div{ color:#F00; font-size:16px; line-height:40px; letter-spacing:10px; text-align:center; text-decoration:none; text-transform:uppercase;
} </style>
表格
<style type="text/css"> table{ /*border-collapse:collapse; 合并表格的边框*/ border-spacing:20px; /* 设置单元格的边框与表格的边框距离*/ table-layout:fixed; } </style>
边框
<style type="text/css"> /* div默认是没有边框呃。*/ div{ width:100px; height:100px; border-style:dotted solid double ; /* 设置边框的样式 上 右 下 左*/ border-color:#F00; border-bottom-color:#0FF; border-top-width:100px; } </style>
盒子模型
<!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> <style type="text/css"> /* 盒子模型: 盒子模型就是把一个html边框比作成了一个盒子的边框,盒子模型要做用于操作数据与边框之间的距离或者 是边框与边框之间的距离。 盒子模型主要是用于操作内边距(padding)与外边距(margin) */ div{ border-style:solid; width:100px; height:100px; /* 内边距 */ padding-left:10px; padding-top:20px; } .one{ margin-bottom:30px; } .two{ margin-left:700px; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div class="one"> 这个是一个div </div> <div class="two"> 这个是二个div </div> </body> </html>
作业:登陆框
<!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> <style type="text/css"> .outer{ border-style:solid; width:370px; height:200px; margin-top:250px; margin-left:420px; background-image:url(../1.jpg); background-repeat:no-repeat; background-position:center; } .userName{ margin-top:60px; margin-left:80px; } .password{ margin-left:80px; margin-top:20px; } input{ border-color:#000; border-width:3px; } #button{ margin-left:120px; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div class="outer" > <div class="userName"> 用户名 <input type="text"/> </div> <div class="password"> 密 码 <input type="password"/> </div> <input id="button" type="submit" value="登陆"/> </div> </body> </html>