Nice to meet you again !
When we create responsive websites for a variety of devices , Know the right CSS The unit is very important . But before delving into decision-making , Let's classify them first to understand their purpose .
How to determine the of responsive websites CSS Company ?
1. Absolute units
- px - Pixels
- pt - spot
- pc - Picas
- in - Inch
- cm - centimeter
- mm - mm
2. Percentage units
- percentage % Company
3. Relative units
-
Relative to font size
- em
- rem - root em
-
Relative to the viewing port / file
- vw
- vh
- vmax
- vmin
Learn the most common units here
px Company
Absolute pixel units are only used on the screen ( Interface ), The remaining units are used for printing .px Unit is not a good choice , In fact, this is not for scaling . Whatever screen size you choose ,px The dimensions of the units are fixed . That's why borders are always preferred px Unit reasons , Because the border remains fixed on all screen sizes .
% Company
This is used to set the width of the element , It is always relative to the size of its immediate parent element . If there is no defined parent , By default body Be considered a parent .
Consider a width of 500px Box , There's a h1 Elements
.box{
width: 500px;
border: 1px solid crimson;
padding: 10px;
}
h1{
width: 50%;
border: 1px solid;
}
effect
If no parent is defined , that root Will be treated as the default parent .
em Company
em The unit is always relative to the font size of its immediate parent .1em ==
A parent font size
. If not covered , The default font size is 16px, Suppose the font size in the parent element is 48px, Then in the child element is 1em == 48px.
h1{
font-size: 1em; /* now 1em == 16px */
}
effect
.container{
font-size: 48px;
/* or 3em, Because the default font size is 16px & Its parent element is body, therefore 3*16px Namely 48px */
}
h1{
font-size: 1em; /* now 1em == 48px */
}
effect
We can use this unit for margins and padding , Because it allows us to use flexible spacing around the box according to the font size of the element . Elements font-size It will vary according to the size of the device , Therefore, the spacing around the elements will also change separately .
rem Company
r representative root em, And em Different , It is always relative to the root font size , No matter what font size its next parent element has . If the root has redefined the font size , Such as 60px that 1rem == 60px .
html{
font-size: 60px;
}
.container{
font-size: 16px;
}
h1{
font-size: 1rem;
}
effect
vw Company
vw representative viewprot width( Viewport width ), It means vw Always relative to the root width 1%, Independent of the width of the parent element . therefore , If
1vw == 1%
that100vw == 100%
Viewport width .
Let's consider the following example , The width of one of the children is relative to the size of the parent , The width of the other subitem is relative to the root .
.container{
width: 600px;
border: 2px solid black;
text-align: center;
font-size: 20px;
}
.box1{
width: 100%;
background: skyblue;
}
.box2{
width: 70vw;
background: pink;
}
effect
* vh Company
vh representative viewprot height ( Viewport height ), Such as vw It's also relative to the root / Document 1% Height . Let's consider the following example , The height of one of the children is related to the size of the parent , The height of another child is related to the root .
.container{
border: 2px solid;
font-size: 40px;
width: 800px;
height: 400px;
display: flex;
text-align: center;
margin: 0 auto;
}
.box1{
height: 100%;
width: 50%;
background: skyblue;
}
.box2{
height: 100vh;
background: pink;
width: 50%;
}
effect
wuhu ! take off !
Summarize
px Units are often used for borders .
% The width of the unit relative to the parent .
em The margin and padding of the unit relative to the font size of the element .
rem The font size of the unit relative to the root .
vw and vh Represents the width and height relative to the root .
These are 6 individual css unit , They are most commonly used to make websites responsive .
I've been writing a technology blog for a long time , And mainly through CSDN publish , This is my article Of responsive websites CSS Unit tutorial . I like to share technology and happiness through articles . You can visit my blog : https://haiyong.blog.csdn.net/ To learn more . I hope you will like !
You are welcome to put forward your opinions and suggestions in the comment area !
If you really learn something new from this article , Like it , Collect it and share it with your friends . Last , Don't forget or support .