R markdown: how to change style with internal css?


Tags: css,r,markdown,knitr

Problem :

I know how to change R markdown style with a custom css file. However, when the changes are minor, I prefer internal or even inline css, to save trouble from managing two files. I googled and haven't find a solution for this. Below is a simple example of changing style with an external css file. Is there a way to do it with internal or inline css?

The R markdown file:

---
title: "test"
output: 
    html_document:
        css: test.css
---

## Header 1 {#header1}
But how to change style with internal css?

The test.css file:

#header1 {
color: red;
}


Solution :

Markdown accepts raw HTML and passes it through unaltered, so define your "styled" elements as HTML:

<h2 style="color: red;">Header 1</h2>

Of course, some tools don't actually allow the raw HTML to be passed through (for security reasons or because the final output is not HTML), so your mileage may vary.

Depending on the Markdown implementation you are using, you may be able to define styles in the attribute list (if it supports arbitrary keys):

## Header 1 {style="color: red;"}

However, that is the least likely to work.

And remember, HTML <style> tags do not need to be in the document <head> to work. If you can use raw HTML, you can include a <style> element in the body of your document (as pointed out by @user5219763 in a comment):

---
title: "test"
output: 
    html_document
---

<style>
    #header1 {
        color: red;
    }
</style>

## Header 1 {#header1}
But how to change style with internal css?

    CSS Howto..

    How to make a div tag without margins in css? [duplicate]

    How To Add Links To Right Of Header?

    How to make div position fixed without floating

    How to indicate there is more text to scroll?

    How do I increase the width of a navbar dropdown?

    How do I stop richfaces adding borders to panels and calendars?

    disabling stylesheets to increase the speed of showing/hiding divs

    CSS Flexbox: how to change the width of the element

    How to layout images in 3 columns [closed]

    CSS: How to crop image that has any size?

    How to rotate and translate using CSS in IE8?

    How to Over ride youtube external style?

    How can I make the side elements spin on the x-axis in this animation?

    How to override margin and right/left in css?

    How do I limit the number of cells on a row in CSS

    Text and image in div - How to align correctly?

    How to remove/replace cached css and js file from client Browser?

    Knockout - How to bind outer container css from set of checkboxes?

    CSS HTML how to remove whole cell links in tables?

    css: how to inherit whole class

    How to align jQuery div to bottom left corner using CSS?

    How to use css image sprites in this code?

    How to tell the code to choose between two variables and make them equal?

    How to remove line between two diagonally opposite css scalene triangles?

    How to prevent span from growing with content

    How to specify BOTH max-width % and px [closed]

    How do I keep multiple DIVs the same height using jQuery?

    How to Display text in Middle of table cell Horizontally and vertically in html css?

    JavaFX 8 - How to change the color of the prompt text of a NOT editable combobox via CSS?

    how to use linked animated css?