CSS unit property guide: em, rem, px and vw/vh
When writing CSS styles, it is very important to choose the appropriate unit property. This article will introduce several commonly used unit attributes: em, rem, px and vw/vh, and provide specific code examples.
Code example:
.parent { font-size: 16px; } .child { font-size: 1em; /* 等同于16px */ width: 2em; /* 等同于32px */ height: 3em; /* 等同于48px */ }
Code example:
html { font-size: 16px; } .child { font-size: 1rem; /* 等同于16px */ width: 2rem; /* 等同于32px */ height: 3rem; /* 等同于48px */ }
Code example:
.child { font-size: 16px; width: 32px; height: 48px; }
Code example:
.child { font-size: 5vw; /* 视口宽度的5% */ width: 30vw; /* 视口宽度的30% */ height: 40vh; /* 视口高度的40% */ }
Summary:
Choosing appropriate unit attributes is very important for writing CSS styles that are flexible and adaptable to different screens. em and rem are suitable for relative sizes, px is suitable for fixed sizes, and vw/vh is suitable for adaptive sizes. Choosing the appropriate unit attributes according to the specific situation can make the web page display better on different devices and screens.
The above is a guide about CSS unit properties, I hope it will be helpful to you.
The above is the detailed content of Guide to CSS unit properties: em, rem, px and vw/vh. For more information, please follow other related articles on the PHP Chinese website!