Customizing div elements in various RMarkdown documents with a CSS file

I want to apply styling to divs in various RMarkdown files using a CSS file. However, I am encountering syntax issues when trying to use the external CSS file to style the div.

Embedding the style tag within the rmarkdown body works as shown below:

---
title: "Untitled"
output: 
  html_document
---

<style>
div.positive {
  background-color: #006B35;
  color: #ffffff;
  font-size: 16px;
  text-align: left;
  vertical-align: middle;
  padding: 0 10px 80px;
}

div.negative {
  background-color: #C83C4D;
  color: #ffffff;
  font-size: 16px;
  text-align: left;
  vertical-align: middle;
  padding: 0 10px 80px;
}
</style>

<div class="positive">
Green background div
</div>

<div class="negative">
Red background div
</div>

When I remove the inline styles and reference a separate CSS file, the divs are not getting styled as expected, as shown in the following code snippet:

CSS

.positive {
  background-color: #006B35;
  color: #ffffff;
  font-size: 16px;
  text-align: left;
  vertical-align: middle;
  padding: 0 10px 80px;
}

.negative {
  background-color: #C83C4D;
  color: #ffffff;
  font-size: 16px;
  text-align: left;
  vertical-align: middle;
  padding: 0 10px 80px;
}

RMarkdown

---
title: "Untitled"
output: 
  html_document:
  css: "style.css"
---

<style>
div.positive {

}

div.negative {

}
</style>


<div class="positive">
Green background div
</div>

<div class="negative">
Red background div
</div>

The reason for wanting to use a single CSS file is to have a centralized location to modify styles across multiple RMarkdown files easily rather than updating each file individually.

Your assistance with this issue would be highly appreciated.

Answer №1

Proper YAML header formatting is crucial for the logical organization of your code. In this case, there was a lack of indentation where it should have been present.

---
title: "Untitled"
output: 
  html_document:
  css: "style.css"
---

This resulted in incorrect structure, and the correct formatting should look like this:

---
title: "Untitled"
output: 
  html_document:
    css: "style.css"
---

Make sure to pay attention to indentations, especially when dealing with multiple levels like in the css: line.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display the progress bar's completion percentage during animation

Creating a progress bar using HTML with the following structure: <div class="progressbar"><div class="text">%0 Completed</div> <div class="progressbar_inner"></div> </div> Implemented jQuery code fo ...

Tips for storing checkbox preferences in a user account on an HTML/PHP website

Hello! I am currently working on a website. I have included a checkbox that updates the option selected in phpMyAdmin. The issue I am facing is that when I press submit and then refresh the page using F5, the preference I chose disappears on the client s ...

Minimize the gaps between items within a CSS grid design

I'm struggling with adjusting the whitespace between 'Job Role' and 'Company Name' in my grid layout. Here's a snippet of what I have: The container css is set up like this: .experience-child { display: grid; grid-tem ...

Using Python in Django to seamlessly add products to the shopping cart

Seeking guidance in creating a simple function that can capture the product ID and insert it into the user's shopping cart upon clicking the "add to cart" button. I have already set up the shopping cart table and the product table but am struggling to ...

Issues with functionality arise when cloning HTML divs using JQuery

VIDEO I created a feature where clicking a button allows users to duplicate a note div. However, the copied note does not function like the original - it's not draggable and changing the color of the copied note affects the original note's color. ...

Adjusting the size of a division element to match the dimensions

Currently, I am still in the process of finalizing the remainder of the page, but I have encountered a minor issue. The problem lies in my image div not matching the height of the container. Although the image itself is the correct size, the image div appe ...

From Spring MVC to JSON data output

I have a JAVA EE backend and I am using Spring MVC. Currently, I have an AJAX call set up as follows: function getAllProjects() { $.getJSON("project/getall", function(allProjects) { ??? }); } In my backend system: @Reques ...

Activate hover event on UI-Grid for interactive display

I often utilize in the following manner: <div ncy-breadcrumb class="title-shadow"></div> {{ getDeviceTree }} <div> <div ui-grid="gridOptions" ui-grid-resize-columns ui-grid-selection></div> </div> My object ...

Tips for decreasing Vuetify Grid column width on larger screens

I'm currently utilizing the vuetify grid system to construct a reply component. The column ratio is set at 1:11 for the icon and reply columns, respectively. Below you'll find the code snippet: <v-row justify="center" class="ma-0 pa-0"> ...

Ways to superimpose an image

Struggling to overlay an image over two div columns - I attempted using Z-index and experimented with various positioning properties. What am I missing? The triangle situated behind the two boxes, along with the additional text on JSFiddle, should appear ...

Experiencing issues with exporting <SVG> file due to corruption

I received some downvotes on my previous post, and I'm not sure why. My question was clear, and I provided a lot of information. Let's give it another shot. My issue is with exporting <SVG> files from an HTML document. When I try to open t ...

Convince me otherwise: Utilizing a Sinatra API paired with a comprehensive JS/HTML frontend

As I embark on the journey of designing a social website that needs to support a large number of users, I have come up with an interesting approach: Implementing Sinatra on the backend with a comprehensive REST API to handle all operations on the website ...

Align both text and images in the middle using HTML/CSS

I attempted to align an image next to text and center everything, but I'm facing some challenges. The arrow indicates where I would prefer the image to be placed. HTML <!DOCTYPE html> <head> <title>Simple Page</title> ...

Eliminate styling from text within an HTML document

I'm currently working on removing text decorations from my text. I managed to remove the underline, but when I hover over the text, the color still appears. How can I get rid of this color as well? Take a look at the code below: messbox { backgr ...

Restoring styles back to the default CSS settings

I am currently working on creating visualizations using D3, and one challenge that I have encountered is the need to define a lot of styles within my code instead of in my CSS where I would prefer them to be. This necessity arises mainly to support transi ...

Customizing the display field in an ExtJs Combobox

I am working on a java web application that utilizes an entity class to populate a combobox with ExtJs. The issue I am facing is as follows: Some entries in the displayField may contain html code. To prevent any issues, I used flexjson.HTMLEncoder during ...

Arranging checkboxes in harmony with accompanying text using HTML and CSS

Here is the layout I have created using react JS. Below is the code snippet used to generate each checkbox: const CheckBoxItem = ({ Label, item, paramField, change }) => { return ( <div className="Search-Input-CheckBox-Item" ...

Exploring color options in matplotlib for HTML designs

Is there a way to change color in a matplotlib plot using html in Python? I attempted plt.plot(x,y,marker=".",color="#e0e0e0") However, this resulted in data points displayed as ".", connected by lines of color #e0e0e0 which is not desired. I also experi ...

Assistance required with extracting information from JSON format on a website

Seeking assistance in displaying the last 10 songs played on a web page using JSON data. The current code is not fetching and showing the requested information from the json file. https://jsfiddle.net/Heropiggy95/6qkv7z3b/ Any help in identifying errors ...

Is the image flickering non-stop when the button is pressed?

Every time I push the button, the image on the HTML 5 canvas flickers. After investigating, it seems like the issue might be related to the ctx.clearRect(0,0,100,500) function. Any suggestions on how to fix this problem? I'm currently working on anim ...