Search code examples
r-markdownknitrkableextra

RMarkdown: Add a box around a kable table


This is I'm sure a very simple problem that belies my lack of understanding of kable / css

I'm making a simple kable, e.g.

kable(mtcars)

I'd love it to have a border around the whole table and the caption, with some white space between the border and its contents.

I've achieved this with the css code:

table {
    border: 1px solid black;
    padding: 20px;
}

But there are two problems:

  1. I'd like the caption to be inside of the box
  2. There's no space between the table and the box - I thought padding would address this

I've tried an outline with an offset:

table {
  outline:1px solid black;
  outline-offset: 20px;
}

But then my text wraps too tight, and the box cuts over it


Solution

  • You could use a div block, e.g.

    ---
    title: "Title"
    output: html_document
    ---
    
    
    :::{.border style="padding: 10px; border: 1px solid #dee2e6 !important;"}
    ```{r}
    #| echo: false
    mtcars[1:5,] |> kableExtra::kbl(caption = "Caption") |> kableExtra::kable_styling()
    ```
    :::
    

    enter image description here