当前位置: 首页 > 图文教程 > 网页制作 > CSS样式表 > css 入门基础教程

CSS样式表
CSS教程:闭合CSS浮动元素的几种方法
细说CSS3中的选择符
网页超级链接该以什么方式打开?
背投广告设计:用最少的时间来做最效率的事情
css中用javascript判断浏览器版本
css教程:经常保持CSS的整洁度和有序性
css教程:选择合适的、有意义的元素描述内容
CSS编写中灵活运行注释的意义
css教程:CSS文件应该保持整洁和统一
DIV CSS常用的网页布局代码
完全掌握纯CSS布局网页
serif和sans serif:西方国家的字母体系
基于XTHML标准的DIVCSS布局对SEO的影响
CSS合理的编码与组织技巧
网站变黑白灰色的4种代码详细讲解
css布局实例:网页布局的方法
YUI 中的 Grids CSS值得关注和学习的
CSS代码是否合理?是否优化?
css教程:网页中Span和Div的区别
div css网页适应不同分辨率技巧

CSS样式表 中的 css 入门基础教程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-20   浏览: 136 ::
收藏到网摘: n/a

一、CSS语法:
h1 {color:blue;font-size:12px;}
h1就是HTML的标签,又叫选被器,是被选择样式化的对象,color:blue;就是效果一,font-size:12px;效果二
二、来个例子:
p {color:red;text-align:center}
或者这样,更方便读取
p
{
color:red;
text-align:center;
}
放到HTML里的代码就是:

复制代码
代码如下:

<html>
<head>
<style type="text/css">
p
{
color:red;
text-align:center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
</body>
</html>

三、CSS注释用/* 这里是被注释的内容 */

复制代码
代码如下:

/*This is a comment*/
p
{
text-align:center;
/*This is another comment*/
color:black;
font-family:arial
}

四、CSS的id和Class选择器来控制样式
id可以用作样式化一个单独的对象,比如我们要对某一个P元素做个效果,而其他的默认效果。
id在CSS中的定义方式就是:

复制代码
代码如下:

#para1
{
text-align:center;
color:red
}

这里的para1就是HTML文件中某一个元素的id,看个例子

复制代码
代码如下:

<html>
<head>
<style type="text/css">
#para1
{
text-align:center;
color:red
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>

看效果图,只有id为para1的P元素有效果。
Class也就是类选择器,可以表示一组元素的样式,id只对一个有效
[code]
<html>
<style>
.cen {text-align: center}
</style>
<body>
<h1 class="cen">
This heading will be center-aligned
</h1>
<p class="cen">
This paragraph will also be center-aligned.
</p>
</body>
</html>

这样h1和p的内容都会自动居中了


一、外部样式表:适用于很多页面都需要用到样式表时
<head><link rel="stylesheet" type="text/css" href="mystyle.css" /></head>
用文本编辑器进行编辑,里面不能包含任何HTML的标签并以CSS为后缀保存就是一个样式表文件,如下内容

复制代码
代码如下:

<PRE class=brush:css>hr {color: sienna;}
p {margin-left: 20px;}
body {background-image: url("images/back40.gif");}
</PRE>

需要注意的是在属性值和单位间不要有空间,否则只有IE6有效,在Mozilla/Firefox/Netscape中无效。
应该用
<PRE class=brush:css>p {margin-left: 20px;} 而不是 p {margin-left: 20 px;}
</PRE>
二、内部样式表:
当某个文档需要特殊样式时,就可以用内部样式表,可以用<style>包在里面

复制代码
代码如下:

<head>
<style type="text/css">
hr {color: sienna;}
p {margin-left: 20px;}
body {background-image: url("images/back40.gif");}
</style>
</head>

三、内联样式表
只对当前元素使用,只要在相关标签内使用样式(style),可以包含CSS属性

复制代码
代码如下:

<p style="color: sienna; margin-left: 20px">
This is a paragraph
</p>

四、多重样式表
某些属性在不同的样式表中被同样的选择器定义,那么属性值将从更具体的样式表中被继承过来。
如,外部样式表针对h3选择器的三个属性

复制代码
代码如下:

h3 {
color: red;
text-align: left;
font-size: 8pt;
}

而外部样式表拥有针对h3选择器的两个属性

复制代码
代码如下:

h3 {
text-align: right;
font-size: 20pt;
}

假如拥有样式表的这个页面同时与外部样式表链接,那么h3得到的样式是:

复制代码
代码如下:

color: red;
text-align: right;
font-size: 20pt;



一、CSS背景控制
可以控制元素的的背景,大概有以下属性
view sourceprint?1 background-color
background-image
background-repeat
background-attachment
background-position
.控制背景色
例如:

复制代码
代码如下:

<html>
<head>
<style type="text/css">
body
{
background-color:#b0c4de;
}
</style>
</head>
<body>
<h1>My CSS web page!</h1>
<p>Hello world! This is a W3Schools.com example.</p>
</body>
</html>


颜色还可以用其他方式表示,如:"red","rgb(255,0,0)","#ff0000"

复制代码
代码如下:

<html>
<head>
<style type="text/css">
h1
{
background-color:#6495ed;
}
p
{
background-color:#e0ffff;
}
div
{
background-color:#b0c4de;
}
</style>
</head>
<body>
<h1>CSS background-color example!</h1>
<div>
This is a text inside a div element.
<p>This paragraph has it's own background color.</p>
We are still in the div element.
</div>
</body>
</html>

.控制背景图

复制代码
代码如下:

<html>
<head>
<style type="text/css">
body {background-image:url('paper.gif')}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>


.背景重复(如何需要在页面上对背景图像进行平铺,可以使用background-repeat属性)
普通效果

复制代码
代码如下:

<html>
<head>
<style type="text/css">
body
{
background-image:url('gradient2.png');
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

平铺效果

复制代码
代码如下:

<html>
<head>
<style type="text/css">
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

4.背景定位
可以用back-ground-position属性改变图像在背景中的位置。

复制代码
代码如下:

<html>
<head>
<style type="text/css">
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background image example.</p>
<p>The background image is only showing once, but it is disturbing the reader!</p>
</body>
</html>

只显示一张图片,如果 去除 background-repeat:no-repeat;<BR>就显示多张图片。
图片置顶,靠右

复制代码
代码如下:

<html>
<head>
<style type="text/css">
body
{
background:#ffffff url('img_tree.png') no-repeat top right;
margin-right:200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Now the background image is only show once, and positioned away from the text.</p>
<p>In this example we have also added a margin on the right side, so the background image will never disturb the text.</p>
</body>
</html>