跟我学CSS 2 语法

今天发现了已经被翻译完的一个版本。嗯,有点失望。不过反正我还是要继续学习,所以不会就此结束我的翻译!后来仔细对照了一下,还是有些地方不一致的。我只忠于我的原文就好了。

不过也要记住,在做事之前先观察好整个大背景环境。不要做重复劳动。

今天的内容还是很简单的一些东西。我记得第一次设计页面的时候被人问到id和class的区别。找了一篇文章,来解释这个问题>>>

喜欢的站点:http://www.w3school.com.cn/index.html

原文出处:http://www.w3schools.com/css/css_syntax.asp

Syntax

语法

The CSS syntax is made up of three parts: a selector, a property and a value:

CSS语法由三部分组成:一个选择器,一个属性和一个值:

selector {property: value}

The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon, and surrounded by curly braces:

选择器一般就是你希望定义的HTML元素/标签,更改你希望更改的属性,给每个属性赋一个值。属性和值之间用冒号分割,二者由大括号包围:

body {color: black}

Note: If  the value is multiple words, put quotes around the value:

注:如果值有多个单词构成,用引号引起来:

p {font-family: "sans serif"}

Note: If you wish to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a red text color:

注:同一选择器下的多个属性由分号分割。下面的例子演示了怎样将一个段落居中并将文字设为红色:

p {text-align:center;color:red}

To make the style definitions more readable, you can describe one property on each line, like this:

为了方便阅读,可以将各个属性分行展示:

p
{
text-align: center;
color: black;
font-family: arial
}


Grouping

组合

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text color:

你可以将多个选择器组合在一起,用逗号链接。下例将所有标题元素文本定义为红色:

h1,h2,h3,h4,h5,h6 
{
color: green
}


The class Selector

选择器“类”

With the class selector you can define different styles for the same type of HTML element.

使用类选择器可以为同一类型的HTML元素定义不同的样式。

Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:

比如,你希望同一个文件中的段落有两种样式:一种居右,一种居中。你可以按照下面的方式定义:

p.right {text-align: right}
p.center {text-align: center}

You have to use the class attribute in your HTML document:

这样,你就需要用类来定义HTML元素:

<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.
</p>

Note: To apply more than one class per given element, the syntax is:

注:如需给一个元素定义多个类,可以这样写:

<p class="center bold">
This is a paragraph.
</p>

The paragraph above will be styled by the class "center" AND the class "bold".

这个段落就被同时赋予两个类,名字分别为“center”和“bold”。

You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:

如果你需要定义所有HTML元素中的某个类,需要将标签名去掉,如下例所示。此例中,所有具有“center”类厄HTML元素都会被居中显示:

.center {text-align: center}

In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector: 

在下面的例子中,h1和p都有叫做“center”的类,这就说明,h1和p都要遵守“.center”这一选择器的规则:

<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p>

Remark Do NOT start a class name with a number! It will not work in Mozilla/Firefox.
 Remark不要以数字为首命名一个类哦,这样将在火狐中失效。


Add Styles to Elements with Particular Attributes

为具有某种特定属性的元素添加样式

You can also apply styles to HTML elements with particular attributes.

一个已经具有某种属性的HTML元素仍然可以被定义样式。

The style rule below will match all input elements that have a type attribute with a value of "text":

下面的例子中,蓝色背景色的样式将对所有据有text属性值的input元素生效:

input[type="text"] {background-color: blue}


The id Selector

id选择器

You can also define styles for HTML elements with the id selector. The id selector is defined as a #.

你也可以用id选择器来定义HTML元素的样式。id选择器以“#”号开头。

The style rule below will match the element that has an id attribute with a value of "green":

下列的样式举例中,所有据有“green”id属性的元素的文本内容都将被定义为绿色。

#green {color: green}

The style rule below will match the p element that has an id with a value of "para1":

下面的样式作用于所有据有“para1”id的所有段落(p)元素:

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

Remark Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
 Remark 不要以数字开头命名ID!这样样式将在火狐中失效。

CSS Comments

CSS注释

Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:

注释用来解释你的代码,可以帮助日后对源代码进行编辑。注释是不会被浏览器识别的。CSS注释以“/*”开始,以“*/”结束:

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

Posted by yuvia

2008/07/18 16:23 2008/07/18 16:23
Response
No Trackback , No Comment
RSS :
http://2008.yuvia.org/rss/response/270

跟我学CSS 1 Introduction

嗯,最近翻译东西,心想不如翻译我想学的东西。因此开始翻译CSS教程。8过初期的东西俺都懂,只是想做一个完整的教程。

俺们要搬了,囧。搬到有很多男人的地方。。。请开始自行yy。。。
原文出处:http://www.w3schools.com/css/css_intro.asp

What You Should Already Know

Before you continue you should have some basic understanding of the following:

  • HTML / XHTML

If you want to study this subject first, find the tutorials on our Home page.

学CSS前,你得先会的东西

开始CSS学习,请先对下面的知识有初步了解:
  • HTML / XHTML

想先学HTML / XHTML,请走这边。



What is CSS?

  • CSS stands for Cascading Style Sheets
  • Styles define how to display HTML elements
  • Styles are normally stored in Style Sheets
  • Styles were added to HTML 4.0 to solve a problem
  • External Style Sheets can save you a lot of work
  • External Style Sheets are stored in CSS files
  • Multiple style definitions will cascade into one

CSS是个啥?

  • CSSCascading Style Sheets (层叠样式表)的缩写
  • 样式决定HTML元素的展示形式
  • 样式保存在样式表
  • 样式最先被引入到HTML 4.0是为了解决某问题
  • 外部样式表能帮你省不少事
  • 外部样式表保存为CSS类型文件
  • 多个样式定义均层叠为一个文件



CSS Demo

With CSS, your HTML documents can be displayed using different output styles:

See how it works

CSS Demo

一个HTML可以因为不同样式表变得不同哦:

看看范例


Styles Solve a Common Problem

HTML tags were originally designed to define the content of a document. They were supposed to say "This is a header", "This is a paragraph", "This is a table", by using tags like <h1>, <p>, <table>, and so on. The layout of the document was supposed to be taken care of by the browser, without using any formatting tags.

As the two major browsers - Netscape and Internet Explorer - continued to add new HTML tags and attributes (like the <font> tag and the color attribute) to the original HTML specification, it became more and more difficult to create Web sites where the content of HTML documents was clearly separated from the document's presentation layout.

To solve this problem, the World Wide Web Consortium (W3C) - the non profit, standard setting consortium, responsible for standardizing HTML - created STYLES in addition to HTML 4.0.  

All major browsers support Cascading Style Sheets.

样式表解决一个常见问题

HTML标签最初是为了定义文件内容制定的,例如<h1>,<p>,<table>它们表达不同的意义:“这是一个标头”,“这是一个段落”,“这是一个表格”,等等等等。如果没有格式标签,文件的页面布局由浏览器决定。

Netscape和IE作为两大主要浏览器,不断收录新的HTML标签(像<font>标签、颜色属性等)。这使得编写一个表达布局脱离HTML语言控制的网页越来越不可能了。

为了解决这个问题,负责制定HTML标准的非盈利组织W3C(万维网联盟)制定了样式这一概念。

所有主流浏览器均支持阅读层叠样式表。



Style Sheets Can Save a Lot of Work

Styles sheets define HOW HTML elements are to be displayed, just like the font tag and the color attribute in HTML 3.2. Styles are normally saved in external .css files. External style sheets enable you to change the appearance and layout of all the pages in your Web, just by editing one single CSS document!

CSS is a breakthrough in Web design because it allows developers to control the style and layout of multiple Web pages all at once. As a Web developer you can define a style for each HTML element and apply it to as many Web pages as you want. To make a global change, simply change the style, and all elements in the Web are updated automatically.

样式表省时省力

样式决定HTML元素的展示放肆,就像HTML 3.2时的字体标签和颜色属性一样,CSS一般存为外部.css格式的文件。外部样式表使你能够仅仅修改CSS文件,就轻易改变网页的外观和布局。

CSS使网页开发者能够一次性控制多个网页的样式和布局,是网页设计的一项重大突破。你可以为每项HTML元素编写一个样式,然后在多个网页中应用。小小改变一个样式,就能带动全局改变,所有的网页元素够跟着自动更新。




Multiple Styles Will Cascade Into One

Style sheets allow style information to be specified in many ways. Styles can be specified inside a single HTML element, inside the <head> element of an HTML page, or in an external CSS file. Even multiple external style sheets can be referenced inside a single HTML document. 

多个样式合为一体

样式表允许以各种方式指示信息。样式表可以嵌入单个HTML元素、可以在HTML<head>中说明,也可以作为外部样式表形式存在。多个外部样式也可以定义同一个HTML文件。

Cascading Order

What style will be used when there is more than one style specified for an HTML element?

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority:

  1. Browser default
  2. External style sheet
  3. Internal style sheet (inside the <head> tag)
  4. Inline style (inside an HTML element)

So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style declared inside the <head> tag, in an external style sheet, or in a browser (a default value).

层叠顺序

一个HTML元素中被多个样式定义时,哪个被优先使用呢?

一般来说,所有的样式都被层叠起来,形成一个“虚拟的”新样式表。各项样式按照下列顺序排列,优先级由小到大。

1. 浏览器默认样式
2. 外部样式表
3. 内部样式表(定义在<head>标签部分)
4. 内嵌样式(在HTML元素内部定义)

所以,内嵌样式(在HTML元素内部定义)具有最高优先级,可以覆盖内部样式表、外部样式表和浏览器(默认值)中的样式定义。

Posted by yuvia

2008/07/17 16:20 2008/07/17 16:20
Response
No Trackback , a comment
RSS :
http://2008.yuvia.org/rss/response/269


블로그 이미지

我和我的一二三四天

- yuvia

Notices

Archives

Authors

  1. yuvia

Calendar

«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Site Stats

Total hits:
111947
Today:
78
Yesterday:
209