search
HomeWeb Front-endHTML Tutorial第 14 章 CSS 颜色与度量单位 - 水之原

学习要点:

1.颜色表方案

2.度量单位

 

主讲教师:李炎恢

 

本章主要探讨 HTML5 中 CSS 颜色和度量单位等问题,包括颜色的选取方式、相对长度和绝对长度等。

 

一.颜色表方案

颜色的表现形式主要有三种方式:颜色名称、十六进制代码和十进制代码。

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;"> red</span>;
}

解释:这是将一个段落内的文字设置为红色,采用的是英文颜色名称。问题是,其他各种颜色我们将如何设置?

 

在古老的 HTML4 时,颜色名称只有 16 种。

颜色名称

十六进制代码

十进制代码

含义

black

#000000

0,0,0

黑色

silver

#c0c0c0

192,192,192

银灰色

gray

#808080

128,128,128

灰色

white

#ffffff

255,255,255

白色

maroon

#800000

128,0,0

栗色

red

#ff0000

255,0,0

红色

purple

#800080

128,0,128

紫色

fuchsia

#ff00ff

255,0,255

紫红

green

#008000

0,128,0

绿色

lime

#00ff00

0,255,0

闪光绿

olive

#808000

128,128,0

橄榄色

yellow

#ffff00

255,255,0

黄色

navy

#000080

0,0,128

海军蓝

blue

#0000ff

0,0,255

蓝色

teal

#008080

0,128,128

水鸭色

aqua

#00ffff

0,255,255

浅绿色

 

当然,目前颜色名称远远不止这些,可以搜索更多的 HTML 颜色表或 CSS 颜色表查阅。这里提供一些页面如下

http://xh.5156edu.com/page/z1015m9220j18754.html

http://finle.me/colors.html

http://www.w3school.com.cn/tags/html_ref_colornames.asp

 

在上面的表格中,我们也罗列出对应的十六进制和十进制颜色表示方法。使用方法如下:

//红色的十六进制方案 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;"> #ff0000</span>;
}

十进制表示方法就比较多样化,有四种方案:

函数

说明

示例

rgb(r,g,b)

用 RGB 模型表示颜色

rgb(0,128,128)

rgba(r,g,b,a)

同上,a 表示透明度 0~1 之间

rgba(0,128,128,0.5)

hsl(h,s,l)

用 HSL 模型(色相、饱和度和透明度)来表示颜色

hsl(120,100%,30%)

hsla(h,s,l,a)

同上,a 表示透明度 0~1 之间

hsla(120,100%,30%,0.5)

 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;"> rgb(112, 128, 114)</span>;<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;"> rgba(0, 128, 128, 0.5)</span>;<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;"> hsl(120, 100%, 30%)</span>;<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;"> hsla(120, 100%, 30%, 0.5)</span>;
}

 

目前又有一个疑问,这些值从哪里获取。除了颜色表之外,想要微调自己的颜色值。我们可以使用 photoshop 等平面设计软件的调色板获取相应的值。

 

二.度量单位

在 CSS 长度设置中,我们经常需要使用到度量单位,即以什么样的单位设计我们的字体或边框长度。而在 CSS 中长度单位又分为绝对长度和相对长度。

绝对长度指的是现实世界的度量单位,CSS 支持五种绝对长度单位。

绝对长度单位

单位标识符

说明

in

英寸

cm

厘米

mm

毫米

pt

pc

pica

相对长度指的是依托其他类型的单位,也是五种。

 相对长度单位

单位标识符

说明

em

与元素字号挂钩

ex

与元素字体的“x 高度”挂钩

rem

与根元素的字号挂钩

px

像素,与分辨率挂钩

%

相对另一值的百分比

 

下面我们使用一些常用的单位作为演示,而不做演示的基本用不到了。

 

//em 相对单位 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;"> silver</span>;<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 15px</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 2em</span>;
}

解释:em 是相对单位,与字号大小挂钩,会根据字体大小改变自己的大小,灵活性很高。

 

//px 相对单位,绝对特性

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;"> silver</span>;<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 15px</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 55px</span>;
}

解释:虽然 px 也是相对单位,但由于和分辨率挂钩,导致他其实就变成一个绝对单位了,自然灵活性没有 em 高,但是使用难度较低,且大量的开发者习惯性使用它。

 

//%百分比

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;"> silver</span>;<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 200%</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 50%</span>;
}

解释:长度比较好理解,就是挂钩它所在区块的宽度。而 font-size 则是继承到的原始大小的百分比。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools