css 入门基础教程

所属分类: 网页制作 / CSS 阅读数: 1849
收藏 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>
更多精彩内容其他人还在看

CSS入门教程:计算CSS盒模型宽和高

 出处:当我们布局一个网页的时候,经常会遇到这样的一种情况,那就是最终网页成型的宽度或是高度会超出我们预先的计算,其实就就是所谓的CSS的盒模型造成的。 #test{margin:10px;padding:10px;width:100px;height:100px;}
收藏 0 赞 0 分享

在IE流览器中正确显示PNG透明图片

  png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!   但目前IE中对于插入
收藏 0 赞 0 分享

CSS教程:DIV底部放置文字

  css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近的距离还可以精确到像素,自己可以调节,是不是很不错呢?
收藏 0 赞 0 分享

如何用CSS让文字居于div的底部

  这个问题是别人提出的,因为css对文字的布局上没有靠容器底部对齐的参数,(或许有但是我没有发现)不过目前我使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,我用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近
收藏 0 赞 0 分享

从A页面连接到B页面后并直接把B页面的隐藏层显示

  这个效果实现的是,在B页面里有两个层,一个显示层,我们暂定名c层,一个是隐藏层,我们暂定名d层,单独进B页面的时候,c层显示,d层隐藏,然而从A页面连接到B页面的时候,则是让d层显示,c层隐藏,我觉得这个效果对网页设计者以后会有很大帮助,现在把代码发出来,
收藏 0 赞 0 分享

CSS样式表定义标签li前面样式

定义LI前面的小点样式 view plaincopy to clipboardprint? 语法: list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lowe
收藏 0 赞 0 分享

符合标准的div css制作的弹出菜单

本文介绍了五款符合标准的div css制作的弹出菜单,而且不含有js的. NO.1最基本的:二级dropdown弹出菜单 <!DOCTYPE html PUB
收藏 0 赞 0 分享

CSS实现在文章每段后面加入带连接的隐藏文字

代码主要理解3个参数:createElement、createTextNode、appendChild。这3个js参数分别是创建元素、创建字符、追加节点。代码原理:循环页面段落标签<p>,创建连接元素<a>,创建要显示的连接字符,用SetAttribute
收藏 0 赞 0 分享

CSS:浏览器特定选择器介绍

当你想在一个浏览器里改变样式而不像在其他浏览器中改变时,这些选择器很有用。 IE6以下 *html{} IE 7 以下 *:first-child html {} * html {} 只对IE 7 *:first-child html {} 只对IE 7
收藏 0 赞 0 分享

WEB标准学习,认识两种网页声明的含义

即网页标准推出来以后,我们时常会看到两种不同的网页的声明,一个是Dhtml,一个是Xhtml。如下所示: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
收藏 0 赞 0 分享
查看更多