了解UA StyleSheets和CSS3初始值

Imagine that you build a page using only , with no attached or applied. When you render the page in a browser, how does the program know to display links as blue and underlined, and paragraphs as black? Why is the margin for h1 elements 14pt in IE by default, and 0.67em in Firefox?

假设您仅使用构建页面,而没有附加或应用 。 在浏览器中呈现页面时,程序如何知道将链接显示为蓝色和带下划线的段落,将段落显示为黑色? 为什么默认情况下,IE中h1元素的margin默认为14pt ,而在Firefox中为0.67em

The answer to these questions is User Agent stylesheets. UA stylesheets are CSS files built into the browser. Each browser has its own UA file, which it uses to determine how to render every website, unless the browser is explicitly told otherwise.

这些问题的答案是用户代理样式表。 UA样式表是浏览器内置CSS文件。 除非明确告知浏览器否则每个浏览器都有自己的UA文件,该文件用于确定如何呈现每个网站

The way we tell the browser to use the site’s CSS, rather than the UA styles, is by linking or embedding a stylesheet to a page, or by using inline styles. The browser then makes a simple decision:

我们告诉浏览器使用网站CSS而不是UA样式的方法是, 将样式表链接或嵌入到页面中 ,或者使用内联样式。 然后,浏览器做出一个简单的决定:

  • If the new styles conflict with the browser’s UA stylesheet rules, the site’s style declaration will be used when rendering the element.

    如果新样式与浏览器的UA样式表规则冲突,则在呈现元素时将使用网站的样式声明。
  • Anything the site style rules don’t specify will be assumed by the UA stylesheet.

    UA样式表将假定网站样式规则中指定的任何内容。

This leads developers to create mammoth reset stylesheets that attempt to set every CSS property to a shared default value; applied to a web page, this effectively wipes out any effect of a UA stylesheet. The designer/developer can then build custom CSS for the site, somewhat assured that browsers will not unexpectedly assume a value for some property they have left unspecified. (You can see the UA CSS for IE, Opera at IEcss.com; reading the UA stylesheet for Firefox is easier: just type resource://gre-resources/html.css into the URL bar)

这导致开发人员创建庞大的重置样式表 ,尝试将每个CSS属性设置为共享的默认值。 应用于网页,这可以有效消除UA样式表的任何影响。 然后,设计人员/开发人员可以为该网站构建自定义CSS,这可以一定程度地保证浏览器不会意外地假定自己未指定的某些属性的值。 (您可以在IEcss.com上查看IE的UA CSS, Opera ;阅读Firefox的UA样式表更为简单:只需在URL栏中键入resource://gre-resources/html.css即可 )

CSS Resets are not without controversy; as I have discussed, I personally prefer a less heavy-handed approach. For example:

CSS重置并非没有争议。 正如我已经讨论过的 ,我个人更喜欢一种不那么笨拙的方法。 例如:

<div style="color: red;">
	<p>This is some text <span>in color.</span>
</div>

The color of the paragraph text, inherited from the parent <div>, will be red. What if we wanted to set the text inside the span to black? There are two choices. The traditional approach is this:

从父<div>继承的段落文本的颜色将为红色。 如果我们想将跨度内的文本设置为黑色怎么办? 有两种选择。 传统方法是这样的:

<div style="color: red;">
	<p>This is some text <span style="color: #000;">in color.</span>
</div>

There is also a second option:

还有第二种选择:

<div style="color: red;">
	<p>This is some text <span style="color: initial">in color.</span>
</div>

initial is a value that resets a property back to the default UA style. This can be particularly useful when the style you wish to negate may be, long, complex, or difficult to remember:

initial是一个将属性重置为默认UA样式的值。 当您希望否定的样式过长,复杂或难以记住时,此功能特别有用:

div {
	border: 3px dotted green;
}
div.special {
	border: initial;
} 
/* resets the border on divs with a class of “special” to that specified in the UA, i.e. no border; } */

重要的提示 (Important Note)

Internet Explorer does not currently support initial at all (at least as of this writing).

Internet Explorer当前根本不支持initial (至少在撰写本文时)。

翻译自: https://thenewcode.com/345/Understanding-UA-StyleSheets-and-the-CSS3-initial-Value

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值