xaringanthemer CSS Help ================ Garrick Aden-Buie 2018-05-09 Jump to: [Colors](#colors), [Sizes](#sizes), [Positioning](#positioning) ## About this vignette 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. See `vignette("template-variables", package = "xaringanthemer")` for a full list of the theme functions. 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. For example, we can create an R variable with a specific color that is used in several places in a theme ``` r firebrick <- "#CD2626" write_xaringan_theme( header_color = firebrick, link_color = firebrick ) ``` or we can directly give the character string ``` r write_xaringan_theme( header_color = "#CD2626", link_color = "#CD2626" ) ``` in both cases, we get CSS like the following that sets the link color ``` css a, a > code { color: #CD2626; } ``` Note that when a string is given to the theme function, the outer quotes are removed. 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. ## Colors In CSS, text colors are specified with the `color:` property, background colors use `background-color:`, and border colors use `border-color:`. In **xaringanthemer**, template variables that set - text color end with `_color`; - background color end with `_background_color`; - border color end with `_border_color`; ### Setting colors In CSS, there are a number of ways to specify a color: - You can use a [color keyword](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#Color_keywords), such as `"darkslategray"` or `"red"`. - You can use the [RGB color specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb\(\)_and_rgba\(\)) either via - the hexadecimal representation - `"#CD2626"` or - `"#CD262680"` (50% transparency) - or the rgb function notation - `"rgb(255, 38, 38)"` or - `"rgba(255, 38, 28, 0.5)"` (50% transparency). - You can use the [HSL color specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl\(\)_and_hsla\(\)) via the functions - `"hsl(270,60%,70%)"` or - `"hsl(270, 60%, 50%, .15)"` (15% transparency). In R, there are a number of ways to specify a color: - Use `rgb(205, 38, 38, maxColorValue = 255)` as an equivalent to the CSS `"rgb()"` function. - Without the `maxColorValue` argument, the `rgb()` function expects decimal numbers in the range \[0, 1\], like `rgb(0.8039, 0.1490, 0.1490)`. - The `rgb()` function also sets transparency via the `alpha` argument (in the \[0, 1\] range). - You can get the hexadecimal representation of a built-in R color using the `col2rgb()` function together with the `rgb()` function: ``` r rgb(t(col2rgb("red")), maxColorValue = 255) #> [1] "#FF0000" ``` ## Sizes In **xaringanthemer**, any template variable that accepts a CSS size (or length unit) ends with `_size`. Sizes are also used for positioning and those template variables end include `position` in their name. 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"`). Mozilla’s devloper portal has a [full list of CSS length units](https://developer.mozilla.org/en-US/docs/Web/CSS/length). These sizes are either *absolute* or *relative* values. Relative values are set relative to the size of the parent element, but absolute values ignore the parent element. - Pixels `"px"` Pixels are an *absolute* size unit, traditionally representing one device pixel. E.g. `"16px"` or `"23px"`. - Percentage `"%"` Percentages are relative to the size of the parent element, scaled linearly. E.g. `"75%"` or `"150%"`. - em Units `"em"` em Units are just like percentages, except expressed as decimals. E.g. `"0.75em"` or `"1.5em"`. To make this more concrete, here is a simple “page” containing a section header and two paragraphs. ``` html
This is paragraph 1...
This is paragraph 2...