Parent element text-align:center;Child element:inline-block;
Advantages: good compatibility;
Disadvantages: need to set child elements and parent elements at the same time
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; text-align: center; } .child { display: inline-block; width: 300px; height: 100px; background: blue; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Sub-element margin:0 auto;
Advantages: good compatibility
Disadvantages: required Specify width
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; } .child { width: 300px; height: 100px; background: blue; margin:0 auto; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Child elements{display:table;margin:0 auto;}
Advantages: Only need to set itself
Disadvantages: IE6,7 need to adjust the structure
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; } .child { width: 300px; height: 100px; background: blue; margin:0 auto; display:table;margin:0 auto;s } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Parent Element: relative; child element: absolute; left: 50%; margin-left: - half of the width
Advantages: Good compatibility
Disadvantages: Need to know the width of the child element
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; position: relative; top: 0; left: 0; } .child { width: 300px; height: 100px; background: blue; position: absolute; top: 0; left: 50%; margin-left: -150px; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Parent element: relative; child element: absolute; left:50%; transform :translate(-50%,0);
Advantages: Poor compatibility
Disadvantages: No need to know the subtitles The width of the element
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; position: relative; top: 0; left: 0; } .child { width: 300px; height: 100px; background: blue; position: absolute; top: 0; left: 50%; transform: translate(-50%,0); } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Flexible box: Parent element: display:flex;justify-content:center;
Advantages: Simple
Disadvantages: Poor compatibility
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; display: flex; justify-content: center; } .child { width: 300px; height: 100px; background: blue; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
vertical-align:center;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>水平居中1</title> <style> .parent { width: 500px; height: 400px; background: red; display:table-cell; vertical-align:middle; } .child { width: 300px; height: 100px; background: blue; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>垂直居中1</title> <style> .parent { width: 500px; height: 400px; background: red; vertical-align:middle; line-height:450px; } .child { width: 300px; height: 100px; background: blue; display:inline-block; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html> <!-- 这种方法需要知道父元素和子元素的高度,父元素的line-height的值为父元素高度减去子元素高度的一半。-->
Parent element: position:relative;Child element: positon:absolute;top:50%;transform:translate( 0,-50%);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>垂直居中1</title> <style> .parent { width: 500px; height: 400px; background: red; position: relative; top: 0; left: 0; } .child { width: 300px; height: 100px; background: blue; position: absolute; top: 50%; left:0; transform: translate(0,-50%); } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Parent element: position:relative; Child element: positon:absolute;top:50%;margin-top:- Half the height of the child element;
Parent element: display:flex;align-items:center;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>垂直居中1</title> <style> .parent { width: 500px; height: 400px; background: red; display: flex; align-items: center; } .child { width: 300px; height: 100px; background: blue; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Parent element: display:table-cell;vertical-align:middle;text-align:center;
Child element; display:inline-block;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>垂直居中1</title> <style> .parent { width: 500px; height: 400px; background: red; display:table-cell; vertical-align:middle; text-align:center; } .child { width: 300px; height: 100px; background: blue; display: inline-block; } </style> </head> <body> <p class="parent"> <p class="child"></p> </p> </body> </html>
Parent element: position:relative;
Child element: position:absolute
The above is the detailed content of Summary and sharing of CSS layout techniques. For more information, please follow other related articles on the PHP Chinese website!