Élément parent text-align:center; élément enfant : inline-block;
Avantages : bonne compatibilité ;
Inconvénients : nécessité de définir des éléments enfants et des éléments parents en même temps
<!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>
Marge de l'élément enfant :0 auto;
Avantages : Bonne compatibilité
Inconvénients : Nécessité de spécifier la largeur
<!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>
élément enfant {display:table;margin:0 auto;}
Avantages : Il vous suffit de vous configurer
Inconvénients : IE6 et 7 doivent ajuster la 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>
Élément parent : relatif ; élément enfant : absolu ; gauche : 50 % ; marge-gauche : - moitié de la largeur
Avantages : Bonne compatibilité
Inconvénients : nécessité de connaître la largeur de l'élément enfant
<!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>
Élément parent : relatif ; élément enfant : absolu;left:50%;transform:translate(-50%,0);
Avantages : mauvaise compatibilité
Inconvénients : Pas besoin de connaître la largeur de l'élément enfant
<!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>
Boîte flexible : Élément parent : display:flex;justify-content:center;
Avantages : Simple
Inconvénients : Mauvaise compatibilité
<!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的值为父元素高度减去子元素高度的一半。-->
Élément parent : position : relative ; Éléments enfants : positon: absolu; 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>
Élément parent : position : relative ;Élément enfant : position :absolue;top :50 %;margin-top : la moitié de la hauteur de l'élément enfant ;
Élément parent : 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>
Élément parent : display :table-cell;vertical-align:middle;text-align:center ;
élément enfant; 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>
Élément parent : position : relative ;
Élément enfant : position : absolue
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!