Search code examples
rr-markdownkableextra

Adjusting background color opacity in row_spec


I'm trying to adjust the opacity of the background color using kableExtra::row_spec but it seems that the part governing the alpha is being ignored. Is there anyway that I can control the alpha of a background color using this function? I have done a fair bit of searching but have not found anyone else with a similar issue as mine.

Here is a an example of what I am trying to do. I am expecting that the second row has the same background color as row 1 but with an opacity of 20%.



df <- data.frame(
  x = 1:2,
  y = c(10, 20)
)

kableExtra::kable(
  df, 
  caption = "Table"
) |> 
  kableExtra::kable_styling() |> 
  kableExtra::row_spec(1, background = "#3CB6CE") |> 
  kableExtra::row_spec(2, background = "#3CB6CE33")



Solution

  • One option would be to set the opacity using the extra_css= argument like so:

    kableExtra::kable(
      df,
      caption = "Table"
    ) |>
      kableExtra::kable_styling() |>
      kableExtra::row_spec(1, background = "#3CB6CE") |>
      kableExtra::row_spec(2,
        background = "#3CB6CE",
        extra_css = c("opacity: 0.2")
      )
    

    enter image description here