1 內部為行內標簽或行內塊級標簽,水平居中設置 text-align: center 垂直居中設置 line-height 為標簽的高度
2.內部為塊級標簽,水平居中設置 margin: 0 auto; 垂直居中設置 line-height == height (讓行高 == 父級標簽的高度)
1 <html> 2 <head lang="en"> 3 <meta charset="UTF-8"> 4 <title> 5 6 </title> 7 8 <!-- 9 水平居中: 10 行內標簽 行內-塊級標簽 text-align: center; 11 塊級標簽 margin: 0 auto; 12 13 垂直居中 14 line-height == height (讓行高 == 標簽的高度) 15 --> 16 <style> 17 div{ 18 background-color: red; 19 width: 400px; 20 height: 250px; 21 line-height: 250px; 22 23 /*水平居中*/ 24 text-align: center; 25 } 26 27 span{ 28 background-color: green; 29 } 30 31 p{ 32 background-color: goldenrod; 33 width: 200px; 34 height: 250px; 35 line-height: 250px; 36 /*margin-left: auto;*/ 37 /*margin-right: auto;*/ 38 margin: 0 auto; 39 } 40 41 button{ 42 width: 200px; 43 height: 200px; 44 } 45 </style> 46 </head> 47 <body> 48 <div> 49 <!--<span>行內標簽</span>--> 50 <!--<button>行內-塊級標簽</button>--> 51 <p>塊級標簽</p> 52 </div> 53 </body> 54 </html>