在 Jupyter Notebook 中居中 HTML 表格和文本



我知道Jupyter正在使用它自己的CSS样式,这限制了你可以对HTML做的事情,除非你编辑它。

我尝试以比 Python print更复杂的方式显示一些信息,为此我使用:

from IPython.core.display import display, HTML
display(HTML('<center>some header text</center>')))

不过,所有内容都左对齐 - 文本和表格。有没有简单的解决方法?

或者有没有其他方法可以在Jupyter Notebook中居中表格?

你可能需要在 html 元素中使用少量自定义 CSS。重要的位是style='margin: 0 auto'例如:

from IPython.core.display import display, HTML
display(HTML("""
<table style='margin: 0 auto'>
<tr><th>Hi!</th><th>There!</th></tr>
<tr><td>1</td><td>2</td></tr>
</table>
"""))

最新更新