css 入门基础教程

所属分类: 网页制作 / CSS 阅读数: 1856
收藏 0 赞 0 分享
一、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只对一个有效

复制代码
代码如下:

<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为后缀保存就是一个样式表文件,如下内容
[code]
<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>
更多精彩内容其他人还在看

Opera中国的WEB标准课程

网页制作Webjx文章简介:在这篇文章里,我要向大家介绍我和其他很多人花费数月时间开发的一个课程——Web标准课程,该课程旨在向大家提供Web设计和开发的坚实基础,无论读者是谁,此教程完全免费、可访问,并且不需要预备知识。当然,我主要还
收藏 0 赞 0 分享

CSS样式表渐进增强的基本概念

网页制作Webjx文章简介:如果你挠着头想弄清楚优雅降级和渐进增强的区别,我告诉你,这是视角问题。优雅降级和渐进增强都考虑网站在各种设备的各种浏览器上如何良好运转。两者区别的关键在于它们各自关注的焦点,以及这种关注对工作流程的影响
收藏 0 赞 0 分享

简单介绍Web Developer插件制作网页

网页制作Webjx文章简介:Firefox浏览器是一个良好支持W3C标准的开放源代码的浏览器,拥有Linux/Windows/Mac版本。因为Firefox浏览器良好支持W3C标准,所以使用Firefox来调试网页是非常好的。 Firefox浏览器是
收藏 0 赞 0 分享

CSS布局带来的巨大影响:CSS display属性值

网页制作Webjx文章简介:网页元素应用上那些与表格相关的display属性值后,能够模仿出与表格相同的特性。我将会在该文中给大家演示这种方法给CSS布局带来的巨大影响。 应原书编辑要求,先在文章顶部给出链接:《Everything You
收藏 0 赞 0 分享

用div css模拟表格对角线

这只是探讨一种CSS模拟表格对角线的用法,实际在工作中可能觉得这样做有点小题大作,这不是本主题讨论的重点。如果对此深以为然的朋友,请一笑过之 首先声明: 这只是探讨一种CSS模拟表格对角线的
收藏 0 赞 0 分享

IE Firefox在css中的差别 (部分)

1、单位问题 问题:任何距离的数值ie可以不加单位,ff必须要求写单位(0除外) 解决:写全单位如padding:0px; 2、水平居中 问题:div里的内容,ie默认为center,而ff默认left 解决:mairgin:0px auto; 3、高度问题
收藏 0 赞 0 分享

不用js可以实现信息提示效果

[code] <style> body { font:verdena; font-size:14px; color:#000 } h1{ font:verdena; font-size:22px; color:#000 } h2{ font:verdena;
收藏 0 赞 0 分享

CSS解决未知高度的垂直水平居中自适应问题

今天有人问起,晚上试着写出来,供参考; 以下代码兼容主流浏览器IE6、IE7、Firefox、Opera。 从最简单的开始………… 一、如何让一个DIV水平居中? 这个简单不作过多说明! [code] <st
收藏 0 赞 0 分享

CSS cursor 属性 -- 鼠标指针样式效果

取值: [ [<uri> ,]* [ auto | crosshair | default | pointer | move | e-resize | ne-resize | nw-resize | n-resize | se-resize | sw-resize |
收藏 0 赞 0 分享

css 简单区别ie6,ie7,firefox的写法

同一样式里可以这样 [code] margin:17px; FF +margin:17px; IE6 IE7 _margin:17px; IE6 [/code] 按这个顺序,刚好区分开三个浏览器
收藏 0 赞 0 分享
查看更多