ICG Designer ICG Designer - 4 months ago 9
CSS Question

How do I center a <table>?

I want my table centered so I tried this:

<table align=center>HEEHHE</table>
which doesn't work.

My code:

<?php
$servername = "***:3306";
$username = "**";
$password = "!!!!";
$dbname = "***";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT balance FROM tbl_users WHERE userID = 8";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table align="center"><tr><th>Aktuell im Jackpot:</tr></th>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["balance"]."</tr></td>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

Answer

First of all, your code is wrong:

echo "<table align="center"><tr><th>Aktuell im Jackpot:</tr></th>"; // wrong code

You should escape quotation marks: \"center\"; Or use single quotes like echo '<table style="margin: 0 auto;">...</table>';.

Well, it has a better way you stylize things, but I'm going straight to the point: instead align="center", use style="margin: 0 auto; width: 500px;".

If you need to center in vertical, this guide is good for you: https://css-tricks.com/centering-css-complete-guide/