TD 文本内容未垂直对齐
我正在开发一个小型井字游戏,但在垂直对齐 TD 的内容时遇到问题。正如您在下面的屏幕截图中看到的,我将垂直对齐属性应用于选定的 TD,但文本仍然没有垂直居中,并且仍略低于正方形中心。
如何正确对齐 TD 的内容?
我将源文件上传到此 文件夹 中,以备需要时使用。
预先致谢,
我按照用户 Maik Lowrey 的建议附上了相关代码。
HTML
<div class="row"><div class="column d-flex justify-content-center">
<table>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
</div></div>
JS
let cells = document.querySelectorAll("td");
cells[selected_cell].innerText=event.key;
这就是我现在得到的,网格的结构已经改变,但字母仍然没有垂直对齐:
I'm working on a small tic tac toe game and i have a problem aligning vertically the content of my TDs. As you can see in the following screenshot i apply the vertical-align property to the selected TD but the text is still not centered vertically and remains slightly below the center of the square.
How can i align the content of the TDs properly?
I uploaded the source files in this folder in case they are needed.
Thanks in advance
I attach the relevant code as suggested by the user Maik Lowrey.
HTML
<div class="row"><div class="column d-flex justify-content-center">
<table>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
</div></div>
JS
let cells = document.querySelectorAll("td");
cells[selected_cell].innerText=event.key;
This is what i get now, the structure of the grid has changed but the letter is still not aligned vertically:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要将容器内的任何元素居中,请将以下 css 添加到容器中
To center any element inside the container, add below css to the container
只需使用 verticle-align:middle 来表格..它可以解决你的问题
Simply you can use verticle-align:middle to table.. it can solve your problem
使元素居中的一种方法是使用
text-align: center
进行水平对齐。还要将line-height
设置为等于垂直对齐元素的height
。示例:
请参阅:https://www.sitepoint.com/atoz-css-quick -尖端垂直/
A way for centering elements is to use
text-align: center
for aligned horizontally. Also setline-height
equal withheight
of the element for aligned vertically.Example:
See: https://www.sitepoint.com/atoz-css-quick-tip-vertical/