#CSS The most commonly used length units are pixels (px), percentage (%), rem, em, vh, vw, pt, cm, mm, in, etc. These units can be used to set the element's width, height, border size, font size, etc.
- The pixel (px) unit is one of the most commonly used units. It is a fixed length unit calculated relative to the physical pixels of the electronic screen. Here is a code example:
div { width: 200px; height: 100px; font-size: 16px; border: 1px solid #000; }
In the code example above, the width
and height
properties use pixel units to define the width and width of the div element. The height, font-size
attribute uses pixel units to define the font size, and the width of the border
attribute also uses pixel units.
- Percent (%) units are calculated relative to the size of the parent element. Here is a code example:
div { width: 50%; height: 50%; }
In the above code example, the width and height of the div element are set to 50% of the dimensions of the parent element.
- rem units are calculated relative to the font size of the root element (i.e. the html element). Here is a code example:
html { font-size: 16px; } div { width: 10rem; height: 5rem; font-size: 1.2rem; }
In the above code example, the font size of the root element is set to 16px, the width and height of the div element are 10 times the font size of the root element, and the font size Is 1.2 times the font size of the root element.
- em units are calculated relative to the font size of the element itself. Here is a code example:
div { font-size: 16px; width: 2em; height: 2em; }
In the above code example, the width and height of the div element are set to twice the font size of the element itself.
In addition to the above units, there are some other length units:
- vh (viewport height) represents a percentage relative to the viewport height.
- vw (viewport width) represents the percentage relative to the viewport width.
- pt (point) is the printing unit, 1pt is approximately equal to 1/72 inch.
- cm (centimeter) means centimeter, 1cm is approximately equal to 96px/2.54.
- mm (millimeter) means millimeter, 1mm is approximately equal to 96px/25.4.
- in (inch) means inches, 1in is approximately equal to 96px.
In short, it is necessary to choose the appropriate length unit in different situations to achieve the best effect.
The above is the detailed content of What are the commonly used CSS length units?. For more information, please follow other related articles on the PHP Chinese website!

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)
