You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将kableExtra生成HTML表格的hover颜色改为黄色?

Customizing Hover Color in kableExtra Tables

Absolutely! You can easily change the hover color from the default gray to yellow by overriding the default Bootstrap CSS styles used by kableExtra. Here are two simple, reproducible methods:

Method 1: Add Custom CSS Directly in kable_styling

You can pass your custom hover style directly to the extra_css parameter of kable_styling. This targets only the specific table you're styling:

require(knitr) 
require(kableExtra) 

kable(mtcars, "html") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"),
                extra_css = "table.dataTable.hover tbody tr:hover {background-color: #ffff99 !important;}")
  • The !important flag ensures your custom color overrides the default Bootstrap gray.
  • Adjust the hex code (#ffff99) to any yellow shade you prefer—for example, #ffeb3b for a bright yellow or #fff3cd for a pale, muted yellow.

Method 2: Apply Global CSS to All Tables

If you want this yellow hover effect to apply to every table in your RMarkdown document, add a CSS chunk at the top of your file:

<style>
table.dataTable.hover tbody tr:hover {
  background-color: #ffff99 !important;
}
</style>

Then use your original kable code as-is—no need to modify the kable_styling call:

require(knitr) 
require(kableExtra) 

kable(mtcars, "html") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))

Both methods will replace the default gray hover with your chosen yellow color, satisfying your client's request.

内容的提问来源于stack exchange,提问作者user2716568

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:14:18