Table of Contents
前面的话
绝对长度单位
像素px(pixels)
英寸in(inches)
厘米cm(centimeters)
毫米mm(millimeters)
1/4毫米q(quarter-millimeters)
点pt(points)
派卡pc(picas)
字体相关相对长度单位
em
rem
ex
ch
视口相关相对长度单位
vh
vw
vmin
vmax
Home Web Front-end HTML Tutorial 深入理解CSS中的长度单位_html/css_WEB-ITnose

深入理解CSS中的长度单位_html/css_WEB-ITnose

Jun 24, 2016 am 11:18 AM

× 目录 [1]px [2]in [3]cm [4]mm [5]q [6]pt [7]pc [8]em [9]rem [10]ex [11]ch [12]vh [13]vw [14]vmin [15]vmax

前面的话

  本文分为绝对长度单位和相对长度单位来介绍CSS中的长度单位的主要知识

 

绝对长度单位

  绝对长度单位代表一个物理测量

 

像素px(pixels)

  在web上,像素px是典型的度量单位,很多其他长度单位直接映射成像素。最终,他们被按照像素处理

 

英寸in(inches)

  1in = 2.54cm = 96px  

 

厘米cm(centimeters)

  1cm = 10mm = 96px/2.54 = 37.8px

 

毫米mm(millimeters)

  1mm = 0.1cm = 3.78px

 

1/4毫米q(quarter-millimeters)

  1q = 1/4mm = 0.945px

 

点pt(points)

  1pt = 1/72in = =0.0139in = 1/72*2.54cm = 1/72*96px = 1.33px

 

派卡pc(picas)

  1pc = 12pt = 1/6in = 1/6*96px = 16px

 

字体相关相对长度单位

  em、ex、ch、rem是字体相关的相对长度单位

 

em

  em表示元素的font-size属性的计算值,如果用于font-size属性本身,相对于父元素的font-size;若用于其他属性,相对于本身元素的font-size

<style>.box{font-size: 20px;}.in{    /* 相对于父元素,所以2*2px=40px */    font-size: 2em;    /* 相对于本身元素,所以5*40px=200px */    height: 5em;    /* 10*40px=400px */    width: 10em;    background-color: lightblue;}</style>
Copy after login

<div class="box">    <div class="in">测试文字</div>    </div>
Copy after login

rem

  rem是相对于根元素html的font-size属性的计算值

  兼容性: IE8-不支持

<style>/* 浏览器默认字体大小为16px,则2*16=32px,所以根元素字体大小为32px */html{font-size: 2rem;}/* 2*32=64px */.box{font-size: 2rem;}.in{    /* 1*32=32px */    font-size: 1rem;    /* 1*32=32px */    border-left: 1rem solid black;    /* 4*32=128px */    height: 4rem;    /* 6*32=192px */    width: 6rem;    background-color: lightblue;}</style>
Copy after login

<div class="box">    <div class="in" id="test">测试文字</div>    </div>
Copy after login

ex

  ex是指所用字体中小写x的高度。但不同字体x的高度可能不同。实际上,很多浏览器取em值一半作为ex值

  [注意]ex在实际中常用于微调

<style>.box{font-size: 20px;}.in{    font-size: 1ex;    border-left: 1ex solid black;    height: 10ex;    width: 20ex;    background-color: lightblue;}</style>
Copy after login

<div class="box">    <div class="in" id="test">测试文字</div>    </div>
Copy after login
Copy after login

<script>var aBtns = document.getElementsByTagName('button');for(var i = 0; i < aBtns.length; i++ ){    aBtns[i].onclick = function(){        test.style.fontFamily = this.innerHTML;    }}    </script>
Copy after login
Copy after login

ch

  ch与ex类似,被定义为数字0的宽度。当无法确定数字0宽度时,取em值的一半作为ch值

  兼容性: IE8-不支持

  [注意]ch在实际中主要用于盲文排版

<style>.box{font-size: 20px;}.in{    font-size: 1ch;    border-left: 1ch solid black;    height: 10ch;    width: 20ch;    background-color: lightblue;}</style>
Copy after login

<div class="box">    <div class="in" id="test">测试文字</div>    </div>
Copy after login
Copy after login

<script>var aBtns = document.getElementsByTagName('button');for(var i = 0; i < aBtns.length; i++ ){    aBtns[i].onclick = function(){        test.style.fontFamily = this.innerHTML;    }}    </script>
Copy after login
Copy after login

视口相关相对长度单位

  视口相关的长度值相对于初始包含块的大小。当初始包含块的宽高变化时,他们都会相应地缩放。然而,当根元素的overflow值为auto时,任何滚动条会假定不存在。

  关于视口相关的单位有vh、vw、vmin、vmax4个单位

  兼容性:IE8-不支持,IOS7.1-不支持,android4.3-不支持(对于vmax,所有IE浏览器都不支持)

  [注意]黑莓错误的将其相对于视觉视口来计算;而safari奇怪地相对于html元素来计算,如果html中增加了内容,这两个单位也会发生变化

vh

  布局视口高度的 1/100

vw

  布局视口宽度的 1/100

<style>body{margin: 0;}.box{    /* 实现与屏幕等高的效果 */    height: 100vh;    background-color: lightblue;}    </style>
Copy after login

<div class="box"></div>
Copy after login

vmin

  布局视口高度和宽度之间的最小值的 1/100

/*类似于contain效果*/.box{    height: 100vmin;    width: 100vmin;}
Copy after login

vmax

  布局视口高度和宽度之间的最大值的 1/100

/*类似于cover效果*/.box{    height: 100vmax;    width: 100vmax;}    
Copy after login

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 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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles