<span style="font-size:30px;color:blue;">栏目一</span> <br> <span style="font-size:10px;color:red;font-style:italic;">栏目二</span> <br> <span style="font-size:40px;color:pink;font-weight: bold;">栏目三</span> <br> <span style="font-size:20px;color:green;font-weight:lighter;">栏目四</span> <br> <span>栏目五</span>
<style type="text/css"> .spanStyle { font-size: 20px; /*字体大小*/ color: red; /*字体颜色*/ font-weight: bold; /*字体粗细*/ font-style: italic; /*文本,正常normal,倾斜italic*/ text-decoration: underline; /*给文本加下划线*/ } </style> <body> <span class="spanStyle">栏目一</span><br> <span class="spanStyle">栏目二</span><br> <span class="spanStyle">栏目三</span><br> <span class="spanStyle">栏目四</span><br> <span class="spanStyle">栏目五</span><br> </body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css2.html</title> <meta http-equiv="keywords" content="value1,value2,..."> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <style type="text/css"> /*使用滤镜 将彩图变为黑白*/ .imgStyle { filter: gray; /*注:此种滤镜方式仅IE支持*/ } a:link img { filter: gray; } /*当鼠标移动到该图片上时变为彩色*/ a:hover img { filter: ""; } </style> </head> <body> <img src="1.jpg" class="imgStyle"/> <img src="2.jpg" class="imgStyle"/> <a href="#"><img src="3.jpg" /></a> </body> </html>
<元素名 class="类选择器名">
注意:
可以通过 " 元素名.类选择器名 " 的方式限定类选择器的使用范围
如, p.cls{ color:red; } 类选择器".cls"只作用于 元素p
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css3.html</title> <meta http-equiv="keywords" content="value1,value2,..."> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <style type="text/css"> /*类选择器*/ .newsStyle { font-size: 20px; background: blue; } </style> </head> <body> <span class="newsStyle">新闻一</span> <span class="newsStyle">新闻二</span> <span class="newsStyle">新闻三</span> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css3.html</title> <meta http-equiv="keywords" content="value1,value2,..."> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <style type="text/css"> /*id选择器*/ #newsId { font-size: 50px; background: silver; } </style> </head> <body> <span id="newsId">这是一则非常重要的新闻</span> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css3.html</title> <meta http-equiv="keywords" content="value1,value2,..."> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <style type="text/css"> /*元素选择器*/ a { color: black; /*字体黑色*/ font-size: 24px; /*字体大小24px*/ text-decoration: none; /*不出现下划线*/ } /*当鼠标移动到超链接上时,自动出现下划线并变大,移开后又消失*/ a:hover { text-decoration: underline; font-size: 34px; } /*点击以后超链接变为红色*/ a:visited { color: red; } </style> </head> <body> <a href="#">百度</a> <a href="#">新浪</a> <a href="#">搜狐</a> <a href="#">腾讯</a> </body> </html>
ID > class > element
<!-- 文档类型,用于指定DTD(说明当前这个HTML版本) --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css1.html</title> <!-- 给搜索引擎看的 --> <meta http-equiv="keywords" content="value1,value2,..."> <!-- 告诉浏览器以什么编码来解析该HTML文件 --> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <!--引入CSS文件 <link rel="stylesheet" type="text/css" href=""> --> </head> <style type="text/css"> .spanStyle { font-size: 20px; /*字体大小*/ color: red; /*字体颜色*/ font-weight: bold; /*字体粗细*/ font-style: italic; /*文本,正常normal,倾斜italic*/ text-decoration: underline; /*给文本加下划线*/ } </style> <body> <span style="font-size:30px;color:blue;">栏目一</span> <br> <span style="font-size:10px;color:red;font-style:italic;">栏目二</span> <br> <span style="font-size:40px;color:pink;font-weight: bold;">栏目三</span> <br> <span style="font-size:20px;color:green;font-weight:lighter;">栏目四</span> <br> <span>栏目五</span> <hr> <span class="spanStyle">栏目一</span><br> <span class="spanStyle">栏目二</span><br> <span class="spanStyle">栏目三</span><br> <span class="spanStyle">栏目四</span><br> <span class="spanStyle">栏目五</span><br> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css2.html</title> <meta http-equiv="keywords" content="value1,value2,..."> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <style type="text/css"> /*使用滤镜 将彩图变为黑白*/ .imgStyle { filter: gray; /*注:此种滤镜方式仅IE支持*/ } a:link img { filter: gray; } /*当鼠标移动到该图片上时变为彩色*/ a:hover img { filter: ""; } </style> </head> <body> <img src="1.jpg" class="imgStyle"/> <img src="2.jpg" class="imgStyle"/> <a href="#"><img src="3.jpg" /></a> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>css3.html</title> <meta http-equiv="keywords" content="value1,value2,..."> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <style type="text/css"> /*类选择器*/ .newsStyle { font-size: 20px; background: blue; } /*id选择器*/ #newsId { font-size: 50px; background: silver; } /*元素选择器*/ a { color: black; /*字体黑色*/ font-size: 24px; /*字体大小24px*/ text-decoration: none; /*不出现下划线*/ } /*当鼠标移动到超链接上时,自动出现下划线并变大,移开后又消失*/ a:hover { text-decoration: underline; font-size: 34px; } /*点击以后超链接变为红色*/ a:visited { color: red; } </style> </head> <body> <span class="newsStyle">新闻一</span> <span class="newsStyle">新闻二</span> <span class="newsStyle">新闻三</span> <br/><hr/> <span id="newsId">这是一则非常重要的新闻</span> <br/><hr/> <a href="#">百度</a> <a href="#">新浪</a> <a href="#">搜狐</a> <a href="#">腾讯</a> </body> </html>