<!DOCTYPE html> <html> <head> <style> #all{ margin:0px auto; width:500px; /* 必須制定寬度 */ height:200px; background-color:blue; } </style> </head> <body> <div id="all"> <div> </body> </html>
<!DOCTYPE html> <html> <head> <style> #all{ margin:0px auto; width:500px; /* 必須制定寬度 */ height:200px; background-color:blue; } #string{ margin:0px auto; width:300px; height:100px; background-color:red; text-align:center; /* 文字居中 */ font-size:32px; /* 文字大小 */ color:white; /* 文字顏色 */ } </style> </head> <body> <div id="all"> <div id="string">center<div> <div> </body> </html>
text-align:center使得html
<!DOCTYPE html> <html> <head> <style> #all{ margin:0px auto; width:500px; /* 必須制定寬度 */ height:200px; background-color:blue; } #string{ margin:0px auto; width:300px; /* 必須制定寬度 */ height:100px; background-color:red; text-align:center; /* 文字居中 */ font-size:32px; /* 文字大小 */ color:white; /* 文字顏色 */ } #image{ margin:0px auto; width:300px; /* 必須制定寬度 */ background-color:white; text-align:center; /* 圖像居中 */ padding-top:20px; /* 圖像上填充 */ padding-bottom:20px; /* 圖像下填充 */ } </style> </head> <body> <div id="all"> <div id="string">center</div> <div id="image"><img src="loader.gif"></div> </div> </body> </html>
<!DOCTYPE html> <html> <head> <style> #all{ margin:0px auto; width:500px; /* 必須制定寬度 */ height:200px; background-color:blue; } </style> </head> <body> <div id="all"> <!-- 表格在父窗口中居中 --> <table width="80%" align="center" border="1"> <tbody> <!-- 單元格居中 --> <tr height="50px"><td align="center">文字居中</td></tr> <tr height="50px"><td align="center"><img src="loader.gif"></td></tr> </tbody> </table> </div> </body> </html>
<!DOCTYPE html> <html> <head> <style> #all{ margin:0px auto; width:500px; /* 必須制定寬度 */ height:200px; background-color:blue; } /* 設置邊框 */ table, th, td{ border: 1px solid black; } /* 設置table位置 */ table{ margin:0px auto; /* 效果等同 <tabel align="center">*/ width:80% /* 必須制定寬度 */ } /* 單元格對齊 */ td{ text-align:center; } </style> </head> <body> <div id="all"> <table> <tbody> <tr height="50px"><td>文字居中</td></tr> <tr height="50px"><td><img src="loader.gif"></td></tr> </tbody> </table> </div> </body> </html>