什么是CSS样式表
CSS是Cascading Style Sheet的缩写。译作「层叠样式表单」。是用于控制(增强)网页样式并允许将样式信息与网页内容分离的一种标记性语言。
如何将样式表载入您的网页
你可以用以下几种方式将样式表加入您的网页,而最接近目标的样式定义优先权越高,高优先权样式将继承低优先权样式的未重叠定义但覆盖重叠的定义。
载入外部样式表文件 (Linking to a Style Sheet)
你可以先建立外部样式表文件(.css),然后使用HTML的link对象。示例如下:
<head> <title>麟妖软圃</title> <link rel=stylesheet href="../common/example.css" type="text/css"> </head>而在XML中,你应该如下例所示在声明区中加入:
<? xml-stylesheet type="text/css" href="../common/example.css" ?>
你可以在你的HTML文档的<html>和<body>标记之间插入一个<style>...</style>块对象,定义方式请参阅样式表语法。示例如下:
<html> <style type="text/css"> <!-- body {font: 10pt "Arial"} h1 {font: 15pt/17pt "Arial"; font-weight: bold; color: maroon} h2 {font: 13pt/15pt "Arial"; font-weight: bold; color: blue} p {font: 10pt/12pt "Arial"; color: black} --> </style> <body>请注意,这里将 style对象的 type属性设置为" text/css",是允许不支持这类型的浏览器忽略样式表单
内联定义即是在对象的标记内使用对象的style属性定义适用其的样式表属性。示例如下:
<td style="margin-left: 0.5in; margin-right:0.5in">这种用法的优点是可灵巧应用样式於各标签中,但是缺点则是没有整篇文件的『 统一性』
例如:
<span style="font:12px/20px 宋体 #000000;">麟妖軟圃</span>
跟LINK用法很像,但必放在<style>...</style>中
<STYLE TYPE="text/css"> <!-- @import url(引入的的位址、路径与档名); --> </STYLE>例如:
<STYLE TYPE="text/css"> <!-- @import url(../common/example.css); --> </STYLE>要注意的是,行末的分号是绝对不可少的!
Selector { property: value; property: value }参数说明:
Selector -- 选择符
property : value; -- 样式表定义。 属性和属性值之间用冒号 : 隔开,定义之间用分号 ; 隔开
每个属性都有一个指定的值:Inherit
它的意思是:将父对象的值等同为计算机值得到,这个值通常仅仅是备用的,显式的声明它可用来强调。
CSS基本属性
CSS基本定义示例
body,td,th { font-family: 宋体; font-size: 12px; color: #000000; } body { background:url(../images/bg.jpg); margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; } a { font-size: 12px; color: #990000; } a:link { text-decoration: none; } a:visited { text-decoration: none; color: #990000; } a:hover { text-decoration: none; color: #FF7C3E; position:relative; top:1px; left:2px; } a:active { text-decoration: none; color: #006600; } .codefont { color: #FFFFFF; font-family: 宋体; font-size: 12px; } .tagfont { color: #CC6633; font-family: 宋体; font-size: 12px; font-weight: bold; } .bar_cn { font-size:12px; color:#000000; font-family: Arial; position: relative; filter: blur(add=1, direction=45, strength=3); cursor:default; }
代码说明 | ||
a:link | CSS1 IE4+ 、 NS4+ 设置a对象在未被访问前的样式表属性 | |
a:hover | CSS1/CSS2 IE4+ 、 NS4+ 设置对象在其鼠标悬停时的样式表属性 | |
a:active | CSS1/CSS2 IE4+ 设置对象在被用户激活(在鼠标点击与释放之间发生的事件)时的样式表属性 | |
a:visited | CSS1 IE4+ 、 NS4+ 设置a对象在其链接地址已被访问过时的样式表属性 |
代码内最后的.codefont {}、.tagfont {}、.bar_cn {}代码段为自定义的样式属性
在页面中文本使用示例
<span class="codefont">这里所显示的是.codefont自定义的样式</span>在表格中使用示例
<td cliass='tagfont'>这里所显示的是.tagfont自定义的样式</td>在表格中使用示例
<table class="bar_cn" width="380" border="0" cellspacing="0" cellpadding="0" style="border:1px solid #000000"> </tr> <td cliass='bar_cn'>这里所显示的是.bar_cn自定义的样式</td> </tr> </table>
效果演示: | 这里所显示的是.bar_cn自定义的样式 |
在同一页面内控制不同链接的CSS属性
在Dreamweave的CSS中定义一个新的标示,按照HTML的语法,CSS中定义超级连接语法示例如下
/* img样式方案 */ a.img { font-size: 12px; color: #990000; } a.img:link { text-decoration: none; } a.img:visited { text-decoration: none; color: #990000; } a.img:hover { text-decoration: none; color: #FF7C3E; position:relative; top:0px; left:0px; } a.img:active { text-decoration: none; color: #006600; }
示例中img为自定义的方案名,然后在页面使用中选中某个连接后,在CSS面版中点中img即可。
要注意的是如果在img中未定义的某条样式而a中有定义、则会继承a的样式,假设img中未曾定义a.img:hover{}中的位置偏移、那么将会采a用中的偏移样式、所以要在img中到达不偏移的效果则需加上top:0px; left:0px;
按需要,你可以定义N个标示,N种鼠标over的效果。
CSS基本示例
自定义的按钮
.mybutton { background-color: #FFCC99; /*按钮底背景色*/ border-bottom: #FF6633 1px solid; /*按钮底边框颜色*/ border-left: #FF9966 1px solid; /*按钮左边框颜色*/ border-right: #FF6633 1px solid; /*按右底边框颜色*/ border-top: #FF9966 1px solid; /*按钮顶边框颜色*/ color: #666666; /*按钮文字颜色*/ height: 30px; /*按钮高度*/ }
效果演示: 颜色可自行修改
自定义的立体字
.myfont{ width: 220; color: #000000; filter: DropShadow(Color=#FFCC99, OffX=1, OffY=1, Positive=2); font-family: "宋体"; font-size: 12px; text-decoration: none; text-align: inherit; }
效果演示: | 这是自定义的文本样式 |
虚线表格边框
.mode_table_quote{ padding: 3px 10px; margin: 5px 10px; border: 1px dotted #FF0000; }
效果演示:
这是虚线表格边框的效果 |