generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 11
/
06_css-for-shiny.Rmd
396 lines (295 loc) · 8.86 KB
/
06_css-for-shiny.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# CSS for Shiny
**Learning objectives:**
- Cover the basics of **CSS**.
- Use **CSS** to customize a Shiny app appearance.
## How to include CSS?
- Three methods to include CSS in a web page:
- **External**
- Points to an external file.
- It's the recommended way, due to being
easier to maintain.
- **HTML way**: `<link rel="stylesheet" href="style.css"/>`
- **Shiny way** `tags$link(rel = "stylesheet", type="text/css", href="www/style.css")`
- **Internal**
- Insert `style` tag in `<head>`.
- Hard to maintain.
- *Shiny way*: `tags$head(tags$style("p {color: red;}"))`
- **Inline**
- Insert style at *tag level*.
- Hard to maintain.
- *Shiny way*: `p(style = "color:red;", "Red text")`
## What does Shiny's creator think? {-}
- Useful [comment](https://github.com/rstudio/shiny/issues/3225#issuecomment-751132164)
from the author of Shiny, about how to include custom CSS files.
- As noted in the previous comment, including a CSS file via the
`htmlDependency` function is not necessary if such CSS code is
completely custom (no need to avoid dependencies' version conflicts).
- Therefore, for **custom CSS**, simply use the `tags$link` method.
## CSS selectors
- CSS selectors define on **which elements** to apply CSS rules.
- [Table](https://www.w3schools.com/cssref/css_selectors.php) of basic CSS selectors.
- A web page where this subchapter's CSS code is applied, can be found
in the folder `examples/chapter-06/css-selectors` of this book's GitHub
[repository](https://github.com/r4ds/bookclub-shinyui).
- It's recommended to use such web page in order to follow along
the examples in this subchapter.
- Aditionally, another page showcasing common CSS properties
can be found in the repo folder `examples/chapter-06/common-css`
### Basic
- CSS declaration structure:
```css
selector {
property1: value1;
property2: value2;
}
```
### Select by tag name {-}
- Select **one** HTML element via its tag name:
```css
span {
color: red;
}
```
- Select **multiple** HTML elements via their tag name:
```css
p, div {
background-color: lightsalmon;
}
```
### Select by class or id {-}
- Example of attributes class and id:
`<p class="random" id="not-random">some text</p>`
- Respective CSS selectors:
```css
/* class
Style for one or many elements
*/
.random {
color: gold;
background-color: black;
}
/* id
Style for only one specific element
*/
#not-random {
color: crimson;
}
```
### Intermediate
Now, we introduce selectors corresponding
to the *state*, *part* or *attribute*
of an HTML element.
### Pseudo-class {-}
- The [pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes)
(`:`) specifies a special **state** of the selected elements.
- Examples:
```css
li:hover {
color: peru;
}
li:active {
font-size: 200%;
}
li:first-child {
text-decoration: underline;
}
li:not(:first-child) {
color: red;
}
```
### Pseudo-element {-}
- The [pseudo-element](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements)
(`::`) specifies a part to modify of the selected element.
- Examples:
```css
p::before {
content: "random words: ";
}
p::first-letter {
text-transform: uppercase;
}
p::after {
content: "blah blah blah blah";
}
```
- Eye-catching [application](https://www.youtube.com/watch?v=-VOUK-xFAyk&ab_channel=KevinPowell)
of the `::before` and `::after` pseudo-elements.
### Attributes {-}
- [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes)
are aditional values that configure or adjust the behaviour of HTML elements.
- Examples:
```css
div[data-nChildren="3"] {
border: 5px solid rgb(200, 100, 50, 0.5);
border-radius: 30px;
}
textarea[rows="5"] {
background-image: linear-gradient(
to right, crimson, skyblue
);
}
em[contenteditable="true"] {
opacity: 0.5;
}
```
### Advanced
### Nesting {-}
- **Child** selector:
- Example:
```css
/* Every list's main items */
ul > li {
color: green;
}
/* Nested list's items */
li > ul > li {
color: red;
}
```
- **Descendant selector**:
- Example:
```css
/* All items (main or sub) from list */
ol li {
font-family: cursive;
}
```
### Parent selector {-}
- The CSS tools seen so far allow us to perform
a **descendant selection** based on properties of ancestors,
*not the other way around*.
- The `:has()` pseudo-class allows us to perform an
**ancestor selection** based on properties of its descendants.
- The [`:has()`](https://developer.mozilla.org/en-US/docs/Web/CSS/:has)
pseudo-class was added in the year 2022 to most modern browsers.
- Such recent new feature introduces
[many posibilities](https://www.youtube.com/watch?v=OGJvhpoE8b4&ab_channel=KevinPowell)
for web design.
- Examples:
```css
div:has(p:active) {
outline: 2px dashed limegreen;
}
table:has(td:hover) td:not(:hover) {
filter: blur(1px);
}
```
### Animations {-}
- CSS keyframe syntax:
```css
@keyframes animation-name {
from { property: value; } /* 0% */
to { property: new_value; } /* 100% */
}
```
- CSS syntax to **attach an animation**:
```css
selector {
animation: name, duration, delay, count, ...;
/* OR */
animation-name: ...;
animation-duration: ...,;
/* ... */
}
```
- The [transition](https://developer.mozilla.org/en-US/docs/Web/CSS/transition)
CSS property also allows us to **smoothly** change some property's values.
- Examples:
```css
/* transition */
p {
background-color: red;
}
p:hover {
background-color: orange;
transition: background-color 2s;
}
/* keyframes */
rect:hover {
animation: 2s linear slide-in;
}
@keyframes slide-in {
0% {
transform: translateX(0%);
}
50% {
transform: translateX(70%);
}
100% {
transform: translateX(100%);
}
}
```
## Layout
- HTML elements are modelled as boxes,
- HTML elements can either be **inline**
(starts on the *same line*) or **block-level**
(*generates a break line*).
- [Inline vs block-level elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements)
- Block-level elements can be modelled as a box, with
the possibility to alter its dimensions via properties
such as `margin-top, margin-left, margin, padding` and
[more](https://subscription.packtpub.com/book/web-development/9781787281585/1/ch01lvl1sec03/the-box-model-and-block-versus-inline-elements).
## Flexbox and CSS Grid {-}
- The most popular CSS layout modules in recent years are
**Flexbox** and **CSS Grid**.
- Both layout modules support content placement in
**2 dimensions** ... one of the main reasons to
use one instead of the other is
[content **placement** vs content **flow**](https://webdesign.tutsplus.com/articles/flexbox-vs-css-grid-which-should-you-use--cms-30184).
- > If you want to accurately control the position of items within a layout, CSS Grid is the way to go.
- > Flexbox enables you to allocate space and align items flexibly (determined by the item content).
## Media queries {-}
- Depending on [factors](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries)
such as the dimensions of the screen from which
a website is accessed, its content may or not be
**efficiently organized** (or even readable).
- Example using Flexbox:
```html
<div class="flex-container">
<div></div>
<div></div>
<div></div>
</div>
```
```css
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container > div {
width: 200px;
aspect-ratio: 3/2;
background-color: crimson;
}
@media (max-width: 600px) {
.flex-container > div {
width: 500px;
}
}
```
## BEM
- <https://css-tricks.com/bem-101/>
- <https://www.toptal.com/css/introduction-to-bem-methodology#:~:text=BEM%20stands%20for%20Block%20Element,CSS%20classes%20into%20independent%20modules.>
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/mx7RrARw930")`
<details>
<summary> Meeting chat log </summary>
```
00:11:24 Russ: Hi everyone
00:11:27 iPhone: Hello
00:29:35 Lucio Cornejo: hello :(
00:30:26 Federica Gazzelloni: Hello
00:31:37 iPhone: Hello!!!
00:33:43 Lucio Cornejo: https://github.com/rstudio/shiny/issues/3225#issuecomment-751132164
00:43:03 Russ: I don't think I've used `:not` before, that's pretty neat
00:46:10 Lucio Cornejo: https://www.youtube.com/watch?v=-VOUK-xFAyk&ab_channel=KevinPowell
00:57:11 Lucio Cornejo: https://www.youtube.com/watch?v=OGJvhpoE8b4&ab_channel=KevinPowell
01:04:08 Arthur Shaw: @lucio, is CSS "calculating" the transition from one state to another?
01:05:15 Arthur Shaw: Replying to "@lucio, is CSS "calc..."
For the translation property
01:08:47 Arthur Shaw: Gotta drop off for another meeting. Look forward to watching the last part of the recording!
Thanks for the great presentation.
```
</details>