为连续表格中的 kable 标题实现不同的自定义 CSS(字体颜色)
P粉563831052
P粉563831052 2023-09-01 19:53:36
[CSS3讨论组]
<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=&quot;asis&quot;} cat(&quot; &lt;style&gt; caption { color: blue; } &lt;/style&gt; &quot;) knitr::kable(head(iris), format=&quot;html&quot;, digits=4, row.names=FALSE, caption='Caption blue', escape=TRUE)|&gt; kableExtra::kable_styling(font_size=14) |&gt; kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |&gt; kableExtra::scroll_box(width=&quot;100%&quot;, height=&quot;200px&quot;) ``` ```{r results=&quot;asis&quot;} cat(&quot; &lt;style&gt; caption { color: red; } &lt;/style&gt; &quot;) knitr::kable(head(iris), format=&quot;html&quot;, digits=4, row.names=FALSE, caption='Caption red', escape=TRUE) |&gt; kableExtra::kable_styling(font_size=14) |&gt; kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |&gt; kableExtra::scroll_box(width=&quot;100%&quot;, height=&quot;200px&quot;) ```</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>',

有效吗?

问候, 诺埃尔

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号