+ - 0:00:00
Notes for current slide
Notes for next slide

Look Sharp!

Style with CSS

Garrick Aden-Buie

rstudio::conf(2020, "JavaScript for Shiny Users")

1

Quick Review: Block v Inline

Which type of display doesn't have a width or height?

2

Quick Review: Block v Inline

Which type of display doesn't have a width or height?

Which type of display is shown on its own line?

3

Quick Review: Block v Inline

Which type of display doesn't have a width or height?

Which type of display is shown on its own line?

What are some examples of inline elements?

4

Quick Review: Block v Inline

Which type of display doesn't have a width or height?

Which type of display is shown on its own line?

What are some examples of inline elements?

Examples of block elements?

5

Display

.not-down {
display: block;
background: red;
color: white;
}
<p class="not-down">
...never gonna...
</p>

He sings the songs that remind him of the good times. He sings the songs that remind him of the best times.

Oh, Danny Boy.

I get knocked down, but I get up again. You're never gonna to keep me down.

6

Display

.best {
display: inline;
background: red;
color: white;
}
<span class="best">
the best times
</span>

He sings the songs that remind him of the good times. He sings the songs that remind him of the best times.

Oh, Danny Boy.

I get knocked down, but I get up again. You're never gonna to keep me down.

7

Display

.danny {
display: none;
background: red;
color: white;
}
<p class="danny">
Oh Danny Boy
</span>

He sings the songs that remind him of the good times. He sings the songs that remind him of the best times.

Oh, Danny Boy.

I get knocked down, but I get up again. You're never gonna to keep me down.

8

CSS Box Model

9

Everything is (in) a box

js4shiny.com/box-size

10
  • Focus on the parts of the box

Border and Shorthand Properties

border: 1px solid turquoise;

11

Border and Shorthand Properties

border: 1px solid turquoise;

border-width: 1px;
border-style: solid;
border-color: turquoise;
12

Margin and Padding

margin: 1em;

margin-top: 1em;
margin-right: 1em;
margin-bottom: 1em;
margin-left: 1em;

padding: 1em;

padding-top: 1em;
padding-right: 1em;
padding-bottom: 1em;
padding-left: 1em;
13

Margin and Padding

margin: 0 1em;

margin-top: 0;
margin-right: 1em;
margin-bottom: 0;
margin-left: 1em;

padding: 0 1em;

padding-top: 0;
padding-right: 1em;
padding-bottom: 0;
padding-left: 1em;
14

Margin and Padding

margin: 1em 0;

margin-top: 1em;
margin-right: 0;
margin-bottom: 1em;
margin-left: 0;

padding: 1em 0;

padding-top: 1em;
padding-right: 0;
padding-bottom: 1em;
padding-left: 0;
15

Margin and Padding

margin: 1px 2px 3px 4px;

margin-top: 1px;
margin-right: 2px;
margin-bottom: 3px;
margin-left: 4px;

padding: 1px 2px 3px 4px;

padding-top: 1px;
padding-right: 2px;
padding-bottom: 3px;
padding-left: 4px;
16

Earlier we saw

<p style="text-align: center">
<img src="http://placekitten.com/200/200" />
</p>

17

Earlier we saw

<p style="text-align: center">
<img src="http://placekitten.com/200/200" />
</p>

Is <img> inline or block?

18

Image is inline but we can make it block

<p style="text-align: center">
<img style="display:block;" src="http://placekitten.com/200/200" />
</p>
19

What will happen?

Image is inline but we can make it block

<p style="text-align: center">
<img style="display:block;" src="http://placekitten.com/200/200" />
</p>

20

Image is inline but we can make it block

<p style="text-align: center">
<img style="display:block;" src="http://placekitten.com/200/200" />
</p>

#out-kitten-block-img :hover {
background: var(--washed-red);
outline: 1px solid var(--red);
}
21

Image is inline but we can make it block

<div class="kitten-container">
<div class="kitten"></div>
</div>
22

Image is inline but we can make it block

<div class="kitten-container">
<div class="kitten"></div>
</div>
.kitten {
margin: 0 auto;
height: 200px;
width: 200px;
background: url('http://placekitten.com/200/200');
}
23

Center Text

.centered {
text-align: center
}
meow
24

Center Block

.centered {
display: block;
width: 33%;
margin-left: auto;
margin-right: auto;
}
25

Center Block

.centered {
display: block;
width: 33%;
margin: 0 auto;
}
26

27

Vertical Center

.container-of-item {
display: flex;
align-items: center;
}
28

All Center

.container-of-item {
display: flex;
align-items: center;
justify-content: center;
}
29

CSS Selectors

30

Joining Selectors

<div class="card card-eyeshadow">
<header>
<div class="pan">
<div class="shade almost-there"></div>
</div>
<h1 id="item">Super Shock Shadow</h1>
</header>
<p>By <em class="brand">Colourpop</em></p>
<div id="cost" class="us-dollar">$8</div>
</div>
31

Joining Selectors

<div class="card card-eyeshadow">
<header>
<div class="pan">
<div class="shade almost-there"></div>
</div>
<h1 id="item">Super Shock Shadow</h1>
</header>
<p>By <em class="brand">Colourpop</em></p>
<div id="cost" class="us-dollar">$8</div>
</div>
32

Multiple Rules

h1, p

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

33

Same Element

div#cost.us-dollar

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

34

Descendent of

header .pan

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

35

Descendent of

.card-eyeshadow .pan

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

36

Descendent of

.card .shade

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

37

Descendent of

.card .shade

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div&gt
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

38

Direct child of

.card > div

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

39

Direct child of

p > .brand

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

40

Direct child of

p > .brand

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

41

Siblings

header ~ #cost

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

42

Immediate sibling

header + p

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
</div>

43

Siblings

p ~ div

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

44

Siblings

p ~ div

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

45

Siblings

p + div

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

46

Siblings

p + div

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

47

Beyond Element, ID, Class

Type Example Selector
Element p
Class .shiny
IDs #shiny
Pseudo-classes :hover, :active, :empty
Attributes [attribute]
Pseudo-elements ::before
Anything *
48

Attribute selectors are awesome and are super helpful in tricky situations where you don't have control over the HTML or the classes (hello shiny!)

Attribute Selectors

div[id]

A <div> that has an id attribute

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

49

Attribute Selectors

div[id="cost"]

A <div> that has an id attribute whose value is cost

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

50

Attribute Selectors

[class*="brand"]

An element where brand appears in the class attribute

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

51

Attribute Selectors

[class*="brand"]

An element where brand appears in the class attribute

<div class="card card-eyeshadow">
  <header>
    <div class="pan">
      <div class="shade almost-there"></div>
    </div>
    <h1 id="item">Super Shock Shadow</h1>
  </header>
  <p>By <em class="brand">Colourpop</em></p>
  <div id="cost" class="us-dollar">$8</div>
  <div class="brand-image">...</div>
</div>

52

Attribute Selectors

53

Cascade

54

We're usually not creating the CSS from scratch, instead we modify existing CSS to make things look better.

The browser is a jumble of competing CSS rules.

When two rules collide...

h1 .shiny {
color: lightblue;
}
#title.shiny {
color: red;
}

...which one wins?

56

CSS Cascade

Importance

What type of rule is it?

  1. transition

  2. !important

  3. animation

  4. normal

.shiny {
color: lightblue;
}
.shiny {
color: blue !important;
}
57

CSS Cascade

Importance

Origin

Where was the rule defined?

  1. website

  2. user

  3. browser

/* browser default */
p {
color: black;
}
/* shiny.css */
p {
color: lightblue;
}
58

CSS Cascade

Importance

Origin

Specificity

How specific is the rule?

  1. inline styles style="color: lightblue"

  2. id #shiny

  3. class, attribute, pseudo-class .shiny:hover

  4. element, pseudo-elements strong

.shiny {
color: blue;
}
#shiny {
color: lightblue;
}
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

CSS Cascade

Importance

Origin

Specificity

The most points on the highest level wins

p.shiny {
color: blue;
}
.shiny:first-child {
color: lightblue;
}
85

CSS Cascade

Importance

Origin

Specificity

The most points on the highest level wins

Ties can be broken within specificity by points one level down

#shiny .paragraph {
color: blue;
}
#shiny p {
color: lightblue;
}
86

CSS Cascade

Importance

Origin

Specificity

The most points on the highest level wins

Ties can be broken within specificity by points one level down

How selectors are combined doesn't matter

#shiny ~ p {
color: blue;
}
#shiny + p {
color: lightblue;
}
87

CSS Cascade

Importance

Origin

Specificity

Position

Which rule was defined last?

.shiny {
color: blue;
}
.shiny {
color: red;
}
88

Test Your CSS Aim

89

Quick Review: Block v Inline

Which type of display doesn't have a width or height?

2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
oTile View: Overview of Slides
Esc Back to slideshow