😎 Give your xaringan slides some style
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

258 líneas
9.1KB

  1. ---
  2. title: "xaringanthemer CSS Help"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{xaringanthemer CSS Help}
  6. %\VignetteEngine{knitr::rmarkdown}
  7. %\VignetteEncoding{UTF-8}
  8. ---
  9. ```{r setup, include = FALSE}
  10. knitr::opts_chunk$set(
  11. collapse = TRUE,
  12. comment = "#>"
  13. )
  14. source("../R/theme_settings.R")
  15. ```
  16. Jump to: [Colors](#colors), [Sizes](#sizes), [Positioning](#positioning)
  17. ## About this vignette
  18. This vignette cover some basic CSS, in particular to give an idea about the possible values you can use with the various arguments to the xaringan theme functions.
  19. See `vignette("template-variables", package = "xaringanthemer")` for a full list of the theme functions.
  20. Because we are setting the CSS properties from R, we can either give **xaringanthemer** function arguments a character string or we can call an R function or variable that returns a character string.
  21. For example, we can create an R variable with a specific color that is used in several places in a theme
  22. ```r
  23. firebrick <- "#CD2626"
  24. style_xaringan(
  25. header_color = firebrick,
  26. link_color = firebrick
  27. )
  28. ```
  29. or we can directly give the character string
  30. ```r
  31. style_xaringan(
  32. header_color = "#CD2626",
  33. link_color = "#CD2626"
  34. )
  35. ```
  36. in both cases, we get CSS like the following that sets the link color
  37. ```css
  38. a, a > code {
  39. color: #CD2626;
  40. }
  41. ```
  42. Note that when a string is given to the theme function, the outer quotes are removed.
  43. In the sections below, R code is represented without quotes -- like `rgb(0.8, 0.15, 0.15)` -- and CSS code is represented inside quotes -- like `"rgb(205, 38, 38)"` -- to differentiate between R and CSS functions with the same or similar names.
  44. ## Colors
  45. [color-keywords]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#Color_keywords
  46. [color-rgb]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb()_and_rgba()
  47. [color-hsl]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl()_and_hsla()
  48. In CSS, text colors are specified with the `color:` property, background colors use `background-color:`, and border colors use `border-color:`.
  49. In **xaringanthemer**, template variables that set
  50. - text color end with `_color`;
  51. - background color end with `_background_color`;
  52. - border color end with `_border_color`;
  53. ### Setting colors
  54. In CSS, there are a number of ways to specify a color:
  55. - You can use a [color keyword][color-keywords], such as `"darkslategray"` or `"red"`.
  56. - You can use the [RGB color specification][color-rgb] either via
  57. - the hexadecimal representation
  58. - `"#CD2626"` or
  59. - `"#CD262680"` (50% transparency)
  60. - or the rgb function notation
  61. - `"rgb(255, 38, 38)"` or
  62. - `"rgba(255, 38, 28, 0.5)"` (50% transparency).
  63. - You can use the [HSL color specification][color-hsl] via the functions
  64. - `"hsl(270,60%,70%)"` or
  65. - `"hsl(270, 60%, 50%, .15)"` (15% transparency).
  66. In R, there are a number of ways to specify a color:
  67. - Use `rgb(205, 38, 38, maxColorValue = 255)` as an equivalent to the CSS `"rgb()"` function.
  68. - Without the `maxColorValue` argument, the `rgb()` function expects decimal numbers in the range [0, 1], like `rgb(0.8039, 0.1490, 0.1490)`.
  69. - The `rgb()` function also sets transparency via the `alpha` argument (in the [0, 1] range).
  70. - You can get the hexadecimal representation of a built-in R color using the
  71. `col2rgb()` function together with the `rgb()` function:
  72. ```{r}
  73. rgb(t(col2rgb("red")), maxColorValue = 255)
  74. ```
  75. ## Sizes
  76. In **xaringanthemer**, any template variable that accepts a CSS size (or length unit) ends with `_size`.
  77. Sizes are also used for positioning and those template variables end include `position` in their name.
  78. There are many units available in CSS sizes, but the three most common and easiest to use are pixels (`"px"`), percentage (`"%"`), and em units (`"em"`).
  79. Mozilla's devloper portal has a [full list of CSS length units](https://developer.mozilla.org/en-US/docs/Web/CSS/length).
  80. These sizes are either *absolute* or *relative* values.
  81. Relative values are set relative to the size of the parent element, but absolute values ignore the parent element.
  82. Pixels `"px"`
  83. : Pixels are an *absolute* size unit, traditionally representing one device pixel.
  84. E.g. `"16px"` or `"23px"`.
  85. Percentage `"%"`
  86. : Percentages are relative to the size of the parent element, scaled linearly.
  87. E.g. `"75%"` or `"150%"`.
  88. em Units `"em"`
  89. : em Units are just like percentages, except expressed as decimals.
  90. E.g. `"0.75em"` or `"1.5em"`.
  91. To make this more concrete, here is a simple "page" containing a section header and two paragraphs.
  92. ```html
  93. <div class="page">
  94. <h1>Section 1</h1>
  95. <p>This is paragraph 1...</p>
  96. <p>This is paragraph 2...</p>
  97. </div>
  98. ```
  99. Intuitively, you might want the section header to have a somewhat bigger font size than the paragraph text, but you don't want to have to set the text size for each and every paragraph or header.
  100. To do this, we can set the base size of any element inside the `<div class="page">`, and adjust the header size relatively.
  101. ```css
  102. .page {
  103. font-size: 16px;
  104. }
  105. h1 {
  106. font-size: 2em;
  107. }
  108. ```
  109. Now our paragraph font will be 16 pixels tall, and the level 1 headers will be twice as big.
  110. If we later decide to change the base font size, say to `"15px"`, the header text will still be twice as big as the paragraph text.
  111. ## Positioning
  112. ```{r echo=FALSE}
  113. tv_position <- grep("position", template_variables$variable, value = TRUE)
  114. tv_desc <- template_variables[template_variables$variable %in% tv_position,]$description
  115. tv_desc <- strsplit(tv_desc, ",")
  116. tv_desc <- purrr::map(tv_desc, ~ tolower(.[1]))
  117. ```
  118. If you're reading this, you're probably wondering how you make an element be *where you want it to be*.
  119. There are `r length(tv_position)` items that **xaringanthemer** can help you position:
  120. ```{r echo=FALSE, results='asis'}
  121. cat(paste0("- `", tv_position, "` (", tv_desc, ")"), sep = "\n")
  122. ```
  123. ### Footnote Position
  124. **xaringanthemer** provides one template variable to adjust the position of the footnote element.
  125. Footnotes can be insterted into a slide using the `.footnote[Here's my quick footnote]` syntax.
  126. The `footnote_position_bottom` argument adjust how far from the bottom of the slide the footnote appears.
  127. The default value is `"`r template_variables[template_variables$variable == "footnote_position_bottom", "default"]`"`,
  128. but you can adjust this value up or down to get the footnote where you want.
  129. ### Background Position
  130. The background position is set using the theme function arguments that end with `background_position`.
  131. See [this article on background-position](https://developer.mozilla.org/en-US/docs/Web/CSS/background-position#Syntax) from Mozilla for more information.
  132. Try any of the following values to get started:
  133. ```r
  134. background_position = "center"
  135. background_position = "top"
  136. background_position = "left"
  137. background_position = "bottom"
  138. background_position = "25% 75%" # X-value (from left) Y-value (from top)
  139. background_position = "bottom 10px right 20px" # 10px from bottom, 20px from right
  140. background_position = "top left 10px" # at top but adjusted left 10px
  141. ```
  142. ### General Positioning
  143. Read this section if you want to put a slide element at a specific spot on your slide.
  144. The `position` CSS element is used to specify where an element is located on the screen.
  145. Mozilla provides a [very good reference on positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/position) that I've summarized here.
  146. An element can be `"relative"`-ly positioned, `"absolute"`-ly positioned, `"fixed"`, `"sticky"` or `"static"` (default).
  147. For an element with a computed position (i.e. not `"static"`), you can also specify the `top`, `right`, `bottom`, and `left` CSS properties for that element.
  148. The `top`/`bottom` parameters specify vertical displacement, and the `right`/`left` specify horizontal displacement.
  149. Relatively positioned `position: relative`
  150. : For relatively positioned elements, the element position is adjusted relative to where it *would have been* if it were `static`.
  151. Absolutely positioned `position: absolute` or `position: fixed`
  152. : Absolutely positioned elements are positioned relative to the block that contains the element (called a containing block).
  153. A `fixed` element won't move with scrolling (but `fixed` is not recommended for remarkjs slides).
  154. If you want something to appear in a specific position on your slide, you'll need to use the `extra_css` argument of the **xaringanthemer** functions.
  155. For example, lets say you want a 300px by 300px box to appear on the right side of your slide, you'll need to create a special css class:
  156. ```r
  157. style_xaringan(
  158. extra_css = list(
  159. ".box-right" = list(
  160. "height" = "300px",
  161. "width" = "300px",
  162. "position" = "absolute",
  163. "top" = "33%",
  164. "left" = "65%"
  165. )
  166. )
  167. )
  168. ```
  169. This creates CSS like this:
  170. ````css
  171. ```{r echo=FALSE, results='asis'}
  172. cat(xaringanthemer:::list2css(list(
  173. ".box-right" = list(
  174. "height" = "300px",
  175. "width" = "300px",
  176. "position" = "absolute",
  177. "top" = "33%",
  178. "left" = "65%"
  179. )
  180. )))
  181. ```
  182. ````
  183. which you can then use in your slides by wrapping the slide content in `.box-right[]`.
  184. ```
  185. .box-right[
  186. Stuff inside the box
  187. ]
  188. ```