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

WBOY
WBOYOriginal
2016-06-24 11:52:521009browse

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>

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

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>

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;}

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}

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;}

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;}

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;}

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;}

 

总结

持续关注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:
    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