Skip to content

Commit ff561fc

Browse files
committed
Added some CSS content
1 parent 670c4c9 commit ff561fc

9 files changed

Lines changed: 4208 additions & 18 deletions
92.3 KB
Loading

docs/lessons/Aside_CSS.html

Lines changed: 315 additions & 14 deletions
Large diffs are not rendered by default.

docs/lessons/Aside_CSS_property_reference.html

Lines changed: 2118 additions & 0 deletions
Large diffs are not rendered by default.

docs/search.json

Lines changed: 345 additions & 2 deletions
Large diffs are not rendered by default.
92.3 KB
Loading

lessons/Aside_CSS.qmd

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,135 @@
11
---
2-
title: "Customizing apps with CSS in RShiny"
3-
author: "Will Gammerdinger"
2+
title: "Customizing apps with CSS"
3+
description: |
4+
Customizing the appearance of R Shiny apps with CSS allows developers to control layout, colors, spacing and visual identity beyond what default Shiny styling provides. This lesson introduces the basics of creating and linking a CSS file, understanding selectors and applying styles to common Shiny UI components. It is designed as a practical starting point for participants who want to enhance the look and usability of their Shiny applications.
5+
author:
6+
- Will Gammerdinger
7+
date: "2025-11-20"
8+
categories:
9+
- R
10+
- Shiny
11+
- CSS
12+
- UI styling
13+
keywords:
14+
- R Shiny
15+
- CSS selectors
16+
- UI customization
17+
license: "CC-BY-4.0"
18+
editor_options:
19+
markdown:
20+
wrap: 72
421
---
522

23+
## Learring Objectives
24+
25+
In this lesson, you will:
26+
27+
- Create a CSS file for a Shiny app
28+
- Navigate our CSS resource to edit stlyes in your app
29+
30+
## What is CSS?
31+
32+
CSS, also known as Cascading Style Sheet, is a style sheet language that allows you to customize the look for HTMLs. In fact, these lessons that your learning from right now even have their own CSS file controlling the colors, fonts, etc. of the things you see on this page. When we use a CSS file in conjunction with our Shiny app, it gives us much more control over the styling of each aspect of the elements in our UI in the same way that you can have fine control over your plots when creating them in R.
33+
34+
:::{.callout-note}
35+
Before we get started, it should be noted that this lesson and its accompanying resources are designed to be a starting place for getting your head around customizing your apps with a CSS file. It is not intended to be an exhaustive to everything you can do with a CSS file as there are virtually endless combinations of things you can style and ways to style them. We will introduce to you the concepts of how you can style things and then point you to a resource that we have developed to help you get started with styling your app with CSS.
36+
:::
37+
38+
## Setting your workspace up for CSS
39+
40+
First, you will need to make a directory within your Shiny app's directory called `www`. Whenever you run a Shiny app, it will check for this folder. In order to create this folder, let's click the "New Folder" button in the "Files" tab, name it `www` and click <kbd>OK</kbd>.
41+
42+
Now, we need to make a `style.css` file and place it within this `www` directory. In order to do this, enter the `www` directory, click the "New File" button in the "Files" tab, select "Text File", name it `style.css` and click <kbd>OK</kbd>.
43+
44+
Lastly, you need to add the `includeCSS()` function with the path to your CSS file to the app's UI like:
45+
46+
```{r}
47+
#| label: CSS_example
48+
#| eval: false
49+
ui <- fluidPage(
50+
includeCSS("www/style.css"),
51+
<rest_of_your_UI>
52+
)
53+
```
54+
55+
Now, let's open up the `style.css` file in RStudio next to our app and get to work!
56+
57+
## CSS File Format
58+
59+
CSS files hold a set of CSS rules that govern the style of your app or HTML page. In order for your CSS file to be implemented, you need to format the rules in CSS format. In order to help with this, let's look at an example of what a CSS rule from a CSS file might look like:
60+
61+
![Example showing the components of a CSS rule](../img/CSS_entry_example_with_label.png){width=600px}
62+
63+
We will discuss each of the components of a CSS rule in the sections below.
64+
65+
### Commenting your CSS
66+
67+
Commenting your code is a great practice in order to make your code more readable to other. While in many languages you comment your code with a `#`, in CSS you open a comment with `/*` and close a comment with `*/`. In the example code above you can see that we had a comment in front of the rule. It can be really helpful to have your label each of your rules with a comment, so that you and easily figure out which rule is governing a particular part of your HTML or app.
68+
69+
### Selectors
70+
71+
Selector *select* which HTML elements you would like to target for styling. You can think about this as the names of the things to style. These selectors fall into several categories, but the main two that we will deal with are:
72+
73+
- **Class selectors** - These selectors apply a CSS rule set across all object of that class and they start with a `.`. For example, if you are trying to target all of the input sliders, then you would be concerning yourself with the `.irs--shiny` class.
74+
75+
:::{.callout-note}
76+
You may have multiple class selectors like in the above example of `.irs--shiny .irs-handle`. In this case, `.irs-handle` is considered a descendant selector, which means that within the `.irs--shiny` there exists a class called `.irs-handle`. The class selectors in this case will be separated by a space.
77+
:::
78+
79+
- **ID selectors** - The selectors apply a CSS rule set to a specific `inputID` and they start with a `#`.
80+
81+
:::{.callout-note}
82+
An example of this is very common in input widget labels. For example you might see a CSS rule set like:
83+
84+
```{r}
85+
#| label: ID_selector_label_1
86+
#| eval: false
87+
/* Slider Label */
88+
#<inputID>-label{
89+
color: orange;
90+
font-size: 20px;
91+
}
92+
```
93+
94+
In this example the `<inputID>` would need to be replaced with the `inputID` for this slider. Rulesets defined here only apply to the label of ***this*** input slider's labels.
95+
96+
Another example might look like:
97+
98+
```{r}
99+
#| label: ID_selector_label_2
100+
#| eval: false
101+
#my_slider .irs-handle {
102+
background-color: cornflowerblue;
103+
}
104+
```
105+
106+
In this case the class selector `.irs--shiny` has been replaced an ID selector targetting a specific input ID `#my_slider`.
107+
:::
108+
109+
### Declaration Block
110+
111+
#### Declaration
112+
113+
##### Property
114+
115+
##### Value
116+
117+
## CSS Element Reference
118+
119+
In the list below, we have laid out different input and layout elements that you might want to alter with a CSS file. Within each lays out the different elements that you can style and
120+
121+
- [`actionButton`]()
122+
- [`checkboxInput`/`checkboxGroupInput`]()
123+
- [`dateInput`/`dateRangeInput`]()
124+
- [`navbar`]()
125+
- [`radioButtons`](Aside_CSS_radioButtons.qmd)
126+
- [`selectInput`]()
127+
- [`sidebarPanel`]()
128+
- [`sliderInput`](Aside_CSS_sliderInput.qmd)
129+
- [`textInput`/`textAreaInput`]()
130+
131+
We have also created a [CSS Property Reference](Aside_CSS_property_reference.qmd) that gives a move detailed accounting what each property can accept for values.
132+
6133
***
7134

8135
[Back to Schedule](..)

0 commit comments

Comments
 (0)