CSS记要

元素

从分类上,2种元素

1.         可被置换:没有内容

2.         非被置换的:有内容

 

从表现上,2种元素

1.         块级元素:

2.         行级元素:

 

CSS简介

CSSHTML位置中的三种方式

1.         放在head<head>,通过style标签指定

<head>

<style>B {text-transform: uppercase}</style>

</head>

selector: B

2.         放在body中,通过普通标签加上styleclassid属性来指定,扩展或覆盖普通标签的基本特征

<p style=”font-size: x-large”>

selector: p

body中,也可以通过<span><div>特定的标签加上styleclassid属性指定

<div class=”test”>

3.         单独一个外部文件,通过link标签来指定

<link rel=’stylesheet’ href=’formatting.css’ type=’ text/css’>

 

应用CSS4HTML标签及3HTML属性:

l         <style><span><div><link>

l         style, class, id

 

 

<span>是一个行级样式,用于临时覆盖已经定义好的样式。<div>也一样,但主要用于块级元素。

 

行级元素,指在一行内所出现的HTML标签,如<b><i>等,这些元素在屏幕上并不导致重新开始或结束一行。块级元素,如<p><h1>等,将导致重新开始或结束一行。

 

内联样式在定义两个以上的样式时,样式之间需要以“;”隔开。

 

 

几种CSS的优先顺序:

1.         父辈的样式将被子辈的样式继承

2.         按明示的权重排序,而标有!IMPORTANT的样式具有最高权重。

3.         通过<link>导入的标签,将被本页中的样式覆盖。

4.         特定的样式覆盖一般的样式

5.         具有相同权重的样式,最后出现的样式覆盖之前的样式。

 

CSS:

       Cascade Styled Sheet

 

 

<class>的定义:

1.         与具体标签挂钩,只能被所定义的标签使用。如

h1.red {color: red}

h1.green {color: green}

h1.blue {color: blue}

2.         不与具体标签挂钩,可被任何标签使用。如

.red {color: red}

.green {color: green}

.blue {color: blue}

 

CSS格式与语法

CSS基本语法

l         选择器 {声明; | 声明; …}

声明中,如果有多项属性的,以空格隔开。唯一例外:

font: large/150% sans-serif;

 

l         组合:

Ø         选择器组合

h1, h2, h3 {font-family: Arial;}

Ø         声明组合

p1, p2, p3 {font-family: Arial;}

Ø         选择器 + 声明的组合

h1, h2, h3 {font-family: Arial; font-size: x-large;}

 

 

l         文档通配选择器(CSS2):适用于文档下所有元素,但级别低于任何已经定义好的元素

* {color: red;}

 

classid选择器

       可将基本元素进行更细化的分类。

l         class选择器

1.         定义: .warning {color: red} =  *.warning {color: red},可用于所有元素

使用: <p class=”warning”>This is a warning paragraph.</p>

<h1 class=”warning”> This is a warning h1.</h1>

2.         定义: p.warning {color: red},只用于特定元素

使用: <p class=”warning”>This is a warning paragraph.</p>

3.         多类。

定义:.warning {font: bold;} ------------------------------à (a)

.urgent {font: italic;} -------------------------------à (b)

.warning.urgent {background: silver;} ----------à (c) 同时符合两个条件时(注意,类选择器中间不能有空格)

 

使用:[顺序无关紧要]

<p class=”urgent warning”>This paragraph contains multiple class selector.</p>

                     注意:IE bug: IE中,对于以上(a), (b), (c)三种定义,<p class=”urgent warning”><p class=”urgent”>均将背景设为silver。而正确的应是,只有在同时符合两个条件,即<p class=”urgent warning”>,才能设为silverIE显然将(c)解释为只要符合两个条件其一即可。

 

l         id选择器

CSS中,id选择器可以被多次使用,但HTML要求同一网页中只能有一个唯一的id出现。id选择器大小写敏感。

定义:#warning {font: bold;}

使用:<p id=”warning”>A warning paragraph.</p>

 

属性选择器 [IE不支持]

1.         简单属性选择器:

h[class] {color: red;}

h[class][title] {color; red;}

2.         有属性值的属性选择器:

h[class=”warning”] {color: red;}

h[class=”warning”][title=”important”] {color: red;}

3.         匹配部分属性值的属性选择器:

h[class~=”warning”] {color: red;}

4.         匹配特定属性的属性选择器:

*[lang|=”en”] {color: red;}

 

相邻元素选择器

1.         普通下属选择器:应用于在DOM中具有上下级结构的两个以上的元素,不管是否直接父子关系

              p em {color: red;}

2.         父子关系选择器:应用于在DOM中具有父子结构的两个以上的元素 [IE不支持]

h1 > strong {color: red;}

3.         相邻兄弟选择器:应用于在DOM中具有相邻兄弟结构的两个以上的元素 [IE不支持]

h1 + p {margin-top: 0;}

 

 

伪类选择器及伪元素选择器

l         伪类选择器

1.         <a:XXX>:决定链接如何显示。

定义的顺序很重要:link -> visited -> focus -> hover -> active [lvfha]

a:link – 正常状态 [静态]

a:visited – 被点击后 [静态]

a:focus – 获得输入焦点时 [动态]

a:hover – 鼠标停留时 [动态]

a:active – 被点击时 [动态]

注意:动态的伪类可被所有元素使用,但在IE中,只有<a>元素才能使用动态的伪类。

2.         first-child:选择作为其他元素第一个子元素的那个元素 [IE不支持]

p:first-child {color: red;}

3.         lang:选择语言 [IE不支持]

*:lang(en) {color: red;}

4.         伪类的组合:

a:link:hover {color: red;}

注意:IE不支持,在上述例子中,它只使a:hover {color: red;}起作用。

l         伪元素选择器

1.         first-line:选择元素第一行

p:first-line {}

除去字体、颜色及背景属性外,只能设置以下的属性:word-spacing, letter-spacing, text-decoration, text-transform, line-height, clear。此外,只能出现在选择器的最后。如p:first-line em{}是无效的。

2.         first-letter:选择元素第一个字母

p:first-letter{}

除去字体、颜色及背景属性外,只能设置以下的属性:word-spacing, letter-spacing, text-decoration, text-transform, line-height, clear。此外,只能出现在选择器的最后。如p:first-letter em{}是无效的。

3.         before:在元素之前产生特定的内容 [IE不支持]

h2:before {content: "[["; color: red;}

4.         after:在元素之后产生特定的内容 [IE不支持]

h2:after {content: "]]"; color: red;}

 

 

CSS的级联结构

3种级联结构

1.         权重

2.         继承

3.         级联

 

权重

l         权重的格式

0,0,0,0

1,0,0,1

l         权重的比较

从左到右,逐一比较,大者胜出。

l         权重的计算:

Ø         !important的权重最高,格式为:

p {color: green !important; background: yellow;}

Ø         行级样式,权重为1,0,0,0

Ø         对于选择器中每一个id的属性值,加0,1,0,0

例如:#warning {color: red;}      /*     权重 =  0,1,0,0      */

Ø         对于选择器中每一个类属性值,属性选择,或伪类,加0,0,1,0

例如:.red {color: red;}           /*     权重 =  0,0,1,0      */

*.red {color: red;}          /*     权重 =  0,0,1,0      */

                         /* 通配符的权重为 0,0,0,0   */

a:link {color: red;}          /*     权重 =  0,0,1,0      */

[href] {color: red;}          /*     权重 =  0,0,1,0      */

Ø         对于选择器中每一个元素,伪元素,加0,0,0,1

例如:h1 {color: red;}             /*     权重 =  0,0,0,1      */

p:first-line {color: red;}      /*     权重 =  0,0,0,1      */

h1 em {color: red;}          /*     权重 =  0,0,0,2      */

Ø         组合及通配选择器不增加权重

l         权重的拆分:

h1, h2, h3 {color: red; font-size: 12px;}

拆分为:

h1 {color: red;}

h1 {font-size: 12px;}

h2 {color: red;}

h2 {font-size: 12px;}

h3 {color: red;}

h3 {font-size: 12px;}

 

继承

从上到下,但有一例外:<body>的属性可以从下到上传给<html>

几个不能继承的元素:border及其属性。

继承没有权重,或者说其权重比其他元素的权重都低,包括权重为0的通配符。

 

级联

规则:

1.         找出所有包含符合给定元素声明

 

 

CSS单位

度量单位

       12pt为一个汉字的宽度

       绝对度量值:

u       inches: 英寸,1英寸=25.4毫米 = 72 = 6皮卡

u       millimeters: 毫米

u       centimeters: 厘米,1厘米=10毫米

u       picas:皮卡,1皮卡 = 1/6英寸 = 12 = 4.233毫米

u       point: 点,1= 1/72英寸 = 0.353毫米 (默认情况下,显示器上可变宽度字体的大小为12点,如Times Roman;固定宽度字体大小为10点,如Courier

       相对度量值:

u       pixels: 像素,1像素 = 0.25 – 0.35毫米 (显示器长宽的标准度量单位,与显示器与分辨率有关)

u       m-length:特定字体,英语单词”m”的宽度

u       x-height:特定字体,英语单词”x”的高度

等宽字体的这两个值是相等的

 

      

l         长度单位:共有8种,inches(in), millimeters(mm), centimeters(cm), picas(pc), point(pt), pixels(px), m-length(em), x-height(ex).

l         百分比单位:%,相对于屏幕。

 

颜色表示方法:

1.         颜色名,如blue

2.         616进制,如#FFCCFF

3.         精简316进制,如#FCF,每位都将被复制一位出来

4.         RGB方式,如rgb(255,255,0)

5.         百分比的RGB方式,如rgb(0%,100%,0%)

 

 

 

 

 

font属性

<font>标签在HTML 4.0中已被废弃。

 

l         font-family

六个安全的font-family应放在最后: serif, sans-serif, cursive, fantasy, monospace

 

l         font-size

值:

²        xx-small | x-small | small | medium | large | x-large | xx-large : 绝对形式,从小到大

²        larger | smaller: 相对于已经设定好的设定值

²        n + 度量单位

²        %

 

l         font-style

是否斜体

值:

²        normal | italic | oblique:后两者均为斜体

 

l         font-variant

大小写

值:

²        normal | small-caps

 

l         font-weight

设置字体的粗度

值:

²        100 | 200 | 300 | 400(normal) | 500 | 600 | 700(bold) | 800 | 900: 绝对值

²        bold | normal

²        bolder | lighter: 相对于已经设定好的设定值

 

l         font

设置以上所有字体属性。注意:{font: bold; 40pt; sans-serif;}{font: 40pt; bold; sans-serif;}将会出现不一样的效果。

 

 

颜色

l         color

设置颜色

值:

²        颜色名,如blue

²        616进制,如#FFCCFF

²        精简316进制,如#FCF,每位都将被复制一位出来

²        RGB方式,如rgb(255,255,0)

²        百分比的RGB方式,如rgb(0%,100%,0%)

 

l         background-color

设置背景颜色

值:

²        color一样的5种设置方式

²        transparent: 透明

 

背景图像

l         background-image

设置背景图像

值:

²        url(img): background-image: url(img/boy.gif)

 

l         background-repeat

设置背景图像是否重复

值:

²        repeat

²        repeat-x

²        repeat-y

²        no-repeat

 

l         background-attachment

设置窗口滚动时,背景图像是否移动

值:

²        scroll

²        fixed

 

l         background-position

设置背景图像的位置

值:

²        x% y%

²        x y

²        (left | center | right) | (top | center | bottom)

 

l         background

设置以上所有图像属性,包括background-color。次序无关紧要。

 

 

 

文本

l         letter-spacing

设置英语单个字母之间的间隙

值:

²        normal

²        n + 度量单位

 

l         word-spacing

设置单字之间的间隙

值:

²        normal

²        n + 度量单位

 

l         line-height

设置行距

值:

²        normal

²        n + 度量单位

²        %

 

l         vertical-align

设置相对于页面上其他元素的文本垂直位置

值:

²        baseline

²        bottom

²        middle

²        sub

²        super

²        text-top

²        text-bottom

²        top

²        n + 度量单位

²        %

IE6中,只有sub, super, top得到支持

 

 

l         text-align

排列文本

值:

²        center

²        justify

²        left

²        right

 

l         text-decoration

修饰文本

值:

²        blink: IE 6不支持

²        line-through

²        none

²        overline

²        uderline

 

l         text-indent

相对于左边界的第一行的文本缩进

值:

²        n + 度量单位 (可为负值)

²        %

 

l         text-transform

设置单词大小写

值:

²        capitalize: 所有单词的首字母均为大写

²        lowercase

²        none

²        uppercase

 

 

边框

三大类:

 

1.         border: 边框

2.         margin:外部间距,边框与其他元素的间距

3.         padding:内部间距,内部元素与边框的间距

4.         clear | float | height | width

 

 

l         border-style

设置边框类型

值:

²        dashed

²        dotted

²        double

²        inset

²        groove

²        none

²        outset

²        ridge

²        solid

在使用inset, outset, ridge, groove等属性时,注意将border-color的属性设为非黑色,否则看不出立体效果。

 

l         border-color

设置边框颜色

值:

参看颜色

 

l         border-width

设置边框厚度。在IE6中,只设置border-width不行,还必须同时设置border-style;而且边框的宽度将充满整个屏幕的宽度。

值:

²        n [ n n n ] : 默认为像素(pixels)度量单位

²        n [ n n n ] + 度量单位:必须设置所有需要的度量单位

²        n: 所有的边框厚度

²        n n: 第一个参数指定顶端及底端的厚度,第二个参数指定左右两边的厚度

²        n n n: 第一个参数指定顶端的厚度,第二个参数指定左右两边的厚度,第三个参数指定底端的厚度

²        n n n n: 第一个参数指定顶端的厚度,第二个参数指定右边的厚度,第三个参数指定底端的厚度,第四个参数指定左边的厚度(顺时针,从顶端开始)

²        thin | medium | thick

 

l         border-bottom

设置边框底端厚度。可设置border-style, border-color, border-width的所有属性。

 

l         border-left

设置边框左边厚度。可设置border-style, border-color, border-width的所有属性。

 

l         border-right

设置边框右边厚度。可设置border-style, border-color, border-width的所有属性。

 

l         border-top

设置边框顶端厚度。可设置border-style, border-color, border-width的所有属性。

 

l         border-bottom-width

设置边框底端厚度。border-bottom可以只设置单边的宽度,而与border-bottom不同的是,border-bottom-width由于必须设置border-style,而border-style将设置所有边的属性,因此,其他的边将用缺省的值来设置。

值:

²        n : 默认为像素(pixels)度量单位

²        n + 度量单位

²        thin | medium | thick

 

l         border-left-width

设置边框底端厚度。border-left可以只设置单边的宽度,而与border-left不同的是,border-left-width由于必须设置border-style,而border-style将设置所有边的属性,因此,其他的边将用缺省的值来设置。

值:

²        n : 默认为像素(pixels)度量单位

²        n + 度量单位

²        thin | medium | thick

 

l         border-right-width

设置边框底端厚度。border-right可以只设置单边的宽度,而与border-right不同的是,border-right-width由于必须设置border-style,而border-style将设置所有边的属性,因此,其他的边将用缺省的值来设置。

值:

²        n : 默认为像素(pixels)度量单位

²        n + 度量单位

²        thin | medium | thick

 

l         border-top-width

设置边框底端厚度。border-top可以只设置单边的宽度,而与border-top不同的是,border-top-width由于必须设置border-style,而border-style将设置所有边的属性,因此,其他的边将用缺省的值来设置。

值:

²        n : 默认为像素(pixels)度量单位

²        n + 度量单位

²        thin | medium | thick

 

l         border

设置边框属性。可设置border-style, border-color, border-width的所有属性。

 

l         clear

设置其他元素不能浮动于其相应的边上。<br clear=”all”>可在设了float属性后真正地另起一行。

值:

²        both

²        left

²        right

²        none

 

l         float

设置元素的浮动位置。可将块级元素的断行功能解除掉。注意,当同时设置了marginfloat属性后,在IE6中将对margin对齐。

值:

²        none

²        left

²        right

 

l         height

设置元素的高度。适用于非文本对象。

值:

²        auto

²        %

²        n + 度量单位

 

l         width

设置元素的宽度。适用于非文本对象。

值:

²        auto

²        %

²        n + 度量单位

 

l         padding-bottom

设置块级元素中内部元素的底端到边框底端的距离。

值:

²        %: 相对于本块级元素的宽度

²        n + 度量单位

 

l         padding-left

设置块级元素中内部元素的左端到边框左端的距离。

值:

²        %

²        n + 度量单位

 

l         padding-right

设置块级元素中内部元素的右端到边框右端的距离。

值:

²        %

²        n + 度量单位

 

l         padding-top

设置块级元素中内部元素的顶端到边框顶端的距离。

值:

²        %

²        n + 度量单位

 

l         padding

设置padding-bottom, padding-left, padding-right, padding-top的所有属性值。

值:

²        auto

²        %

²        n [ n n n ] : 默认为像素(pixels)度量单位

²        n [ n n n ] + 度量单位:必须设置所有需要的度量单位

²        n: 所有的边框距离

²        n n: 第一个参数指定顶端及底端的距离,第二个参数指定左右两边的距离

²        n n n: 第一个参数指定顶端的距离,第二个参数指定左右两边的距离,第三个参数指定底端的距离

²        n n n n: 第一个参数指定顶端的距离,第二个参数指定右边的距离,第三个参数指定底端的距离,第四个参数指定左边的距离(顺时针,从顶端开始)

 

l         margin-bottom

设置元素边框底端到其他元素边框顶端的距离。

值:

²        auto

²        %: 相对于本元素的宽度

²        n + 度量单位

 

l         margin-left

设置元素边框左边到其他元素边框右边的距离。

值:

²        auto

²        %:

²        n + 度量单位

 

l         margin-right

设置元素边框右边到其他元素边框左边的距离。

值:

²        auto

²        %:

²        n + 度量单位

 

l         margin-top

设置元素边框顶端到其他元素边框底端的距离。

值:

²        auto

²        %:

²        n + 度量单位

 

l         margin

设置margin-bottom, margin-left, margin-right, margin-top的所有属性值。

值:

²        auto: 接受原来已经设置好的值

²        %

²        n [ n n n ] : 默认为像素(pixels)度量单位

²        n [ n n n ] + 度量单位:必须设置所有需要的度量单位

²        n: 所有的边框距离

²        n n: 第一个参数指定顶端及底端的距离,第二个参数指定左右两边的距离

²        n n n: 第一个参数指定顶端的距离,第二个参数指定左右两边的距离,第三个参数指定底端的距离

²        n n n n: 第一个参数指定顶端的距离,第二个参数指定右边的距离,第三个参数指定底端的距离,第四个参数指定左边的距离(顺时针,从顶端开始)

 

 

CSS的使用

几条基本原则:

1.         先定义文档结构,再编辑CSS选择器

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值