method 1: width and height known.
idea:
relative positioning of the parent element
absolute positioning of the child element
left: 50%; top: 50%;
margin-left: negative half width.
margin-top: minus half of the height;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>居中</title>
<style type="text/css">
#box{
width: 400px;
height: 200px;
position: relative;
background: red;
}
#box1{
width: 200px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -50px;
background: green;
}
</style>
</head>
<body>
<div id="box">
<div id="box1">
</div>
</div>
</body>
</html>
method 2: width and height themselves unknown
means that the subbox itself still has a width and a height that it doesn’t know.
relative positioning of parent box
absolute positioning of child box
top, right, bottom, left all 0
margin: auto;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>居中</title>
<style type="text/css">
#box{
width: 800px;
height: 400px;
position: relative;
background: red;
}
#box1{
width: 100px;
height: 50px;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
background: green;
}
</style>
</head>
<body>
<div id="box">
<div id="box1">
</div>
</div>
<script type="text/javascript">
</script>
</body>
</html>
method 3: flex layout
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>垂直居中</title>
<style type="text/css">
.box{
width: 400px;
height: 200px;
background: #f99;
}
.box1{
width: 200px;
height: 100px;
background: green;
}
.center{
display: flex;
justify-content: center;//实现水平居中
align-items: center;//实现垂直居中
}
</style>
</head>
<body>
<div class="box center">
<div class="box1">
</div>
</div>
</body>
</html>
Method 4: translation positioning +transform
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>css3让一个盒子居中</title>
<style type="text/css">
.parent_box{
width: 400px;
height: 200px;
background: red;
position: relative;
}
.child_box{
width: 200px;
height: 100px;
background: #9ff;
position: absolute;
top: 50%;
left: 50%;
transform: translate( -50%,-50%);
}
</style>
</head>
<body>
<div class="parent_box">
<div class="child_box">
</div>
</div>
</body>
</html>
method 5: table-cell layout
parent display: table-cell; vertical-align: middle; Sub-level margin: 0 auto;
** horizontal center
plus a horizontal center: margin-left: 50%; transform: translateX(-50%);
Read More:
- How to center the box horizontally and vertically in HTML
- Several ways to center elements horizontally and vertically
- Invalidation of margin top property of nested box in CSS style box
- How to center your HTML button itself? ——Use the align attribute of center or Div
- CSS remove the blue box after button click
- JavaScript / JS native dynamic introduction of external CSS files and dynamic insertion of CSS code fragments
- Latex: How to Set text Center
- 11. Center text
- Selenium: 8 Ways to Locate Elements
- Introduction to total phase data center
- window.open () several ways to open windows
- When using CSS to write the registration page, we found that the editing effect is not effective
- Four ways to get Django parameters in request
- Several ways for Ubuntu to open command line terminal window
- Learn English together | three ways to realize digital factorial with JavaScript
- Three solutions to flash back of program running result box under vs
- Three ways to get form data in struct2
- Several ways to check the IP address of raspberry pie
- Several ways to view spark task log
- Practice — CSS3 — frame animation to achieve special effects