Heim  >  Fragen und Antworten  >  Hauptteil

Implementieren Sie verschiedene benutzerdefinierte CSS (Schriftfarben) für Kable-Header in fortlaufenden Tabellen

<p>Ich möchte einen blauen Titel und dann einen roten Titel. Ich<code>cat</code> zwei HTML-Abschnitte <code><style>...</style></code>, laut dieser Antwort, aber ich werde rot beide. </p> <p>Wie bekomme ich einen blauen und einen roten Titel? </p> <pre class="brush:php;toolbar:false;">--- Ausgabe: html_document --- „{r setup, include=FALSE} knitr::opts_chunk$set(echo=FALSE) „ „{r results="asis"} Katze(" <Stil> Untertitel { Farbe blau; } </style> ") knitr::kable(head(iris), format="html", Ziffern=4, row.names=FALSE, caption='Beschriftung blau', escape=TRUE)|> kableExtra::kable_styling(font_size=14) |> kableExtra::kable_paper(c('hover', 'pressed', 'responsive'), full_width=T) |> kableExtra::scroll_box(width="100%", height="200px") „ „{r results="asis"} Katze(" <Stil> Untertitel { Farbe Rot; } </style> ") knitr::kable(head(iris), format="html", Ziffern=4, row.names=FALSE, caption='Beschriftung rot', escape=TRUE) |> kableExtra::kable_styling(font_size=14) |> kableExtra::kable_paper(c('hover', 'pressed', 'responsive'), full_width=T) |> kableExtra::scroll_box(width="100%", height="200px") ```</pre>
P粉563831052P粉563831052384 Tage vor503

Antworte allen(2)Ich werde antworten

  • P粉865900994

    P粉8659009942023-09-02 14:35:18

    您还可以为每个表提供一个特殊的 HTML 类,然后将所有样式收集在 css 块中,而不是在每个块中指定 CSS:

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

    或者,我们可以在块之外插入内联 CSS。

    <style>
    .mytable1 > caption {
      color: blue;
    }
    .mytable2 > caption {
      color: red;
    }
    </style>

    Antwort
    0
  • P粉156983446

    P粉1569834462023-09-02 00:44:29

    因为第二个CSS覆盖了第一个CSS。

    最好这样做:

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

    然后像这样使用:

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

    有效吗?

    问候, 诺埃尔

    Antwort
    0
  • StornierenAntwort