flex.Rd
Use flex()
to control how a flex container tag element places its flex
items or child tag elements. For more on turning a tag element into a flex
container see display()
. By default tag elements within a flex container
are treated as flex items.
flex( tag, direction = NULL, justify = NULL, align = NULL, wrap = NULL, reverse = NULL )
tag | A tag element. |
---|---|
direction | A responsive argument. One of |
justify | A responsive argument. One of |
align | A responsive argument. One of |
wrap | A responsive argument. One of |
reverse | A responsive argument. One of |
Other layout functions:
column()
,
fieldset()
,
navbar()
,
responsive
,
webpage()
### Different `direction`s # Many of `flex()`'s arguments are viewport responsive and below we will see # how useful this can be. On small screens the flex items are placed # vertically and can occupy the full width of the mobile device. On medium # or larger screens the items are placed horizontally once again. div( div("A flex item") %>% padding(3) %>% border(), div("A flex item") %>% padding(3) %>% border(), div("A flex item") %>% padding(3) %>% border() ) %>% display("flex") %>% flex( direction = list(xs = "column", md = "row") # <- ) %>% background("grey") %>% border()#> <div class="d-flex flex-column flex-md-row bg-grey border"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div># *Resize the browser for this example.* # You can keep items as a column by specifying only `"column"`. div( div("A flex item") %>% padding(3) %>% border(), div("A flex item") %>% padding(3) %>% border(), div("A flex item") %>% padding(3) %>% border() ) %>% display("flex") %>% flex(direction = "column") # <-#> <div class="d-flex flex-column"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div>### Spacing items with `justify` # Below is a series of examples showing how to change the horizontal # alignment of your flex items. Let's start by pushing items to the # beginning of their parent container. div( replicate( div("A flex item") %>% padding(3) %>% border(), n = 5, simplify = FALSE ) ) %>% display("flex") %>% flex(justify = "start") # <-#> <div class="d-flex justify-content-start"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div># We can also push items to the **end**. div( replicate( div("A flex item") %>% padding(3) %>% border(), n = 5, simplify = FALSE ) ) %>% display("flex") %>% flex(justify = "end") # <-#> <div class="d-flex justify-content-end"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div># Without using a table layout we can **center** items. div( replicate( div("A flex item") %>% padding(3) %>% border(), n = 5, simplify = FALSE ) ) %>% display("flex") %>% flex(justify = "center") # <-#> <div class="d-flex justify-content-center"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div># You can also put space **between** items div( replicate( div("A flex item") %>% padding(3) %>% border(), n = 5, simplify = FALSE ) ) %>% display("flex") %>% flex(justify = "between") # <-#> <div class="d-flex justify-content-between"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div># ... or put space **around** items. div( replicate( div("A flex item") %>% padding(3) %>% border(), n = 5, simplify = FALSE ) ) %>% display("flex") %>% flex(justify = "around") # <-#> <div class="d-flex justify-content-around"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div># *The "between" and "around" values come from the original CSS values # "space-between" and "space-around".* ### Wrap onto new lines # Using flexbox we can also control how items wrap onto new lines. div( replicate( div("A flex item") %>% padding(3) %>% border(), n = 5, simplify = FALSE ) ) %>% display("flex") %>% flex(wrap = TRUE)#> <div class="d-flex flex-wrap"> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> <div class="p-3 border">A flex item</div> #> </div>