水平居中佈局 優點:相容性好; 不足:需要同時設置子元素和父元素 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, ini ...
水平居中佈局
- 父元素text-align:center;子元素: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>水平居中佈局</title> <style> *{ margin: 0; padding: 0; } .parent { width: 100%; background: green; text-align: center; } .child { display: inline-block; width: 800px; height: 100px; background: blue; } </style> </head> <body> <div class="parent"> <div class="child">1</div> <div class="child">2</div> <div class="child">3</div> <div class="child">4</div> <div class="child">5</div> <div class="child">6</div> </div> </body> </html>