I am trying to display a table inline within a p
element. The result should look similar to this:
*-----------------*
Text before. | Cell 1 | Cell 2 | Text after.
*-----------------*
As shown, the table needs to be centered vertically with the text. The table might have multiple rows.
The following works (I am using inline css in this example to simplify the post):
<html lang="en">
<head>
</head>
<body>
<p>
Text before.
<table style="display: inline-block; vertical-align: middle;">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>
Text after.
</p>
</body>
</html>
HOWEVER, if I add <!DOCTYPE html>
to the top of the file, the table appears below the "Text before."
There must be a difference between quirks mode (when the doc type isn’t specified) and when the doc type is specified, but I don’t know how to resolve it.
Thanks!
3
Answers
The difference between when no
<!DOCTYPE html>
is present and when<!DOCTYPE html>
is present. In quirks mode, browsers attempt to mimic the behavior of older versions of HTML, leading to differences in rendering.This keeps the table vertically centered with the text.
Frankly, a
table
is not indicated here. Aninline-grid
container would be ideal.Rows aren’t necessary here but I’ve included them for clarity but it does require
subgrid
for optimisation.BUT, as I said, rows aren’t really required…
You can achieve the same, but need a bit of more css with it. And I think we need to replace the
<p>
with a<div>
tag.