自從W3C(The World Wide Web Consortium)制定了代號為Cougar的HTML的4.0版本以來,存在在Web頁面中的動態效果首次被正式的承認了,W3C把動態超文本(Dynamic HTML)的實現分為了三個部分:腳本、支援動態效果的瀏覽器和CSS。前兩者也許你常用到,但CSS是什麼或許你並不清楚,如果有過製作Web頁面的經歷,你可能聽說過樣式單或風格單,如果對此你亦無耳聞,不要緊,只要你想製作出具有更多新功能的W eb頁,本文將同樣適合你。
一、什麼是CSS?
CSS是Cascading Style Sheet的縮寫,有些書上把它譯為「層疊樣式單」或「級聯樣式單」(下文簡稱「樣式單」),在1997年W3C頒布HTML4標準的同時也公佈了有關樣式單的第一個標準CSS1。樣式單是對以前的HTML(3.2以前的H TML版本)語法的一次重大革新,以前的HTML版本中,各種功能的實現是透過標記元素實現的,這也造成了各個瀏覽器廠商為了標新立意創造各種只有自家支持的標記,各種標記互相嵌套,就可以達到不同的效果,例如要在一段文字中把一部分文字變成紅色,H TML3.2中應該是這樣的:
這裡顯示紅色字
這裡顯示紅色字
tags on the page.
An external style sheet is a style sheet file saved externally. The external file has a .CSS extension, such as "".
You can use the above three methods as needed during application, but in practice, inline style sheets and embedded style sheets are used more often.
3. Grammatical features of style sheets
Style sheets have their own unique writing methods. If you master its grammatical features and understand its various attributes, you will find that using style sheets in Web pages will be How relaxing. For example, there is the simplest HTML document:
Text goes here…
us Styles can be specified using embedded stylesheets.
利用和🎜>利用🎜>ID顯示內容。
)才執行樣式單,同樣的也適合於ID。
為了簡化宣告某些重複屬性的標記,可以用「,」把不同的選擇器隔開,表示它們都表示成相同的屬性,如:
H1,H2 {color:red}
Div, p.mytext {……}
有時我們也希望能夠在特定的範圍內使樣式單生效:
p em {color:red}
元素標記間又空格隔開,表示表示在
和
The and between are shown in red. In addition, you can also use "~" to indicate that one selector is followed by another selector, and it is surrounded by "/" on both sides:
Some sample text that has no other...
相反的,使用clear属性将禁止元素在BOX的左方或右方飘浮:
属性名称: 'clear'
属性值: left| right|both|none
初始值: none
适合对象: 所有元素
是否继承: no
百分比备注: 被禁止
绝对位置属性:
绝对位置属性有四个属性:top、right、bottom和left,属性值为长度单位或百分数:
属性名称: 'top'、'right'、'bottom'、'left'
属性值:
初始值: none
适合对象: 所有元素
是否继承: no
百分比备注: 被禁止
利用以上属性,用户就可以精确定义元素的位置,如:
I used two red hyphens to serve as a change bar. They
will "float" to the left of the line containing THIS
--
word.
z-index屬性:
在CSS中允許元素的重疊顯示,這樣就有一個顯示順序的問題,z-index屬性描述了元素的前後位置,如果把電腦螢幕看作X-Y平面的話,那麼Z軸就是垂直於螢幕的,z-index使用整數表示元素的前後位置,數值越大,就會顯示在相對前方的位置,並且C SS同意在z-index中使用負數。
屬性名稱: 'z-index'
屬性值: auto|
初始值: auto
適合物件: 使用position屬性的元素百分比備註: 被禁止
width屬性:
規定BOX的width屬性,可以使BOX的寬度不依賴它所包含的內容的多少:
屬性名稱: 'width':
屬性值
屬性值 |
適合對象: 塊元素
是否繼承: no
SS也提供了min-width和max-width屬性,使得BOX的寬度在最小寬度和最大寬度之間。
屬性名稱: 'min-width'
屬性值:
|
屬性名稱: 'max-width'
屬性值:
|
初始值: 10%
初始值: 10%
是否繼承: no
相同的BOX還有height屬性控製本身的高度:
屬性名稱: 'height' >屬性值:
|
| auto
初始值: auto
適合對象: 塊元素
是否繼承:1no >在CSS中也提供了min-height和max-height屬性,使得BOX的高度在最小高度和最大高度之間。
屬性名稱: 'min-height'
初始值: 0 適合對象附註:依父元素的height而定
屬性名稱: 'max-height'
屬性值:
|
初始值:是否繼承: no
在規定元素的寬度和高度時,如果元素的面積不足以顯示全部內容的話就要用到overflow屬性:
屬性名稱: 'overflow'
屬性值: visible | hidden | scroll | auto
初始值: visible
適合對象: 元素的position屬性
是否繼承: no
visible:擴大面積以顯示所有內容。
hidden:隱藏超出範圍的內容。
scroll:在元素的右邊顯示一個捲軸。
auto:當內容超出元素面積時,顯示捲軸。
clip屬性:
CSS也提供了一種clip屬性,可以把元素區域剪切成各種形狀,但目前提供的只有方形一種:
屬性名稱: 'clip'
屬性值:
| auto
初始值: auto
適合元素: 元素的position屬性設為absolute
值為rect(top right bottom left)。
line-height and vertical-align attributes:
The line-height attribute can specify the line spacing inside the element, using length units or percentages:
Attribute name: 'line-height'
Attribute value: normal |
DIV { line-height: 1.2em; font-size: 10pt }
DIV { line-height: 120%; font-size: 10pt }
The vertical-align attribute determines the display of the element in the vertical position:
Attribute name: 'vertical-align'
Attribute value: baseline | sub | super | top | text-top | middle | bottom | text-bottom |
|
Initial value: baseline
Suitable object: inline elements
Suitable for inheritance: no
Percentage Note: Depends on the line-height attribute of the element.
middle: Aligned to the middle of the element.
sub: The word sinks.
super: word rises.
text-top: Align the text at the top.
text-bottom: Text bottom alignment.
Top: Aligned with the highest element in this row.
Bottom: Aligned with the lowest element in this row.
Visibility attribute:
This attribute is used to control the display or hiding of elements:
Attribute name: 'visibility'
Attribute value: inherit | visible | hidden
Initial value: inherit
Suitable objects: All elements
Whether to inherit: If the value is inherit, the parent element attributes are inherited
Percentage Note: Prohibited
4. Color and Background (Color and Background) attributes:
Introduced here About how to set the foreground color, background color and picture in CSS.
color attribute:
color attribute is used to set the foreground color of the element:
Attribute name: 'color'
Attribute value:
Initial value: based on the user's initial value Defined
Suitable objects: All elements
Whether to inherit: yes
Percentage Note: Prohibited
The value of the color attribute can be a hexadecimal value, the rgb() function or a color name recognized by CSS. For example:
EM { color: rgb(255,0,0)}
Background attribute:
background-color attribute is used to set the background color. The initial value is Transparent:
Attribute name: 'background-color'
Attribute value:
| transparent
Initial value: transparent
Suitable object: all elements
Whether to inherit: no
Percentage Note: Forbidden
The backgroud-image attribute is used to set the background image:
Attribute value:
| none
Initial value: none
Suitable objects: All elements
Inherited or not: no
Percentage remarks: Prohibited
The url can be an absolute address or a relative address, for example:
P { background-image: none }
The above two attributes can also be achieved using ordinary HTML attributes. The following attributes are CSS extensions to the original HTML.
The background-repeat attribute is used to describe the repeated arrangement of background images:
Attribute name: 'background-repeat'
Attribute value: repeat | repeat-x | repeat-y | no-repeat
Initial value: repeat
Suitable objects: all elements
Whether to inherit: no
Percentage remarks: prohibited
The meaning of the attribute value is:
repeat: two along the X axis and the Y axis The direction repeats the image.
repeat-x: Repeat the image along the X-axis direction.
repeat-y: Repeat the image along the Y-axis direction.
none: No duplicate images.
例如:
BODY {
background: red url(pendant.gif);
background-repeat: repeat-y;
「在軸上重複圖片pendant.gif”,其餘部分以紅色為背景色*/
background-attachment屬性表示在滾動整個文件時,背景圖片的顯示方式。它的屬性值有兩種:fixed和scroll,fixed相當於IE4裡的水印效果,也就是說在拖曳文件時,背景相對是靜止的,scroll則和文檔一起滾動。
background-position屬性用來指定背景圖片顯示的位置:
屬性名稱: 'background-position'
屬性值: [
|
適合對象: 容器元素
是否繼承: no
百分比備註: refer to the size of the element itself
其中屬性值意義為:
「top left」與「left top」表示「0% 0%」。
「top」、「top center」和「center top」表示「50% 0%」。
「right top」和「top right」都表示「100% 0%」。
「left」、「left center」和「center left」表示「0% 50%」。
「center」和「center center」表示「50% 50%」。
「right」、「right center」和「center right」都表示「100% 50%」。
「bottom left」和「left bottom」表示「0% 100%」。
「bottom」、「bottom center」和「center bottom」都表示「50% 100%」
「bottom right」和「right bottom」表示「100% 100%」。
例如:
BODY { background: url(banner.jpeg) right top } /* 100% 0% */ BODY { background: url(banner.jpeg) center } /* 50% 50% */
BODY {00%: url(banner.jpeg 🎜>background屬性是上述背景屬性的捷徑,屬性與順序如下:
屬性名稱: 'background'
屬性值:
| |
| | | 適合對象: 所有元素 是否繼承: no 百分比備註: 只在background-position中容許使用 5.字體(Font)屬性:
這裡定義了關於字體的各種屬性。
font-family屬性定義字體的名稱,可以是一個字體的名稱,也可以是一類字體的名稱,字體的名稱一定要跟電腦系統裡的完全一樣:
屬性名稱: 'font- family'
屬性值: [[
|
],]* [
|
]
初始值: |
]
有些電腦系統中如果沒有樣式單要求的字體,可以再設一個次字體以備萬一。例如:
BODY { font-family: Baskerville, "Heisi Mincho W3", Symbol, serif }
family-name是指確定的某一字體,如Heis-3Mgeneoo如serif。
font-style屬性描述字體的傾斜程度:
屬性名稱: 'font-style'
屬性值: normal | italic | oblique 初始值: italic | oblique
是否繼承: yes
百分比備註: 被禁止
font-variant屬性:
屬性名稱: 'font-variant' 適合對象: 所有元素
是否繼承: yes
百分比備註: 被禁止
一個使用了small-caps屬性的元素中的小寫字母看起來比正常的大寫字母看起來小字母的小字母。一些。
font-weight屬性用來描述字重。
屬性名: 'font-weight'
屬性值: normal | bold | bolder | lighter | 100 | 200 300 | 100 | 200 300 | 100 | 200 300 900
初始值: normal
適合對象: 所有元素
是否繼承: yes
百分比備註: 禁止
100到900代表9種不同的字重,400代表normal,700代表bold,900代表是最重的代表是最重的。 ,bolder或lighter表示字體的字重比父元素高或低一級,例如父元素字重為400,則bolder代表字重500,如果父元素本身字重為9 00,那麼bolder後,字重還是900,同樣的lighter也一樣。有些字體並沒有100到900那麼全的字重,也許是從300到700,那麼字重的最小和最大值也為300到700。例如:
P { font-weight: normal } /* 400 */
H1 { font-weight: 700 } /* bold { font-weight: 700 } /* bold { font-weight: 700 } /* bold { font-weight: 700 } /* bold { font>名稱: 'font-size'
屬性值:
|
|
|
初始值: med
百分比備註: relative to parent element's font size
此屬性可使用絕對大小,也可使用相對大小,其中絕對大小可使用,下列關鍵字表示:
xx-small | small | medium | large | x-large | xx-large
分別代表最小、較少、小、中、大、較大、最大。
Relative size can be described using: larger or smaller.
For example:
P { font-size: 12pt; }
BLOCKQUOTE { font-size: larger }
EM { font-size: 150% }
EM { font-size: 1.5 em } The
Font attribute is a shortcut to the above attributes. The attributes are as follows:
Attribute name: 'font'
Attribute value: [ [ | | | | ]? [/ font: 12pt/14pt sans-serif } P { font: 80% sans-serif } P { font: x-large/110% "new century schoolbook", serif } P { font: bold italic large Palatino, serif }
P { font: normal small-caps 120%/120% fantasy }
6. Text (Text) attributes:
The attributes here will affect the text display in the WEB document.
The text-indent attribute describes the indentation degree of text:
Attribute name: 'text-indent'
Attribute value:
|
Initial value: 0
Suitable for objects : Container element
Whether to inherit: yes
Percentage Note: Depends on the width of the parent element
The following example shows that the indent value of the paragraph is 3em:
Attribute name: 'alignment'
Attribute value: left | right | center | justify
Initial value: as defined by user
Suitable object: block-level elements
Whether to inherit: yes
Percentage Note: Prohibited
text-decoration attribute describes the modification method of text:
Attribute name: 'text-decoration'
Attribute value: none | underline | | overline | | line-through | | blink ]
Initial value: none
Applicable objects: all elements
Whether to inherit: no (see clarification below)
Percentage remarks: prohibited
The meanings of the attribute values are:
underline: underline.
overline: underline.
line-through: delete the line.
blink: Blink (like the function of the blink tag in Navigator)
The text-shadow attribute can add a shadow effect to the text:
Attribute name: 'text-shadow'
Attribute value: none |
[,
]*
Initial value: none
Suitable objects: all
Whether to inherit: No
Percentage Note: Only valid when describing transparency
For example:
The letter-spacing attribute indicates the word spacing of the text: Attribute name: 'letter-spacing'
Attribute value: normal |
Initial value: normal
Suitable objects: all elements
Whether to inherit: yes
Percentage Note: Prohibited 'word-spacing'
Attribute value: normal |
Initial value: normal
Suitable objects: all elements
Whether to inherit: yes
For example :
H1 { word-spacing: 1em }
text-transform attribute can display the text in the BOX in the specified uppercase or lowercase form:
Attribute name: 'text-transform'
Attribute value : capitalize | uppercase | lowercase | none
Initial value: none
Suitable objects: all elements
Whether to inherit: yes
Percentage note: prohibited
The meaning of the attribute value is:
capitalize : Capitalize the first letter of each sentence in the BOX.
uppercase: Change all letters in BOX to uppercase.
lowercase: Change all letters in BOX to lowercase.
The White-space attribute describes how to display spaces in text. In HTML, spaces are omitted, which means that no matter how many spaces you enter at the beginning of a paragraph mark, it will be invalid. There are two ways to enter spaces. There are two methods, one is to directly enter the code of the space " ", or use the tag. There are also attributes similar to pre in CSS:
Attribute name: 'white-space'
Attribute value: normal | pre | nowrap
Initial value: normal
Suitable object: container element
Whether to inherit: yes
Percentage note: prohibited
For example:
PRE { white-space: pre }
P { white-space: normal }
CSS2 Quick Reference 5
Keywords: Others
7. List attributes:
The attributes here are used to describe lists (list ) a series of attributes.
list-style-type attribute describes the symbol used before each item in the list:
Attribute name: 'list-style-type'
Attribute value: disc | circle | square | decimal | lower- roman | upper-roman | lower-alpha | upper-alpha | none
Initial value: disc
Suitable object: list element
Whether to inherit: yes
Percentage remarks: prohibited
Attribute value The meaning is:
disc: round cake shape.
circle: hollow circle.
square: square.
decimal: decimal value.
lower-roman: lowercase Roman numerals.
upper-roman: Uppercase Roman numerals.
lower-alpha: lowercase Greek letters.
upper-alpha: uppercase Greek letters.
For example:
This is the first item.
You can also use list -style-image replaces the symbols at the front of the list with graphics:
This is the second item.
This is the third item.
Attribute name: 'list-style-image' Attribute value:
| none
Initial value: none
Suitable object: List element
Percentage Note: Forbidden
can be an absolute address or a relative address.
The list-style-position attribute is used to describe the position display of the list:
Attribute name: 'list-style-position'
Attribute value: inside | outside
Initial value: outside
Suitable Object: List element
Whether to inherit: yes
Percentage Note: Prohibited
The attribute values outside and inside represent display outside or inside the BOX respectively, for example:
UL { list-style: outside } UL.compact { list-style: inside }
first list item comes first
second list item comes second
first list item comes first
second list item comes second
list- The style attribute is a shortcut for the above attributes:
Attribute name: 'list-style' Attribute value: | | | |
Initial value: no
Suitable Object: List element
Whether to inherit: yes
Percentage Note: Prohibited
For example:
UL { list-style: upper-roman inside } /* Valid for any UL*/
UL ~ UL { list-style: circle outside } /* Valid for any UL mark inside UL*/
8. Table attributes:
Since most of the attributes in the table have been discussed in the above various attributes , so there are only two attributes introduced here:
row-span attribute describes the number of rows spanned by the table:
Attribute name: 'row-span'
Initial value : 1
Suitable objects: table elements
Whether to inherit: no
Percentage remarks: prohibited
column-span attribute describes the number of columns that the table spans:
Attribute name: 'column-span '
Initial value: 1
Suitable object: Table element
Whether to inherit: no
Percentage Note: Prohibited
9. User interface attributes:
cursor attribute, the user can specify the cursor shape to be used on an element:
Attribute name: 'cursor'
Initial value: auto
Suitable object: all elements
Whether to inherit: yes
Percentage Note: Prohibited Animated cursor.
At this point, all the existing properties of CSS have been introduced (there are also some auditory style sheets, which will not be introduced again). This article should come to an end, although the types of browsers that currently support style sheets are still limited. Not many, but they account for most of them. Style sheets will become a unified standard for browsers sooner or later. There are two reasons: 1. Style sheets are the only style standards accepted by W3C, and there is no sign that W3C will use JavaScript style sheets as a standard. Style sheet standards; secondly, CSS effectively solves the problem of introducing events into elements. Cooperating with scripts, it is an indispensable part of dynamic HTML. Try using style sheets to create Web pages, and you will find that it is really convenient. I hope my efforts can help you master the style sheets proficiently.