skip to Main Content

I want a blue caption then a red caption. I cat two HTML <style>...</style> sections, first blue second red, according to this answer, but I get both red.

How to get a blue then a red caption?

---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE)
```

```{r results="asis"}

cat("
<style>
caption {
      color: blue;
    }
</style>
")

knitr::kable(head(iris), 
             format="html",
             digits=4,
             row.names=FALSE,
             caption='Caption blue',
             escape=TRUE)|>
  kableExtra::kable_styling(font_size=14) |>
  kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |>
  kableExtra::scroll_box(width="100%", height="200px") 
```


```{r results="asis"}

cat("
<style>
caption {
      color: red;
    }
</style>
")


knitr::kable(head(iris), 
             format="html",
             digits=4,
             row.names=FALSE,
             caption='Caption red',
             escape=TRUE) |>
  kableExtra::kable_styling(font_size=14) |>
  kableExtra::kable_paper(c('hover', 'condensed', 'responsive'), full_width=T) |>
  kableExtra::scroll_box(width="100%", height="200px")
```

enter image description here

2

Answers


  1. Because second css overwrites the first css.

    Better to do like this:

    cat("
    <style>
    .blue-caption {
          color: blue;
        }
    
    .red-caption {
          color: red;
        }
    </style>
    ")
    

    and then use like this:

    caption='<span class="blue-caption">Caption blue</span>',
    caption='<span class="red-caption">Caption red</span>',
    

    Works?

    Regards,
    Noel

    Login or Signup to reply.
  2. Rather than specifying the CSS in each chunk, you can also give a special HTML class to each table and then gather all styling in a css chunk:

    ---
    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")
    ```
    

    enter image description here

    Alternatively we could insert inline css outside of a chunk.

    <style>
    .mytable1 > caption {
      color: blue;
    }
    .mytable2 > caption {
      color: red;
    }
    </style>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search