Home Web Front-end HTML Tutorial CSS: 7 units you may not know_html/css_WEB-ITnose

CSS: 7 units you may not know_html/css_WEB-ITnose

Jun 24, 2016 am 11:52 AM

Original text: 7 CSS Units You Might Not Know About


As we all know, when using CSS technology, it is easy to be trapped by some strange problems. And when we face new problems, this puts us at a very disadvantageous position.

However, with the development of the Web, new solutions are slowly maturing. Therefore, as a web design and front-end developer, we have no choice but to have a good understanding of the tools or properties we use and be able to use them skillfully.

This also means that for those special tools or attributes, even if they are rarely used, we can still put them to good use when needed.

Today, I will introduce some CSS properties that you may not know before, which are some measurement units such as px and ems, but it is very likely that you have never heard of these before. Let’s take a look.

rem

Start with a unit that is very similar to one we are already familiar with. em is defined as the font size relative to the text within the current object. If you set a font size for the body element, then the em value of any child element of the body is equal to the font-size set by the body.

<body>    <div class="test">Test</div></body>
Copy after login

body {    font-size: 14px;}div {    font-size: 1.2em; // calculated at 14px * 1.2, or 16.8px}
Copy after login

The font size in the div is 1.2em, that is, the div starts from the parent Class elements inherit 1.2 times the font size. Here, the font size of the body is 14px, then the font size of the div is 1.2*14=16.8px.

However, if you use ems to define the font size of nested elements one by one, it will be inconvenient. What's going on? In the following snippet we apply the same CSS as above. Each div inherits the font size from its parent element and gradually increases it.

<body>    <div>        Test <!-- 14 * 1.2 = 16.8px -->        <div>            Test <!-- 16.8 * 1.2 = 20.16px -->            <div>                Test <!-- 20.16 * 1.2 = 24.192px -->            </div>        </div>    </div></body>
Copy after login

While there are some places where this is exactly what we want, often we want to just rely on a single relative unit of measurement. . At this time, you should use rem. The r in rem represents the root element, and its value is the font size set by the root element. In most cases, the root element is html.

html {    font-size: 14px;}div {    font-size: 1.2rem;}
Copy after login

The font sizes of the three nested divs above are all 1.2*14px = 16.8px.

Suitable for grid layout

Rems are not only suitable for font size, but also for grid layout. For example, you can use rem based on the font size of the HTML root element as the size unit for the entire grid layout or UI library, and then use em units in other specific places. This will give you more control over font size and scaling,

.container {    width: 70rem; // 70 * 14px = 980px}
Copy after login

Conceptually, the idea of ​​this method is to let Your interface scales based on your content. However, this does not make sense in all situations.

vh and vw

Responsive web design relies heavily on the percentage rule. However, CSS percentages are not the best solution for every problem. CSS width is relative to the width of the nearest parent element that contains it. What if you want to use the height or width of the viewport instead of the parent element? vh and vw can meet this requirement.

1vh is equal to 1% of the viewport height. For example, if the browser height is 900px, then 1vh = 900*1%=9px. Similarly, if the viewport width is 750px, then 1vw is 7.5px.

They have many uses. For example, we use a very simple method to achieve a box with the same height as the screen using only one line of CSS code.

.slide {    height: 100vh;}
Copy after login

Suppose you want a title with the same width as the screen. You only need to set the font-size unit of the title to vm. Then the title's font-size unit is vm. The font size will automatically scale according to the width of the browser to achieve the effect of synchronizing the font and viewport sizes. Is there any way? !

demo

vmin and vmax

vh and vw are relative to the width and height of the viewport, while vmin and vmax are relative to the viewport height The minimum or maximum value of both width and width. For example, if the height and width of the browser are 700px and 1100px respectively, then 1vmin=7px, 1vmax=11px; if the height and width are 1080px and 800px respectively, then 1vmin=8px, 1vmax=10.8px.

So when are these values ​​needed?

Suppose there is an element and you need to make it always visible on the screen. Just use vmin units for its height and width and give it a value below 100. For example, you can define a square with at least two sides touching the screen:

.box {    height: 100vmin;    width: 100vmin;}
Copy after login

If you want to make this square The frame always covers the entire visible area of ​​the viewport (all four sides always touch the four sides of the screen):

.box {    height: 100vmax;    width: 100vmax;}
Copy after login

Use these together Units can give us a new and interesting way to flexibly utilize the size of our viewport.

ex and ch

The units ex and ch, just like em and rem, depend on the current font and font size. However, unlike em and rem, ex and ch are font-based units of measurement that depend on the font being set.

单位ch通常被定义为数字0的宽度。你可以在Eric Meyers的博客里找到关于它的一些有意思的讨论,例如将一个等宽字体的字母”N”的宽度设置为40ch,那么在另一种类型的字体里它却可以包含40个字母。这个单位的传统用途主要是盲文的排版,但是除此之外,肯定还有可以应用他的地方。

单位ex定义为当前字体的小写x的高度或者1/2的em。很多时候,它是字体的中间标志。

x-height; the height of the lower case x(read more about The Anatomy of Web Typography)

他们有很多的用途,但是大部分用于版式的微调。比如,sup元素(上角标字符),可以利用position:relative;bottom: 1ex;实现,同理,可以实现一个下角标文字。浏览器默认的处理方式是利用上标和下标特定垂直对齐规则,但是如果你想更细粒度更精确得控制,你可以像下面这样做:

sup {    position: relative;    bottom: 1ex;}sub {    position: relative;    bottom: -1ex;}
Copy after login

 

总结

持续关注CSS的发展和扩展是非常重要的,这样你才能熟练运用你工具箱中特定的工具。说不定将来你遇到的某个特殊的问题就需要使用这些复杂的单位来解决。花点时间去阅读新的技术规范,注册订阅一些不错的网站或者资源,类似 cssweekly这样的。 当然不要忘记现在就去注册像Tuts+这样的网站来获取每周的更新,课程,免费教程还有资源!

扩展阅读

More CSS unit goodness.

  • Taking the “Erm..” Out of Ems
  • Taking Ems Even Further
  • Caniuse Viewport units
  • 原文首发:http://www.ido321.com/1301.html

    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)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    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)

    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

    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

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    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.

    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.

    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

    See all articles