Table of Contents
回复内容:
Home Web Front-end H5 Tutorial 相比px而言,在响应式web设计中,em有何优势?

相比px而言,在响应式web设计中,em有何优势?

Jun 07, 2016 am 08:42 AM
em html px

我把html中所有长宽全部改用百分比,为了更好的适配不同尺寸的显示设备,而字体这块一直没有很好的解决办法,1em=16px,em的值并不是根据显示器宽度设置的百分比,那为什么还要将px改为em?我该怎么让我的字体大小也能有效的配合整体的百分比宽度?

回复内容:

px 是相对于屏幕分辨率而言的,一个小格子就代表一个 px 在不同分辨率的屏幕上,高分屏的 px 字体看起来会比低分屏的“小”,但事实上它们是一样大小的。
em 是根据元素字体大小定义的(如果本元素没有定义字体大小那么就以父元素字体大小为准,以此类推),字体大小是多少 px,该元素的 1em 就等于多少 px,所以 em 的优势就是可以根据屏幕分辨率的变化等比例地缩放字体。但是用 em 很不方便的地方就是随时都要考虑元素的字体大小,为了弥补这个缺点便出现了 rem。
rem 跟 em 不同的地方在于 rem 是根据根元素(html)的字体大小来确定的,像一般默认的 html 的字体大小是 16px
<span class="nt">html</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="o">:</span> <span class="m">16px</span><span class="p">;</span>
<span class="p">}</span>
Copy after login
em不是等于16px.

1em是等于父对象的字体大小.

要靠@media来控制不同分辨率下默认字体大小, 然后靠em控制该分辨率下的字体大小. 意义在于,你使用高分屏的时候,12px正好的字,在他上面就小了很多,你要修改px就要每个都改。
em只要在body上写2em,里面就自然2倍了。
倍儿爽。 并不是1em=16px
em是一个相对单位,他的大小是相对于父级设定的字体大小的。
比如

这是父级元素

这是子级元素




这种情况下,1em就=16px;
如果这样写

这是父级元素

这是子级元素



这种情况下,1em就=12px;
同理,如果把1em改成0.5em,那么就是6px; 1.相对于父级字体,方便比例选取
2.方便自适应于各种屏幕(移动开发时依靠字体默认大小来确定显示的大小)
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
4 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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles