CSS网页布局权威指南02 样式表内容

02 Stylesheet Contents

CSS网页布局权威指南02 样式表内容

Inside a stylesheet, you’ll find a number of rules that look a little something like this:

在样式表中,你会发现许多规则看起来有点像这样:

h1 {color: maroon;}
body {background: yellow;}

Styles such as these make up the bulk of any stylesheet—simple or complex, short or long. But which parts are which, and what do they represent?

诸如此类的样式构成了样式表的主体——简单的或复杂的,短的或长的。但是哪一部分是哪一部分,它们代表什么?

Rule Structure

规则的结构

To illustrate the concept of rules in more detail, let’s break down the structure.

为了更详细地说明规则的概念,让我们分解结构。

Each rule has two fundamental parts: the selector and the declaration block. The declaration block is composed of one or more declarations, and each declaration is a pairing of a property and a value. Every stylesheet is made up of a series of these rules. Figure 1-1 shows the parts of a rule.

每条规则都有两个基本部分:选择器和声明块。声明块由一个或多个声明组成,每个声明都是属性和值的一对。每个样式表都由一系列这样的规则组成。规则的组成如图1-1所示。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

The selector, shown on the left side of the rule, defines which piece of the document will be selected for styling. In Figure 1-1,

(heading level 1) elements are selected. If the selector were p , then all

(paragraph) elements would be selected.

显示在规则左侧的选择器定义将选择文档的哪一部分进行样式化。在图1-1中,选择

(标题级别1)元素。如果选择器是p,那么所有

(段落)元素都将被选中。

The right side of the rule contains the declaration block, which is made up of one or more declarations. Each declaration is a combination of a CSS property and a value of that property. In Figure 1-1, the declaration block contains two declarations. The first states that this rule will cause parts of the document to have a color of red , and the second states that part of the document will have a background of yellow . So, all of the

elements in the document (defined by the selector) will be styled in red text with a yellow background.

规则的右侧包含声明块,它由一个或多个声明组成。每个声明都是CSS属性和该属性值的组合。在图1-1中,声明块包含两个声明。第一个声明该规则将导致文档的某些部分具有红色,第二个声明文档的某些部分将具有黄色背景。因此,文档中的所有

元素(由选择器定义)都将被设置为红色文本和黄色背景。

Vendor Prefixing

供应商加前缀

Sometimes you’ll see pieces of CSS with hyphens and labels in front of them, like this: -oborder-image . These vendor prefixes were a way for browser vendors to mark properties, values, or other bits of CSS as being experimental or proprietary (or both). As of early 2023, a few vendor prefixes are in the wild, with the most common shown in Table 1-1.

有时你会看到CSS的前面有连字符和标签,像这样:- border-image。这些供应商前缀是浏览器供应商将属性、值或其他CSS标记为实验性或专有(或两者兼而有之)的一种方式。截至2023年初,一些供应商的前缀还在使用,最常见的如表1-1所示。

Prefix Vendor
-epub- International Digital Publishing Forum ePub format
-moz- Gecko-based browsers (e.g., Mozilla Firefox)
-ms- Microsoft Internet Explorer
-o- Opera-based browsers
-webkit- WebKit-based browsers (e.g., Apple Safari and Google Chrome)
Prefix Vendor
-epub- 国际数字出版论坛ePub格式
-moz- 基于gecko的浏览器(例如,Mozilla Firefox)
-ms- Microsoft Internet Explorer
-o- 欧鹏浏览器
-webkit- 基于webkit的浏览器(例如Apple Safari和Google Chrome)

As Table 1-1 indicates, the generally accepted format of a vendor prefix is a hyphen, a label, and a hyphen, although a few prefixes erroneously omit the first hyphen.

如表1-1所示,通常接受的供应商前缀格式是一个连字符、一个标签和一个连字符,尽管少数前缀错误地省略了第一个连字符。

The uses and abuses of vendor prefixes are long, tortuous, and beyond the scope of this book. Suffice to say that they started out as a way for vendors to test out new features, thus helping speed interoperability without worrying about being locked into legacy behaviors that were incompatible with other browsers. This avoided a whole class of problems that nearly strangled CSS in its infancy. Unfortunately, prefixed properties were then publicly deployed by web authors and ended up causing a whole new class of problems.

供应商前缀的使用和滥用是冗长而曲折的,超出了本书的范围。可以说,它们最初是作为供应商测试新功能的一种方式,从而帮助加快互操作性,而不必担心被锁定在与其他浏览器不兼容的遗留行为中。这避免了几乎扼杀CSS婴儿期的一系列问题。不幸的是,带前缀的属性随后被web作者公开部署,最终导致了一系列全新的问题。

As of early 2023, vendor-prefixed CSS features are nearly nonexistent, with old prefixed properties and values being slowly but steadily removed from browser implementations. You’ll quite likely never write prefixed CSS, but you may encounter it in the wild or inherit it in a legacy codebase. Here’s an example:

到2023年初,供应商前缀的CSS特性几乎不存在,旧的前缀属性和值正在缓慢但稳步地从浏览器实现中删除。您很可能永远不会编写带前缀的CSS,但您可能会在野外遇到它,或者在遗留代码库中继承它。这里有一个例子:

-webkit-transform-origin: 0 0;
-moz-transform-origin: 0 0;
-o-transform-origin: 0 0;
transform-origin: 0 0;

That’s saying the same thing four times: once each for the WebKit, Gecko (Firefox), and Opera browser lines, and then finally the CSS-standard way. Again, this is no longer necessary. We’re including it here only to give you an idea of what it might look like, should you come across this in the future.

同样的事情说了四遍:WebKit、Gecko (Firefox)和Opera浏览器各一次,最后是css标准方式。同样,这不再是必要的。我们把它包括在这里只是为了让你知道它可能是什么样子,如果你将来遇到它。

Whitespace Handling

空白处理

CSS is basically insensitive to whitespace between rules, and largely insensitive to whitespace within rules, although a few exceptions exist.

CSS基本上对规则之间的空白不敏感,基本上对规则内的空白不敏感,尽管存在一些例外。

In general, CSS treats whitespace just like HTML does: any sequence of whitespace characters is collapsed to a single space for parsing purposes. Thus, you can format this hypotheticalrainbow rule in the following ways,

一般来说,CSS对待空白的方式和HTML一样:任何空白字符序列都被压缩成一个空格,以便进行解析。因此,你可以用以下方式格式化这个假想的rainbow规则:

rainbow: infrared red orange yellow green blue indigo violet ultraviolet;rainbow:
infrared red orange yellow green blue indigo violet ultraviolet;rainbow:
infrared
red
orange
yellow
green
blue
indigo
violet
ultraviolet
;

as well as any other separation patterns you can think up. The only restriction is that the separating characters be whitespace: an empty space, a tab, or a newline, alone or in combination, as many as you like.

以及其他你能想到的分离模式。唯一的限制是分隔字符是空白:空格、制表符或换行符,可以单独使用,也可以组合使用,随你喜欢。

Similarly, you can format series of rules with whitespace in any fashion you like. These are just five examples out of an effectively infinite number of possibilities:

类似地,您可以以任何喜欢的方式用空格格式化一系列规则。这些只是无数可能性中的五个例子:

html{color:black;}
body {background: white;}
p {color: gray;}
h2 {color : silver ;
}
ol
{color:silver;
}

As you can see from the first rule, whitespace can be largely omitted. Indeed, this is usually the case with minified CSS, which is CSS that’s had every last possible bit of extraneous whitespace removed, usually by an automated server-side script of some sort. The rules after the first two use progressively more extravagant amounts of whitespace until, in the last rule, pretty much everything that can be separated onto its own line has been.

正如您从第一条规则中看到的,空格在很大程度上可以省略。实际上,这种情况通常出现在小型化CSS中,小型化CSS删除了所有可能的无关空白,通常是通过某种类型的自动化服务器端脚本。在前两个规则之后的规则使用越来越多的空白,直到最后一个规则中,几乎所有可以单独分隔到一行的内容都被分隔开了。

All of these approaches are valid, so you should pick the formatting that makes the most sense— that is, is easiest to read—in your eyes, and stick with it.

所有这些方法都是有效的,所以您应该选择最有意义的格式——也就是说,最容易在您的眼睛中阅读,并坚持使用它。

CSS Comments

CSS的注释

CSS does allow for comments. These are very similar to C/C++ comments in that they are surrounded by / * and * /:

CSS允许注释。这些注释与C/ c++注释非常相似,它们被/ *和* /包围:

/* This is a CSS comment */

Comments can span multiple lines, just as in C++:

注释可以跨越多行,就像c++一样:

/* This is a CSS comment, and it
can be several lines long without
any problem whatsoever. */

It’s important to remember that CSS comments cannot be nested. So, for example, this would not be correct:

重要的是要记住,CSS注释不能嵌套。所以,例如,这是不正确的:

/* This is a comment, in which we find
another comment, which is WRONG
/* Another comment */
and back to the first comment, which is not a comment.*/

One way to create “nested” comments accidentally is to temporarily comment out a large block of a stylesheet that already contains a comment. Since CSS doesn’t permit nested comments, the “outside” comment will end where the “inside” comment ends.

意外创建“嵌套”注释的一种方法是临时注释掉样式表中已经包含注释的大块内容。由于CSS不允许嵌套注释,所以“外部”注释将在“内部”注释结束的地方结束。

Unfortunately, there is no “rest of the line” comment pattern such as // or # (the latter of which is reserved for ID selectors anyway). The only comment pattern in CSS is / * * /. Therefore, if you wish to place comments on the same line as markup, you need to be careful about how you place them. For example, this is the correct way to do it:

不幸的是,没有像//或#这样的“其余行”注释模式(后者是为ID选择器保留的)。CSS中唯一的注释模式是/ * * /。因此,如果希望将注释放置在与标记相同的行上,则需要小心放置注释的方式。例如,这是正确的做法:

h1 {color: gray;} /* This CSS comment is several lines */
h2 {color: silver;} /* long, but since it is alongside */
p {color: white;} /* actual styles, each line needs to */
pre {color: gray;} /* be wrapped in comment markers. */

Given this example, if each line isn’t marked off, most of the stylesheet will become part of the comment and thus will not work:

在这个例子中,如果每一行都没有被标记,大多数样式表将成为注释的一部分,因此将无法工作:

h1 {color: gray;} /* This CSS comment is several lines
h2 {color: silver;} long, but since it is not wrapped
p {color: white;} in comment markers, the last three
pre {color: gray;} styles are part of the comment. */

In this example, only the first rule ( h1 {color: gray;} ) will be applied to the document. The rest of the rules, as part of the comment, are ignored by the browser’s rendering engine.

在本例中,只有第一条规则(h1 {color: gray;})将应用于文档。其余的规则,作为注释的一部分,被浏览器的呈现引擎忽略。

CSS comments are treated by the CSS parser as if they do not exist at all, and so do not count as whitespace for parsing purposes. This means you can put them into the middle of rules—even right inside declarations!

CSS注释被CSS解析器当作根本不存在的注释来处理,因此在解析时不会将其视为空白。这意味着您可以将它们放在规则中间——甚至就在声明中!

Markup

标记

There is no markup in stylesheets. This might seem obvious, but you’d be surprised. The one exception is HTML comment markup, which is permitted inside

样式表中没有标记。这似乎是显而易见的,但你会感到惊讶。唯一的例外是HTML注释标记,由于历史原因可以在

<style><!--
h1 {color: maroon;}
body {background: yellow;}
--></style>

That’s it, and even that isn’t recommended anymore; the browsers that needed it have faded into near oblivion.

就是这样,即使这样也不再被推荐;需要它的浏览器已经几乎被遗忘了。

Speaking of markup, it’s time to take a very slight detour to talk about the elements that our CSS will be used to style, and how those can be affected by CSS in the most fundamental ways.

说到标记,现在是时候稍微绕一下弯路来讨论CSS将用于设置样式的元素,以及CSS如何以最基本的方式影响这些元素。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/788794.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Pointnet++改进即插即用系列:全网首发OREPA在线重新参数化卷积,替代普通卷积 |即插即用,提升特征提取模块性能

简介:1.该教程提供大量的首发改进的方式,降低上手难度,多种结构改进,助力寻找创新点!2.本篇文章对Pointnet++特征提取模块进行改进,加入OREPA,提升性能。3.专栏持续更新,紧随最新的研究内容。 目录 1.理论介绍 2.修改步骤 2.1 步骤一 2.2 步骤二 2.3 步骤三

【25考研】:四川大学计算机学院24届874考研考情分析

去年的考情分析也是我做的&#xff0c; 今年就在去年的基础上做了。保持形式不变&#xff0c;更改数据。 21考情&#xff1a; 万载月寒肠断客&#xff1a;四川大学计算机学院21届CS考研考情分析 22考情&#xff1a; 懒羊羊&#xff1a;四川大学计算机学院2022考研考情分析 2…

AI 论道|极狐GitLab 客户私享会上海站成功举办

3 月 22 日下午&#xff0c;极狐GitLab 在上海办公室举办了客户私享会&#xff0c;邀请了来自多个行业的多家客户&#xff0c;围绕 AI 提升研发效率的道法术器进行了充分交流。整个交流时长达两个多小时。 极狐GitLab 战略业务与区域发展副总裁何庆出席了此次活动并致开场辞。他…

图论做题笔记:dfs

Leetcode - 797&#xff1a;所有可能的路径 题目&#xff1a; 给你一个有 n 个节点的 有向无环图&#xff08;DAG&#xff09;&#xff0c;请你找出所有从节点 0 到节点 n-1 的路径并输出&#xff08;不要求按特定顺序&#xff09; graph[i] 是一个从节点 i 可以访问的所有节…

CSS基础语法-黑马跟课笔记-供记录与查询

一.CSS简介 1.1HTML局限性 只关注内容的语义&#xff0c;可以做简单的样式但是很臃肿且繁琐 1.2CSS优势 CSS层叠样式表&#xff0c;标记语言 设置HTML页面中的文本内容&#xff0c;图片外形&#xff0c;可以美化HTML&#xff0c;让页面布局更美观 HTML做框架&#xff0c;…

【Python面试题收录】Python的可变对象与不可变对象

一、可变对象与不可变对象的定义 在Python中&#xff0c;对象的可变性是指对象的内部状态&#xff08;值&#xff09;是否允许在对象创建后发生改变。根据这一特性&#xff0c;Python的数据类型可以分为两大类&#xff1a;可变对象&#xff08;mutable objects&#xff09;和不…

数据结构——红黑树详解

一、红黑树的定义 红黑树&#xff0c;是一种二叉搜索树&#xff0c;但在每个结点上增加一个存储位表示结点的颜色&#xff0c;可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制&#xff0c;红黑树确保没有一条路径会比其他路径长出两倍&#xff0c…

JavaScript中字符串、列表、对象常用方法

一. String const str hello world!; 1. 查询 1.1 indexOf 返回指定字符串值在字符串中首次出现的位置&#xff0c;没有找到则返回 -1 const n str.indexOf(world)1.2 includes 用于判断字符串是否包含指定的子字符串。如果找到匹配的字符串则返回 true&#xff0c;否则…

基于Docker for Windows部署ChatGPT-Next-Web

基于Docker for Windows部署ChatGPT-Next-Web 项目地址安装Docker for Windows部署项目参数讲解参数示例 运行 项目地址 https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web 安装Docker for Windows 官网地址&#xff1a;https://www.docker.com/ 下拉找到Download 选择W…

11、Lua 数组

Lua 数组 Lua 数组一维数组多维数组 Lua 数组 数组&#xff0c;就是相同数据类型的元素按一定顺序排列的集合&#xff0c;可以是一维数组和多维数组。 Lua数组的索引键值可以使用整数表示&#xff0c;数组的大小不是固定的。 一维数组 一维数组是最简单的数组&#xff0c;其…

Rust 的 termion 库控制终端光标的位置

在控制台应用程序中&#xff0c;固定打印在屏幕的第一行通常涉及到控制终端光标的位置。Rust 标准库本身并不提供直接控制终端光标位置的功能&#xff0c;但你可以使用第三方库如 termion 来实现这个需求。 termion 是一个用于处理终端的 Rust 库&#xff0c;它提供了很多有用…

Java设计模式:代理模式的静态和动态之分(八)

码到三十五 &#xff1a; 个人主页 心中有诗画&#xff0c;指尖舞代码&#xff0c;目光览世界&#xff0c;步履越千山&#xff0c;人间尽值得 ! 在软件设计中&#xff0c;代理模式是一种常用的设计模式&#xff0c;它为我们提供了一种方式来控制对原始对象的访问。在Java中&a…

文件操作的详序

1.为什么使用文件&#xff1f; 如果没有文件&#xff0c;我们写的程序的数据是存储在电脑的内存中&#xff0c;如果程序退出&#xff0c;内存回收&#xff0c;数据就丢失了&#xff0c;等再次运行程序&#xff0c;是看不到程序的数据的&#xff0c;如果将数据进行持久化的保存…

【社会救助管理系统】主要设计及拟采用的技术方案

主要设计及拟采用的技术方案 1. 主要设计&#xff08;1&#xff09;系统架构设计&#xff08;2&#xff09;功能设计&#xff08;3&#xff09;安全性设计 2. 设计思想&#xff08;1&#xff09;系统架构设计思想&#xff08;2&#xff09;功能设计思想&#xff08;3&#xff0…

python实现TCP服务器

import socket # 创建一个socket对象 server_socket socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 获取本地主机名 host socket.gethostname() # 设置一个端口 port 12345 # 绑定端口 server_socket.bind((host, port)) # 设置最大连接数&#xff0c;超过…

Linux 使用管理线程、多线程、分离线程

目录 一、使用线程 1、pthread_create创建线程 2、pthread_join等待线程 主线程获取新线程退出结果 获取新线程退出返回的数组 3、线程异常导致进程终止 4、pthread_exit 5、pthread_cancel 6、主线程可以取消新线程&#xff0c;新线程可以取消主线程吗 二、如何管理线…

使用C#清除字符串结尾的数字的方法参考

假如我们要复制一个物体&#xff0c;一般我们希望复制出来的物体与原来的物体名字不同&#xff0c;而且最好是结尾加上序号&#xff0c;比如原始物体叫abc&#xff0c;那么复制出来的物体叫abc0001。 问题是如果物体本来已经带序号了&#xff0c;比如已经叫abc0005了&#xff0…

59 使用 uqrcodejs 生成二维码

前言 这是一个最近的一个来自于朋友的需求, 然后做了一个 基于 uqrcodejs 来生成 二维码的一个 demo package.json 中增加以依赖 "uqrcodejs": "^4.0.7", 测试用例 <template><div class"hello"><canvas id"qrcode&qu…

nginx + keepalived 搭建教程

1.安装依赖 yum install -y keepalived systemctl start keepalived systemctl enable keepalived 2.配置 a. keepalived.conf配置 global_defs {router_id nginx_server2 # 机器标识(backup节点为nfs_server2) }vrrp_script chk { script "/etc/keepalived/check_po…

信息系统项目管理师——第18章项目绩效域管理(一)

本章节内容属于第四版新增知识&#xff0c;为PMBOK第七版专有&#xff0c;选择、案例、论文都会考&#xff0c;属于比较重要的章节。 选择题&#xff0c;稳定考3分左右&#xff0c;新教材基本考课本原话&#xff0c;需要多读课本&#xff0c;多刷题。 案例题&#xff0c;考的概…