html魔术块中的样式标签在Jupyter笔记本中不起作用



我正在尝试使用Jupyter笔记本来教授HTML和CSS。我可以用%%html在Jupyter笔记本中创建一个HTML块。然而,当我尝试包含一个SOME CSS标记时,CSS不会生效。(我可以使用嵌入式CSS(。

行之有效的例子

以下作品和内容显示为h1,颜色为蓝色:

%%html
<h1 style="color=blue;">Hello</h1>

不起作用的示例

以下内容不起作用。内容显示为h1,但颜色不是蓝色:

%%html
<style>
h1: { background-color: yellow; }
</style>
<h1>hello</h1>

我尝试过的其他事情

以下工作也没有:

我尝试使用type=";text/css">

%%html
<style type="text/css">
h1: { background-color: yellow; }
</style>
<h1>hello</h1>

我尝试使用--isolated选项

%%html --isolated
<style type="text/css">
h1: { background-color: yellow; }
</style>
<h1>hello</h1>

我尝试使用整个HTML文档。

%%html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
h1: { color: blue; }
</style>
</head>
<body>
<h1>hello</h1>
</body>
</html>

我尝试使用IPython HTML函数——标记中的CSS不会被呈现。

from IPython.core.display import HTML
HTML("""
<style>h1: { color: blue; }
</style>
<h1>hello</h1>
""")

综上所述:

有什么方法可以让我用Jupyter笔记本教CSS吗?

您的css中有一个拼写错误,您已经添加:在h1 之后

h1:{…}

%%html
<style>
h1 { background-color: yellow; }
</style>
<h1>hello</h1>

最新更新