body {-family: "Open Sans", sans-serif;
font-height: 1.6;
line-width: 1200px;
max: 0 auto;
margin: 2em;
padding-color: #ffffff;
background: #333333;
color }
1 Using Custom CSS in Quarto
Quarto allows you to customize the look and feel of your HTML outputs by incorporating custom CSS. By defining your styles in a separate .css
file, you can tweak various elements like colors, fonts, spacing, and layout to match your preferences or brand guidelines. Using CSS gives you precise control over the styling of every HTML element generated in your document.
How to create a custom CSS file
You can use the menu options within R Studio to create a CSS file like this:
This will open up a CSS file in R Studio. Note that the file does not have a name at this point (reminding you that it hasn’t been saved).
Once you have saved the file you’ll see it appear in your working directory.
Now you need to include an instruction in your YAML that instructs quarto to look at the CSS file when considering the formatting of your rendered document.
2 Customizing styles
You can define styles for a wide range of elements in your Quarto HTML document, such as:
Headings and Paragraphs: Change fonts, sizes, colors, etc.
Code Blocks: Customize the appearance of code, including background and text colors.
Tables: Modify the look of tables for better readability.
Images and Figures: Adjust sizes, borders, and positioning.
Links, Lists, Buttons: Customize interactions, colors, etc.
You can cut and paste code from this document and change the values in the parameters that you are interested. It should look something like this:
You don’t need to know how to do HTML coding. All you need to be able to do is copy and past the code (from below) that relate to the aspect of the style you’re interested an change the parameters to whatever you like.
3 Usage Tips
-
Selective Styling
Use specific class names to target elements
Avoid overly broad selectors
Test changes incrementally
-
Organization
Group related styles together
Use comments to separate sections
Keep a consistent naming convention
-
Troubleshooting
Use browser developer tools to inspect elements
Check selector specificity
Test across different screen sizes
-
Best Practices
Minimize use of !important
Use CSS variables for consistent colors/values
Consider accessibility (contrast, text size)
4 Sample CSS
You can cut and paste the code that you want to include in your CSS file from any of the section below:
4.1 Document style
4.2 Headings
h1 {: #2c3e50;
color-size: 2.5em;
font-bottom: 0.5em;
margin-bottom: 2px solid #eee;
border-bottom: 0.2em;
padding
}
h2 {: #34495e;
color-size: 2em;
font-top: 1.5em;
margin-bottom: 0.5em;
margin
}
h3 {: #465d71;
color-size: 1.5em;
font-top: 1.2em;
margin }
4.3 Links
a {: #2980b9;
color-decoration: none;
text-bottom: 1px solid transparent;
border: border-color 0.2s ease;
transition
}
:hover {
a-bottom-color: #2980b9;
border }
4.4 Paragraphs and spacing
p {-bottom: 1.5em;
margin-align: justify;
text }
4.5 Lists
ul, ol {-left: 1.5em;
padding-bottom: 1.5em;
margin
}
li {-bottom: 0.5em;
margin }
4.6 Document style
4.7 Code blocks
/* Code blocks */
pre {-color: #f8f9fa;
background: 1px solid #e9ecef;
border-radius: 4px;
border: 1em;
padding: 1em 0;
margin-x: auto;
overflow
}
/* Inline code */
code {-color: #f8f9fa;
background: 0.2em 0.4em;
padding-radius: 3px;
border-size: 0.9em;
font: #e83e8c;
color
}
/* Code cell output */
-output {
.cell-color: #ffffff;
background: 1px solid #dee2e6;
border-radius: 4px;
border: 1em;
padding-top: 0.5em;
margin
}
/* Syntax highlighting customization */
: #007bff; } /* Keywords */
.sourceCode .kw { color: #28a745; } /* Strings */
.sourceCode .st { color: #dc3545; } /* Decimal values */
.sourceCode .dv { color: #6c757d; } /* Comments */
.sourceCode .co { color: #fd7e14; } /* Functions */ .sourceCode .fu { color
4.8 Tables
/* Table styling */
table {: 100%;
width-bottom: 1em;
margin-collapse: collapse;
border-spacing: 0;
border
}
th {-color: #f8f9fa;
background-bottom: 2px solid #dee2e6;
border: 0.75em;
padding-align: left;
text
}
td {: 0.75em;
padding-bottom: 1px solid #dee2e6;
border
}
/* Striped tables */
:nth-child(even) {
tr-color: #f8f9fa;
background
}
/* Hover effect */
:hover {
tr-color: #f2f2f2;
background
}
/* Responsive tables */
@media screen and (max-width: 600px) {
table {: block;
display-x: auto;
overflow
} }
4.9 Table of content
/* TOC container */
-toc {
.quarto-color: #f8f9fa;
background: 1px solid #dee2e6;
border-radius: 4px;
border: 1em;
padding: 1em 0;
margin
}
/* TOC title */
-toc-title {
.quarto-size: 1.2em;
font-weight: bold;
font-bottom: 0.5em;
margin: #2c3e50;
color
}
/* TOC links */
-toc a {
.quarto: #495057;
color-decoration: none;
text: block;
display: 0.2em 0;
padding
}
-toc a:hover {
.quarto: #007bff;
color-color: #f1f1f1;
background
}
/* TOC nesting levels */
-toc ul {
.quarto-style-type: none;
list-left: 1em;
padding
}
-toc > ul {
.quarto-left: 0;
padding }
4.10 Figures and images
/* Figure containers */
figure {: 2em 0;
margin-align: center;
text
}
/* Images */
img {-width: 100%;
max: auto;
height-radius: 4px;
border-shadow: 0 2px 4px rgba(0,0,0,0.1);
box
}
/* Figure captions */
figcaption {: #666;
color-style: italic;
font-size: 0.9em;
font-top: 0.5em;
margin }
4.12 Callout boxes
/* Custom callouts */
.callout {-left: 5px solid #007bff;
border-color: #f8f9fa;
background: 1em;
padding: 1em 0;
margin
}
-note {
.callout-left-color: #17a2b8;
border
}
-warning {
.callout-left-color: #ffc107;
border
}
-important {
.callout-left-color: #dc3545;
border
}
-tip {
.callout-left-color: #28a745;
border }
4.13 Responsive design
/* Responsive breakpoints */
@media screen and (max-width: 768px) {
body {: 1em;
padding
}
h1 {-size: 2em;
font
}
h2 {-size: 1.5em;
font
}
pre {-size: 0.9em;
font
}
}
@media screen and (max-width: 480px) {
body {: 0.5em;
padding
}
h1 {-size: 1.8em;
font
}
-toc {
.quarto: none;
display
} }