Table of Contents
em
rem
频率
布局
分辨率
颜色
color name
HEX
RGB
RGBA
HSL
HSLA
transparent
currentColor
函数
生成内容
Home Web Front-end CSS Tutorial Summary of calculations of units in CSS (code example)

Summary of calculations of units in CSS (code example)

Oct 29, 2018 pm 04:06 PM
css css3 html

The content of this article is about summarizing the calculation of units in CSS (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

CSS unit summary

Public part css

body {
  background-color: #000;
  color: skyblue;
  margin: 0;
  padding: 0;
}
body>div>div {
  border: 1px solid cyan;
  padding: 10px;
  margin: 10px;
  font-weight: bolder;
}
.s {
  background-color: #ddd;
  margin: 10px;
  white-space: nowrap;
  color: yellowgreen;
}
Copy after login

Length

px

Pixels, absolute units at the same screen resolution. When the screen resolutions are different, the pixels are scaled proportionally.

/* list1 */
#length .list1s1 {
  width: 100px;
  height: 100px;
}

#length .list1s2 {
  width: 50px;
  height: 50px;
}
Copy after login
<div>
  <p>px</p>
  <p>像素,同一屏幕分辨率下是绝对单位。屏幕分辨率不同时,像素等比缩放。</p>
  <div>100px X 100px</div>
  <div>50px X 50px</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

em

Relative unit, relative to the font size of the parent element

If the parent element font- size is 20px, then 2em is 40px

em can be accurate to 3 decimal places

/* list2 */
#length .list2fa1 {
  font-size: 18px;
}

#length .list2fa2 {
  font-size: 22px;
}

#length .list2s1 {
  width: 5em;
  height: 5em;
}

#length .list2s2 {
  width: 5em;
  height: 5em;
}
Copy after login
<div>
  <p>em</p>
  <p>相对单位,相对于父元素的字体大小</p>
  <p>如果父元素font-size是20px,那么2em就是40px</p>
  <p>em可精确到小数点后3位</p>
  <div>
    <div>5em X 5em</div>
  </div>
  <div>
    <div>5em X 5em</div>
  </div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

rem

Relative unit, relative to the font size of html

/* list3 */
#length .list3s1 {
  width: 5rem;
  height: 5rem;
}
Copy after login
<div>
  <p>rem</p>
  <p>相对单位,相对于html的字体大小</p>
  <div>5rem X 5rem</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

##ex

  • Relative unit, relative to The height of the character, usually half the font height

  • If the text height is not set, it is relative to the browser font size height

/* list4 */
#length .list4s1 {
  width: 10ex;
  height: 1ex;
}

#length .list4s2 {
  width: 10ex;
  height: 2ex;
}

#length .list4fa3 {
  font-size: 20px;
}

#length .list4s3 {
  width: 10ex;
  height: 2ex;
}
Copy after login
<div>
  <p>ex</p>
  <p>相对单位,相对于字符的高度,通常为字体高度的一半</p>
  <p>如果文字高度未设置,则相对于浏览器字体大小高度</p>
  <div>
    <div>10ex X 1ex</div>
  </div>
  <div>
    <div>10ex X 2ex</div>
  </div>
  <div>
    <div>10ex X 2ex</div>
  </div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

ch

  • Relative unit, width of number

/* list5 */

#length .list5s1 {
  width: 3ch;
}

#length .list5s2 {
  width: 3ch;
}

#length .list5fa2 {
  font-size: 20px;
}

#length .list5s3 {
  width: 3ch;
}
Copy after login
rrree

Summary of calculations of units in CSS (code example)

vw/vh

  • Relative unit

  • The viewport is divided into 100 vw horizontally, It is divided into 100 vh vertically

  • For PC, the viewport is the visible area of ​​the browser

  • For mobile , regardless of horizontal or vertical screen, vw always represents the horizontal width, vh always represents the vertical width

<div>
  <p>ch</p>
  <p>相对单位,数字的宽度</p>
  <div>
    <div>111</div>
    <div>111111</div>
  </div>
  <div>
    <div>111</div>
  </div>
</div>
Copy after login
/* list6 */
#length .list6s1 {
  width: 10vw;
  height: 10vh;
}
Copy after login

Summary of calculations of units in CSS (code example)

vmin/vmax

  • Relative unit

  • The smaller of the width and height of the viewport is 100vmin

  • The larger of the width and height of the viewport is 100vmax

<div>
  <p>vw/vh</p>
  <p>相对单位</p>
  <p>视口横向被分割成100个vw,纵向被分割成100个vh</p>
  <p>对于PC端来说,视口是浏览器可视区域</p>
  <p>对于移动端来说,不论横屏还是竖屏,vw始终表示横向宽度,vh始终表示纵向宽度</p>
  <div>10vw X 10vh</div>
</div>
Copy after login
/* list7 */
#length .list7s1 {
  width: 10vmin;
  height: 10vmin;
}

#length .list7s2 {
  width: 10vmax;
  height: 10vmax;
}
Copy after login

Summary of calculations of units in CSS (code example)##cm/mm/q

    Absolute unit, centimeter cm, millimeter unit mm, 1/4 mm q
  • <div>
      <p>vmin/vmax</p>
      <p>相对单位</p>
      <p>视口的宽度和高度中比较小的为100vmin</p>
      <p>视口的宽度和高度中比较大的为100vmax</p>
      <div>10vmin X 10vmin</div>
      <div>10vmax X 10vmax</div>
    </div>
    Copy after login
    rrree

Summary of calculations of units in CSS (code example) in

    Absolute units, inches in
  • /* list8 */
    #length .list8s1 {
      width: 3cm;
      height: 3cm;
    }
    
    #length .list8s2 {
      width: 30mm;
      height: 30mm;
    }
    
    #length .list8s3 {
      width: 120q;
      height: 120q;
    }
    Copy after login
    <div>
      <p>cm/mm/q</p>
      <p>绝对单位,厘米cm,毫米单位mm,1/4毫米q</p>
      <div>3cm X 3cm</div>
      <div>30mm X 30mm</div>
      <div>120q X 120q</div>
    </div>
    Copy after login

Summary of calculations of units in CSS (code example)##pt/pc

Absolute unit, click pt, send card pc
  • /* list9 */
    #length .list9s1 {
      width: 10in;
      height: 10in;
    }
    Copy after login
    <div>
      <p>in</p>
      <p>绝对单位,英寸in</p>
      <div>10in X 10in</div>
    </div>
    Copy after login

Summary of calculations of units in CSS (code example)%

%
  • Relative value, percentage, relative to parent element
  • /* list10 */
    #length .list10s1 {
      width: 5pt;
      height: 5pt;
    }
    
    #length .list10s2 {
      width: 50pt;
      height: 50pt;
    }
    
    #length .list10s3 {
      width: 5pc;
      height: 5pc;
    }
    Copy after login
    <div>
      <p>pt/pc</p>
      <p>绝对单位,点pt,派卡pc</p>
      <div>5pt X 5pt</div>
      <div>50pt X 50pt</div>
      <div>5pc X 5pc</div>
    </div>
    Copy after login

Summary of calculations of units in CSS (code example)angle

deg/grad/rad/turn
  • Degree deg, gradient grad, radian rad , turn
  • A circle 360deg, 400grad, 2πrad, 1turn
  • /* list11 */
    #length .list11f1 {
      width: 100px;
      height: 100px;
    }
    
    #length .list11s1 {
      width: 80%;
      height: 70%;
    }
    
    #length .list11f2 {
      width: 80px;
      height: 70px;
    }
    
    #length .list11s2 {
      width: 80%;
      height: 70%;
    }
    Copy after login
    <div>
      <p>%</p>
      <p>相对数值,百分比,相对父元素</p>
      <div>
        <div>80% X 70%</div>
      </div>
      <div>
        <div>80% X 70%</div>
      </div>
    </div>
    Copy after login

Summary of calculations of units in CSS (code example)Time

s/ms
  • Seconds, milliseconds ms
  • is used to set Determine animation execution time

频率

  • Hz/kHz

  • 用于设定声音元素频率

布局

  • fr

  • 用于分配一定长度内的剩余空间

/* list1 */
#layout-specific .list1fa1 {
  width: 100px;
  height: 100px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

#layout-specific .list1fa1 p {
  border: 5px solid skyblue;
}
Copy after login
<div>
  <p>fr</p>
  <p>用于分配一定长度内的剩余空间</p>
  <div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

Summary of calculations of units in CSS (code example)

分辨率

  • dpi/dpcm/dppx

  • 每英寸包含点的数量dpi

  • 每厘米包含点的数量dpcm

  • 每像素包含点的数量dppx

颜色

color name

  • 使用颜色关键字指定颜色

/* list1 */
#color .list1s1 {
  width: 100px;
  height: 100px;
  background-color: darkseagreen;
}
#color .list1s2 {
  width: 100px;
  height: 100px;
  background-color: salmon;
}
Copy after login
<div>
  <p>color name</p>
  <p>使用颜色关键字指定颜色</p>
  <div>darkseagreen</div>
  <div>salmon</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

HEX

  • 使用十六进制整数指定颜色

/* list2 */
#color .list2s1 {
  width: 100px;
  height: 100px;
  background-color: #f1d2b3;
}
#color .list2s2 {
  width: 100px;
  height: 100px;
  background-color: #a3c2e1;
}
Copy after login
<div>
  <p>HEX</p>
  <p>使用十六进制整数指定颜色</p>
  <div>#f1d2b3</div>
  <div>#a3c2e1</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

RGB

  • R:red;G:green;B:blue;

  • 颜色的比例指定颜色

  • 值在0到255之间

/* list3 */
#color .list3s1 {
  width: 100px;
  height: 100px;
  background-color: rgb(111,222,123);
}
#color .list3s2 {
  width: 100px;
  height: 100px;
  background-color: rgb(0,1,2);
}
Copy after login
<div>
  <p>RGB</p>
  <p>R:red;G:green;B:blue;</p>
  <p>颜色的比例指定颜色</p>
  <p>值在0到255之间</p>
  <div>rgb(111,222,123)</div>
  <div>rgb(0,1,2)</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

RGBA

  • R:red;G:green;B:blue;A:alpha;

  • 颜色的比例指定颜色,alpna指定透明度

  • 值在0到255之间,alpha的值在0到1之间,0.2可以用.2表示

/* list4 */
#color .list4s1 {
  width: 100px;
  height: 100px;
  background-color: rgba(111,222,123,0.2);
}
#color .list4s2 {
  width: 100px;
  height: 100px;
  background-color: rgba(111,222,123,.2);
}
Copy after login
<div>
  <p>RGBA</p>
  <p>R:red;G:green;B:blue;A:alpha;</p>
  <p>颜色的比例指定颜色,alpna指定透明度</p>
  <p>值在0到255之间,alpha的值在0到1之间,0.2可以用.2表示</p>
  <div>rgba(111,222,123,0.2)</div>
  <div>rgba(111,222,123,.2)</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

HSL

  • H:hue色调,0或者360表示红色,120表示绿色,240表示蓝色

  • S:saturation饱和度,取值在0.0%到100.0%之间

  • L:lightness亮度,取值在0.0%到100.0%之间

/* list5 */
#color .list5s1 {
  width: 100px;
  height: 100px;
  background-color: hsl(280, 50%, 60%);
}
#color .list5s2 {
  width: 100px;
  height: 100px;
  background-color: hsl(50, 50%, 60%);
}
Copy after login
<div>
  <p>HSL</p>
  <p>H:hue色调,0或者360表示红色,120表示绿色,240表示蓝色</p>
  <p>S:saturation饱和度,取值在0.0%到100.0%之间</p>
  <p>L:lightness亮度,取值在0.0%到100.0%之间</p>
  <div>hsl(280, 50%, 60%)</div>
  <div>hsl(50, 50%, 60%)</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

HSLA

  • H:hue色调,0或者360表示红色,120表示绿色,240表示蓝色

  • S:saturation饱和度,取值在0.0%到100.0%之间

  • L:lightness亮度,取值在0.0%到100.0%之间

  • A:alpha透明度

/* list6 */
#color .list6s1 {
  width: 100px;
  height: 100px;
  background-color: hsla(280, 50%, 60%,0.6);
}
#color .list6s2 {
  width: 100px;
  height: 100px;
  background-color: hsla(50, 50%, 60%,.6);
}
Copy after login
<div>
  <p>HSLA</p>
  <p>H:hue色调,0或者360表示红色,120表示绿色,240表示蓝色</p>
  <p>S:saturation饱和度,取值在0.0%到100.0%之间</p>
  <p>L:lightness亮度,取值在0.0%到100.0%之间</p>
  <p>A:alpha透明度</p>
  <div>hsla(280, 50%, 60%,0.6)</div>
  <div>hsla(50, 50%, 60%,.6)</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

transparent

  • 全黑透明色,即rgba(0,0,0,0)

/* list7 */
#color .list7s1 {
  width: 100px;
  height: 100px;
  background-color: transparent;
}
Copy after login
<div>
  <p>transparent</p>
  <p>全黑透明色,即rgba(0,0,0,0)</p>
  <div>transparent</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

currentColor

  • color具有继承性,currentColor相当于继承color颜色

/* list8 */
#color .list8s1 {
  width: 100px;
  height: 100px;
  background-color: currentColor;
}
Copy after login
<div>
  <p>currentColor</p>
  <p>color具有继承性,currentColor相当于继承color颜色</p>
  <div>currentColor</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

函数

  • calc()

  • calc(四则运算)

  • 用于动态计算长度值,运算符前后要加空格

/* list1 */
#function .list1s1 {
  width: calc(50% - 20rem);
  height: calc(20em - 200px);
}

#function .list1s2 {
  width: calc(20rem - 150px);
  height: calc(200px - 6em);
}
Copy after login
<div>
  <p>calc()</p>
  <p>calc(四则运算)</p>
  <p>用于动态计算长度值,运算符前后要加空格</p>
  <div>calc(50% - 20rem) X calc(20em - 200px)</div>
  <div>calc(20rem - 150px) X calc(200px - 6em)</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

生成内容

  • attr()

  • 用于content属性,取当前元素的属性值

  • 可以拼接字符串

/* list1 */
#content .list1s1 {
  width: 100px;
  height: 100px;
}
#content .list1s1:before {
  content: "("attr(datamsgb)")";
  font-size: 12px;
}
#content .list1s1:after {
  content: attr(datamsga);
  font-size: 14px;
}
Copy after login
<div>
  <p>attr()</p>
  <p>用于content属性,取当前元素的属性值</p>
  <p>可以拼接字符串</p>
  <div>实际元素</div>
</div>
Copy after login

Summary of calculations of units in CSS (code example)

The above is the detailed content of Summary of calculations of units in CSS (code example). For more information, please follow other related articles on the PHP Chinese website!

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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

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.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

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.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

See all articles