Following on from this question posed a while ago, does anyone know how I can fill the entire background of a cell using the cell_spec() function in kableExtra?
I have some dummy data below, but I can only manage to colour the background of the text. I'd prefer the entire cell filled and I'm pretty set on using kableExtra to create my table within an R Markdown file. Does anyone know of a workaround for this? Thanks.
library(kableExtra)
name <- paste("Name", LETTERS)
val <- rnorm(26, 100, 50)
d <- data.frame(name, val)
d$val <- cell_spec(d$val, background = if_else(d$val > mean(d$val) + sd(d$val),
"#DBEBF6",
if_else(d$val < mean(d$val) - sd(d$val),
"#FEE7DA",
"transparent")))
kbl(d, escape = FALSE) %>%
kable_styling()
The issue is that
cell_speconly targets the cell content, i.e. thespantag which contains the content, but not the parent table cell, i.e. thetdtag. And by default some padding of8pxis added to the content of the table cell.One option to fill the entire cell would be to use the
extra_css=argument ofcell_spec, i.e. set the margin to-8pxand the padding to8pxso that the cell content fills the entire height of the cell. Additionally setdisplay: flexto fill the entire width of the cell.EDIT With
display: flexwe need some additional CSS to center align the values (see e.g. Aligning items in a flex container):