Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the link appear before the table when it's after it in the HTML?

Tags:

html

    <!DOCTYPE html>
<html>
    <head>
        <title>Firecore's Profile</title>
    </head>
    <body>
        <div style="background-color:#DC143C; text-align:center">
            <p><h1>Firecore Starblade</h1></p>
        </div>
            <p style="text-align:center">
            <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRz83KYRPqRKFLb4rtJdS6JzGL-OZ-OhSVE5nOz1Q7CZ3seOe1-"; width="10%" ; height="10%">
        </p>
        <div>
            <table border="1px" align="center">
                <thead style="background-color:#DC143C">
                    <tr>
                        <th>Attributes</th>
                        <th>HP</th>
                        <th>AP</th>
                        <th>Dex</th>
                        <th>Str</th>
                        <th>Int</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td style="background-color:#DC143C"><strong>Value</strong></td>
                        <td>100</td>
                        <td>50</td>
                        <td>10</td>
                        <td>10</td>
                        <td>10</td>
                    </tr>
                </tbody
            </table>
        </div>
        <div style="text-align:center">
            <a href="firecoreMain.html">Back</a>
        </div>
    </body>
</html>

Just starting out learning HTML. Making a practice site and ran into this problem. Tried googling it but unable to locate the cause. Not understanding why the "Back" HyperLink is showing above the table instead of below. I thought maybe it was because of the uses of p and div elements but i tried different combos with no luck. Thanks for your help in advance.

like image 647
Firecore Avatar asked Oct 16 '25 03:10

Firecore


1 Answers

This seems like a simple problem, but it's actually really interesting. On a simple level, the problem is that the </tbody> tag is missing the closing >. Put it in and your code works.

But the more interesting question is why?

Well, it turns out that the problem is that you are in fact not closing the table tag. Your missing > essentially means you have a tag that looks like this: </tbody </table>. That's one tag and the browser will think "ah, we're closing the tbody and we've got some other stuff that doesn't make sense here so we're going to ignore it."

So you never actually close the table tag. This means that you now have some invalid markup in your table. To be precise, the following code is now part of your table's code:

        </div>
        <div style="text-align:center">
            <a href="firecoreMain.html">Back</a>
        </div>
    </body>
</html>

This is handled according to the rather obscure rules listed here in the HTML5 specification section "Unexpected markup in tables". The basic meaning of all that specification (which the W3C confesses is "for historical reasons, especially strange") is that all the unexpected/invalid markup inside the table gets put before the beginning of the table.

That's why your link does the surprising thing of moving before the table. A simple mistake (the missing >) has ended up sending your browser down a whole rabbit warren of mistaken parsing.

like image 110
lonesomeday Avatar answered Oct 17 '25 17:10

lonesomeday