为连续表格中的 kable 标题实现不同的自定义 CSS(字体颜色)
P粉563831052
P粉563831052 2023-09-01 19:53:36
0
2
510
<p>我想要一个蓝色标题,然后一个红色标题。我 <code>cat</code> 两个 HTML <code><style>...</style></code> 部分,第一个蓝色第二个红色,根据这个答案,但我都得到红色。</p> <p>如何获得蓝色标题和红色标题?</p> <pre class="brush:php;toolbar:false;">--- output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo=FALSE) ``` ```{r results="asis"} cat(" <style> caption { color: blue; } </style> ") knitr::kable(head(iris), format="html", digits=4, row.names=FALSE, caption='Caption blue', escape=TRUE)|> kableExtra::kable_styling(font_size=14) |> kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |> kableExtra::scroll_box(width="100%", height="200px") ``` ```{r results="asis"} cat(" <style> caption { color: red; } </style> ") knitr::kable(head(iris), format="html", digits=4, row.names=FALSE, caption='Caption red', escape=TRUE) |> kableExtra::kable_styling(font_size=14) |> kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |> kableExtra::scroll_box(width="100%", height="200px") ```</pre>
P粉563831052
P粉563831052

全部回复(2)
P粉865900994

您还可以为每个表提供一个特殊的 HTML 类,然后将所有样式收集在 css 块中,而不是在每个块中指定 CSS:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE)
```

```{css}
.mytable1 > caption {
    color: blue;
}
.mytable2 > caption {
    color: red;
}
```

```{r results="asis"}
knitr::kable(head(iris), 
             format="html",
             digits=4,
             row.names=FALSE,
             caption='Caption blue',
             escape=TRUE)|>
  kableExtra::kable_styling(font_size=14, htmltable_class = "mytable1") |>
  kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |>
  kableExtra::scroll_box(width="100%", height="200px") 
```


```{r results="asis"}
knitr::kable(head(iris), 
             format="html",
             digits=4,
             row.names=FALSE,
             caption='Caption red',
             escape=TRUE) |>
  kableExtra::kable_styling(font_size=14, htmltable_class = "mytable2") |>
  kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |>
  kableExtra::scroll_box(width="100%", height="200px")
```

或者,我们可以在块之外插入内联 CSS。

<style>
.mytable1 > caption {
  color: blue;
}
.mytable2 > caption {
  color: red;
}
</style>
P粉156983446

因为第二个CSS覆盖了第一个CSS。

最好这样做:

cat("
<style>
.blue-caption {
      color: blue;
    }

.red-caption {
      color: red;
    }
</style>
")

然后像这样使用:

caption='<span class=\"blue-caption\">Caption blue</span>',
caption='<span class=\"red-caption\">Caption red</span>',

有效吗?

问候, 诺埃尔

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!