css 入门基础教程

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

2013年五大主流浏览器 HTML5 与 CSS3 兼容性大比拼

这篇文章主要介绍了2013年五大主流浏览器 HTML5 和 CSS3 兼容性大比拼,需要的朋友可以参考下
收藏 0 赞 0 分享

前端设计师需要了解的9个问题

这篇文章主要介绍了前端设计师需要了解的9个问题以及注意事项,非常的实用,是篇非常不错的文章,这里推荐给大家
收藏 0 赞 0 分享

CSS强制性换行的方法区别详解

自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法对于div,p等块级元素正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行
收藏 0 赞 0 分享

CSS语义化命名方式及常用命名规则

本文搜集了一些CSS语义化命名方式以及命名规则。如有错误或不妥之处,敬请指出,欢迎你提出更好的建议,加插更多的命名规范。
收藏 0 赞 0 分享

ul li内容宽度的问题的解决方案

在IE6下只要我设置LI的宽度为auto,不论LI里面的内容多长,LI的宽度始终是100%,即UL的宽度(这里我已经写死了UL的宽度)。设置max-width也没用。高手们,在IE系列浏览器中如何使LI的宽度真正的随着内容增长而增长呢?像FF那样。
收藏 0 赞 0 分享

html5+css3气泡组件的实现

本文是html5+css3系列教程的第一篇,给大家讲述html5+css3实现气泡组件,讲解的十分的详细,这里推荐给大家,希望对大家能有所帮助
收藏 0 赞 0 分享

html5+css3之CSS中的布局与Header的实现

本文从CSS3的布局(CSS的布局的演化、CSS3盒模型-box-sizing、float布局中的bfc、Flexbox简介)Header布局的实现(float实现布局、Header js的实现)向我们展示了HTML5与CSS3的魅力。
收藏 0 赞 0 分享

一款恶搞头像特效的制作过程 利用css3和jquery

今天给大家介绍一款恶搞头像特效的制作过程,你可以把任意一张照片放到跳舞的漫画中,为他带上不同的帽子,让他翩翩起舞,下面我们一起来看一下制作过程和效果
收藏 0 赞 0 分享

大图片根据分辨率自适应宽度仍居中显示

一个1920*900的大图,在1024*768的分辨率下仍居中显示,如何做到这一点,下面是一个可行的解决方案
收藏 0 赞 0 分享

CSS3提交意见输入框样式代码

这个提交意见输入框,结构使用到了table,样式用的css3,包含了多方面的知识,比较适合新手朋友们
收藏 0 赞 0 分享
查看更多