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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    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)

    Hot Topics

    Java Tutorial
    1671
    14
    PHP Tutorial
    1276
    29
    C# Tutorial
    1256
    24
    HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

    HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

    HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

    The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

    See all articles