Table of Contents
设置为 3em,就是自身大小的 3 倍;
Home Web Front-end HTML Tutorial 第 20 章 CSS3 前缀和 rem - 水之原

第 20 章 CSS3 前缀和 rem - 水之原

May 20, 2016 pm 04:49 PM

学习要点:

1.CSS3 前缀

2.长度单位 rem

 

主讲教师:李炎恢

 

本章主要探讨 HTML5 中 CSS 在发展中实行标准化的一些问题,重点探讨 CSS3 中新属性前缀问题和新的单位 rem。

 

一.CSS3 前缀

  在 CSS3 的很多新属性推出时,这些属性还处在不太稳定的阶段,随时可能被剔除。而此时的浏览器厂商为了实现这些属性,采用前缀方法。各大厂商前缀列表如下:

浏览器

厂商前缀

Chrome、Safari

-webkit-

Opera

-o-

Firefox

-moz-

Internet Explorer

-ms-

  我们之前学习过几个 CSS3 的新属性,比如:box-shadow、border-radius、opacity等。这几个属性我们在前面的使用中,并没有添加所谓的浏览器厂商前缀。那是因为,这些属性已经在主流浏览器或版本成为了标准。具体进化步骤如下:

1.属性尚未提出,这个属性所有浏览器不可用;

2.属性被提出,但未列入标准,浏览器厂商通过私有前缀来支持该属性;

3.属性被列入标准,可以使用前缀或不使用前缀来实现属性特性;

4.属性不需要再使用前缀,所有浏览器都稳定支持。

 

  我们就拿 border-radius 举例,它是 CSS3 的标准属性。早期的时候处于实验阶段,尚未列入标准时,需要使用厂商前缀。具体浏览器支持度如下:

属性

浏览器

带前缀版本

不带前缀版本

标准/实验

 

 

border-radius

 

IE

不支持

9.0+

标准

 

Firefox

3.0 需带-moz-

4.0+

Safari

3.1 需带-webkit-

5.1+

Chrome

4.0

5.0+

Opera

不支持

10.5+

  如果是手机等移动端一般采用的是 IOS 或安卓系统,那么基本上它的引擎是 webkit,直接参考-webkit-即可。

  在 CSS3 手册上,Chrome 支持 border-radius 的版本为 13.0,而部分教材和文章上写到只要 5.0。当然,这里可能表达的含义可能不同。而截至到 2015 年 4 月份最新的 Chrome 版本已经到 41.0 了,所以,不管是 5.0 还是 13.0 都是老古董了,没必要深究。Opera 支持 border-radius 版本为 11.5,而目前的版本是 28.0,也无伤大雅了。

  而被列入标准的 box-shadow 和 opacity 基本与 border-radius 前缀版本一致。

//因为目前处在标准阶段,没必要写前缀了 

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50px</span>;
}
Copy after login

//实验阶段的写法 

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    -webkit-border-radius</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    -moz-border-radius</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50px</span>;
}
Copy after login

  实验阶段的写法有三句,分别解释一下:-webkit-表示 Chrome 浏览器的私有属性前缀、-moz-表示 Firefox 私有属性前缀,如果是处于实验阶段的旧版本浏览器,那么不支持 border-radius,从而通过厂商前缀的方式来支持。如果是新版浏览器,已经是处于标准阶段,那么又有两种说法:1.如果新版浏览器废弃了前缀,那么前缀写法就不支持了,仅支持标准写法;2.如果新版浏览器没有废弃前缀,那么两种写法都可以,但要注意顺序,且属性值要保持一致。

  如果同时出现四个前缀+一个标准写法,四个前缀是当实验阶段时让四种引擎的浏览器厂商支持自己的私有属性,一个标准写法表示当这个属性列入标准后,使用新版浏览器运行时直接执行这个标准属性。

//前缀写法写在标准后面,且值不一样,就会出现问题

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    -webkit-border-radius</span>:<span style="color: #0000ff;"> 100px</span>;
}
Copy after login

  特别注意:1.IE 的前缀-ms-,和 Opera 的前缀-o-,在 border-radius 中不存在;2.现在的Opera 浏览器也支持-webkit-前缀,-webkit-border-radius 就能支持;3.Safari for Windows 已被苹果公司在 2012 年放弃,遗留版本为 5.1.7。

  最后说明:W3C 官方的立场表示实验阶段的属性仅为了测试,未来有可能修改或删除。而大量的开发者也认为在实际项目中不应该使用前缀,而使用一种优雅降级保证一定的用户体验,而通过渐进增减的方式让新版高级浏览器保证最高的效果。

 

二.长度单位 rem

  CSS3 引入了一些新的尺寸单位,这里重点推荐一个:rem 或者成为(根 em)。目前主流的现代浏览器都很稳定的支持。它和 em、百分比不同的是,它不是与父元素挂钩,而是相对于根元素的文本大小来计算的,这样能更好的统一整体页面的风格。

//首先,来一段 HTML

<span style="color: #0000ff;"><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span>标题<span style="color: #0000ff;"><span style="color: #800000;">em</span><span style="color: #0000ff;">></span>小标题<span style="color: #0000ff;"></span><span style="color: #800000;">em</span><span style="color: #0000ff;">></span><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">
    我是一个段落,我是一段</span><span style="color: #0000ff;"><span style="color: #800000;">code</span><span style="color: #0000ff;">></span>代码<span style="color: #0000ff;"></span><span style="color: #800000;">code</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span></span></span></span></span>
Copy after login

//其次来一段 CSS

<span style="color: #800000;">html </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 62.5%</span>;
}<span style="color: #800000;">

h1 </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 3em</span>;
}<span style="color: #800000;">

p </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 1.4em</span>;
}
Copy after login

  这里做几个解释,我们在之前的 Web 设计中大量使用了 px 单位进行布局。因为,早期的固定布局使用 px 较为方便,逐渐养成了这种习惯。而使用 em 单位其实更加灵活,尤其是在修改样式时,只需要修改一下挂钩元素的那个大小即可,无须每个元素一个个修改。

  但就算是 em,还是有一定问题。网页默认的字号大小为 16px,然后通过设置62.5%,将网页基准设置为 10px。而

设置为 3em,就是自身大小的 3 倍;

设置为1.4em,就是 10px 的 1.4 倍,即 14px。

  现在问题来了,里面的文本想设置 11px,怎么办呢?设置 1.1em 吗?不对,因为它挂钩的父元素不是而是<p>变成了 14px 的 1.1 倍了,而想设置 11px,则需要设置 0.786 倍才行。但是,这样的计算量太大了。所以,W3C 推出了直接基于根元素单位:rem。</p> <p>//直接基于的单位 </p> <div class="cnblogs_code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;span style=&quot;color: #800000;&quot;&gt;code &lt;/span&gt;{&lt;span style=&quot;color: #ff0000;&quot;&gt; font-size&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; 1.1 rem&lt;/span&gt;; }&lt;span style=&quot;font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; background-color: #ffffff;&quot;&gt; &lt;/span&gt;</pre><div class="contentsignin">Copy after login</div></div> </div> <table> <tbody> <tr> <td valign="bottom" width="250"> <p align="center"><strong>浏览器</strong></p> </td> <td valign="bottom" width="249"> <p><strong>rem 单位</strong></p> </td> </tr> <tr> <td valign="bottom" width="250"> <p align="center">Opera</p> </td> <td valign="bottom" width="249"> <p align="center">11.6+</p> </td> </tr> <tr> <td valign="bottom" width="250"> <p align="center">Firefox</p> </td> <td valign="bottom" width="249"> <p align="center">3.6+</p> </td> </tr> <tr> <td valign="bottom" width="250"> <p align="center">Safari</p> </td> <td valign="bottom" width="249"> <p align="center">5.0+</p> </td> </tr> <tr> <td valign="bottom" width="250"> <p align="center">Chrome</p> </td> <td valign="bottom" width="249"> <p align="center">6.0+</p> </td> </tr> <tr> <td valign="bottom" width="250"> <p align="center">IE</p> </td> <td valign="bottom" width="249"> <p align="center">9.0+</p> </td> </tr> </tbody> </table>

Statement of this Website
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

See all articles