如何将kableExtra生成HTML表格的hover颜色改为黄色?
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
!importantflag ensures your custom color overrides the default Bootstrap gray. - Adjust the hex code (
#ffff99) to any yellow shade you prefer—for example,#ffeb3bfor a bright yellow or#fff3cdfor 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

