One or two box models
- They are: W3C box model, IE box model
- difference:
- 1. W3C box model, width (width) refers to the width of the content part. In the IE box model, the width (width) represents the width of the three parts of content+padding+border
- 2. Switch box model, W3C box model:
box-sizing: content-box
, IE box model:box-sizing: border-box
2. There are several ways of horizontal and vertical centering
method one:
- Use absolute positioning (
position: absolute
), then (top: 50%;left: 50%;
), - Then remove the width and height of the margin, and half the width and height of the box model (
margin-top: -150px;margin-left: -150px;
) can be vertically centered
- advantages: good compatibility, disadvantages: you need to know the width and height of the box model, otherwise you don't know the width and height of the margin
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css水平垂直居中</title>
</head>
<body>
<div class="test"></div>
</body>
<style>
.test {
width: 300px;
height: 300px;
background-color: antiquewhite;
position: absolute;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -150px;
}
</style>
</html>
Method Two:
- In order to solve the problem of width and height, CSS3 has introduced
transform: translate(-50%, -50%)
instead of margin, and the percentage of offset is relative to its own size, so there is no need to calculate the width and height of margin~
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css水平垂直居中</title>
</head>
<body>
<div class="test"></div>
</body>
<style>
.test {
width: 300px;
height: 300px;
background-color: antiquewhite;
position: absolute;
top: 50%;
left: 50%;
/* margin-top: -150px;
margin-left: -150px; */
transform: translate(-50%, -50%);
}
</style>
</html>
Method three:
- Through absolute positioning (
position: absolute
), then settop: 0;left: 0;right: 0;bottom: 0;
all the top, bottom, left and right ( ) to 0, and then use (margin: auto
) to center horizontally and vertically
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css水平垂直居中</title>
</head>
<body>
<div class="test"></div>
</body>
<style>
.test {
width: 300px;
height: 300px;
background-color: antiquewhite;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</html>
Method four:
- Through the body flexible layout (
display: flex
), the element that defines the body is vertically centered (align-items: center
), and the element that defines the body is centered horizontally (justify-content: center
)
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css水平垂直居中</title>
</head>
<body>
<div class="test"></div>
</body>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
/*垂直居中*/
align-items: center;
/*水平居中*/
justify-content: center;
}
.test {
background-color: antiquewhite;
width: 300px;
height: 300px;
}
</style>
</html>
Method five:
- By relative positioning (
position: relative
), then horizontal centering (margin: 0 auto
), then vertical centering (top: 50%;transform: translateY(-50%)
)
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css水平垂直居中</title>
</head>
<body>
<div class="test"></div>
</body>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.test {
width: 300px;
height: 300px;
background-color: antiquewhite;
position: relative;
/*水平居中*/
margin: 0 auto;
top: 50%;
transform: translateY(-50%);
}
</style>
</html>