ตารางกลางในรูปแบบ HTML

ฉันจะจัด table ให้อยู่ตรงกลางภายใน div โดยใช้ html ได้อย่างไร

ฉันได้วางเนื้อหาของฉันไว้ในแท็ก div และตั้งค่าแอตทริบิวต์ text-align เป็น center ดังต่อไปนี้

text-align: center;

สิ่งนี้ไม่ได้ผล

ด้านล่างนี้คือ html ของฉัน

<html>
    <body>
        <div style="text-align:center">
            <p>
    text test
            </p>
            <table border="1">
                <tr>
                    <td>100</td>
                    <td>200</td>
                    <td>300</td>
                </tr>
                <tr>
                    <td>400</td>
                    <td>500</td>
                    <td>600</td>
                </tr>
            </table>
        </div>
    </body>
</html>

person R.S    schedule 20.11.2012    source แหล่งที่มา


คำตอบ (6)


กำหนดความกว้างให้กับตาราง และตั้งค่าระยะขอบอัตโนมัติในแนวนอน

table {
  width:500px;
  margin: 10px auto;
}
person Kalpesh Patel    schedule 20.11.2012

ลองด้านล่าง

<table style="margin:0px auto; width:500px">
person Murali Murugesan    schedule 20.11.2012

แก้ไขแท็กตารางตามด้านล่าง

 <table border="1" align="center">

แล้วตรวจสอบ

person svk    schedule 20.11.2012
comment
แท็ก align เลิกใช้แล้ว โปรดใช้วิธีการด้านบน @R.S - person Namphibian; 17.09.2013

แทนที่จะเป็น <div style="text-align:center"> คุณสามารถเขียน <div style="width:600px;margin:0 auto">
ซึ่งให้ div ที่มีความกว้าง 600 พิกเซลและจัดกึ่งกลาง div ในองค์ประกอบหลัก

person hellozimi    schedule 20.11.2012

เพิ่ม margin: 0px auto ลงในแท็ก table ของคุณ

person Cdeez    schedule 20.11.2012

คุณสามารถใส่สไตล์ลงในตารางได้

<table border="1" style="margin:0 auto;">

อย่างไรก็ตาม ฉันขอแนะนำให้คุณใช้สไตล์ชีต ไม่ใช่สไตล์อินไลน์

person Claudio Carta    schedule 20.11.2012