Flexbox Flashcards

1
Q

What is flexbox?

A

The Flexible Box Module, usually referred to as flexbox, was designed as a one-dimensional layout model, and as a method that could offer space distribution between items in an interface and powerful alignment capabilities.

When we describe flexbox as being one dimensional we are describing the fact that flexbox deals with layout in one dimension at a time — either as a row or as a column. This can be contrasted with the two-dimensional model of CSS Grid Layout, which controls columns and rows together.

Flexbox is unlike previous display values (inline, inline-block, and so on), which only affect the elements they are applied to. Instead, a flex container asserts control over the layout of the elements within.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does display: flex; do to an element?

A

Applying display: flex; to an element turns it into a flex container, and its direct children turn into flex items.

  • The flex container fills the available width like a block element.
  • The flex items may not necessarily fill the width of their flex container.
  • The flex items are all the same height, determined naturally by their contents.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does display: inline-flex do to an element?

A

display: inline-flex creates a flex container that behaves more like an inline-block element rather than a block. It flows inline with other inline elements, but it won’t automatically grow to 100% width. Flex items within it generally behave the same as with display: flex.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you create a flex container?

A

By applying

display: flex;

to the container element, it becomes a fex container. Its child elements will become the same height by default.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two axes of flexbox?

A

When working with flexbox you need to think in terms of two axes — the main axis and the cross axis. The main axis is defined by the flex-direction property, and the cross axis runs perpendicular to it.

source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you define flexbox’s main axis?

A

With the flex-direction property.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the possible values of the flex-direction property?

A
  • row - Main axis will run along the row in the inline direction (same as writing mode, left-to-right for english).
  • row-reverse - Main axis will run along the row in the reverse inline direction (reverse of writing mode, right-to-left for english).
  • column - Main axis will run from the top of the page to the bottom — in the block direction.
  • column-reverse - Main axis will run from the bottom of the page to the top — in the reverse block direction.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the flexbox cross axis if you define the main axis with flex-direction: row/row-reverse;?

A

The cross axis runs perpendicular to the main axis, therefore if your flex-direction (main axis) is set to row or row-reverse the cross axis runs down the columns.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the flexbox cross axis if you define fine the main axis with flex-direction: column/column-reverse;?

A

If your main axis is column or column-reverse then the cross axis runs along the rows.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a flex item?

A

The direct children of a Flex Container (elements with display: flex or display: inline-flex set on them) become flex items.

Continuous runs of text inside flex containers will also become flex items.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the peculiarities of flexbox start/end of lines?

A

If the flex-direction is row and I am working in English, then the start edge of the main axis will be on the left, the end edge on the right.

If I were to work in Arabic, then the start edge of my main axis would be on the right and the end edge on the left.

After a while, thinking about start and end rather than left and right becomes natural, and will be useful to you when dealing with other layout methods such as CSS Grid Layout which follow the same patterns.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a flex container?

A

An area of a document laid out using flexbox is called a flex container. To create a flex container, we set the value of the area’s container’s display property to flex or Inline-flex.

.container {
 display: flex;
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does flex-wrap: wrap do?

A

If you add flex-wrap with a value of wrap to the flex container. Should your items be too large to all display in one line, they will wrap onto another line.

NOTE: If the flex-direction is column or column-reverse, then flex-wrap: wrap will allow the flex items to overflow into a new column. However, this only happens if something constrains the height of the container; otherwise, it grows to contain its flex items.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does flex-wrap: nowrap do?

A

If flex-wrap is set to nowrap, which is also the initial value, and the flex-items will shrink to fit the container because they are using initial flexbox values that allows items to shrink.

NOTE: Using nowrap would cause an overflow if the items were not able to shrink, or could not shrink small enough to fit.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the possible values of flex-wrap?

A

flex-wrap accepts the following values:

  • nowrap - The flex items are laid out in a single line which may cause the flex container to overflow. The cross-start is either equivalent to start or before depending on the flex-direction value. This is the default value.
  • wrap - The flex items break into multiple lines. The cross-start is either equivalent to start or before' depending flex-direction` value and the cross-end is the opposite of the specified cross-start.
  • wrap-reverse - Behaves the same as wrap but cross-start and cross-end are permuted.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the flex-flow property set and what are its default values?

A

The flex-flow CSS shorthand property specifies the direction of a flex container, as well as its wrapping behaviour.
This property is a shorthand for the following CSS properties:

flex-direction
flex-wrap

Its initial values are:

flex-direction: row;
flex-wrap: nowrap;

example: flex-flow: row nowrap;

Source MDN

17
Q

What properties can be applied to flex-items?

A
  • flex-grow : sets the flex grow factor of a flex item’s main size
  • flex-shrink: sets the flex shrink factor of a flex item. If the size of all flex items is larger than the flex container, items shrink to fit according to flex-shrink.
  • flex-basis : sets the initial main size of a flex item. It sets the size of the content box unless otherwise set with box-sizing.

Source MDN

18
Q

What does flex-basis property do?

A

The flex-basis CSS property sets the initial main size of a flex item. Size is calculated with content box rules unless otherwise set with box-sizing property.

Source MDN

19
Q

What are the possible values of flex-basis?

A

The flex-basis CSS property sets the initial main size of a flex item. It sets the size of the content box unless otherwise set with box-sizing. Its possible values are:

  • 0 (cero) - the flex item initial width will be the width of the content
  • auto (initial value) - The browser looks to see if the items have a size (width/height). If the items don’t have a size then the content’s size is used as the flex-basis.
  • fixed size for example 200px - the initial width of the component will be whatever size we’ve set

Source MDN

20
Q

What does flex-grow property do?

A

With the flex-grow property set to a positive integer, flex items can grow along the main axis from their flex-basis. This will cause the item to stretch and take up any available space on that axis, or a proportion of the available space if other items are allowed to grow too.

If we gave all of our items a flex-grow value of 1 then the available space in the flex container would be equally shared between our items and they would stretch to fill the container on the main axis.

The flex-grow property can be used to distribute space in proportion. If we give our first item a flex-grow value of 2, and the other items a value of 1 each, 2 parts of the available space will be given to the first item, 1 part each the other items.

Source MDN

21
Q

What are the possible values of flex-grow?

A

<numbers>. Negative values are invalid. Defaults to 0.

Source MDN

22
Q

What are the possible values of flex-shrink?

A

<numbers>. Negative values are invalid. Defaults to 1.

Source MDN

23
Q

what does the flex-shrink property do?

A

The flex-shrink property controls how space is taken away.

If we do not have enough space in the container to lay out our items, and flex-shrink is set to a positive integer, then the item can become smaller than the flex-basis.

Different values can be assigned in order to cause one item to shrink faster than others — an item with a higher value set for flex-shrink will shrink faster than its siblings that have lower values.

Source MDN

24
Q

What does the flex shorthand property set and what are its initial values?

A

The flex shorthand allows you to set the three values in this order — flex-grow, flex-shrink, flex-basis.
And its initial values are:

 flex-grow: 0;
 flex-shrink: 1;
 flex-basis: auto;

Source MDN

25
Q

What are the possible values of flex shorthand property?

A
  • initial - The item is sized according to its width and height properties. It shrinks to its minimum size to fit the container, but does not grow to absorb any extra free space in the flex container. This is equivalent to setting flex: 0 1 auto.
  • auto - The item is sized according to its width and height properties, but grows to absorb any extra free space in the flex container, and shrinks to its minimum size to fit the container. This is equivalent to setting flex: 1 1 auto.
  • none - The item is sized according to its width and height properties. It is fully inflexible: it neither shrinks nor grows in relation to the flex container. This is equivalent to setting flex: 0 0 auto.
  • <flex-grow> - Defines the flex-grow of the flex item. Negative values are considered invalid. Defaults to 1 when omitted. (initial is 0)
  • <flex-shrink> - Defines the flex-shrink of the flex item. Negative values are considered invalid. Defaults to 1 when omitted. (initial is 1)
  • <flex-basis> - Defines the flex-basis of the flex item. A preferred size of 0 must have a unit to avoid being interpreted as a flexibility. Defaults to 0 when omitted. (initial is auto)

Source MDN

26
Q

How can you specify the values of flex shorthand property?

A

The flex property may be specified using one, two, or three values.

One-value syntax

  • the value must be one of:
  • a<number>: In this case it is interpreted as flex: <number> 1 0;
  • a <width>: In this case it is interpreted as flex: 1 1 <width>;
  • one of the keywords: none, auto, or initial.

Two-value syntax:

  • The first value must be:
  • a <number> and it is interpreted as flex-grow value
  • The second value must be one of:
  • a <number>: then it is interpreted as flex-shrink value
  • a valid value for width: then it is interpreted as flex-basis value

Three-value syntax - the values must be in the following order:
* a <number> for flex-grow.
* a <number> for flex-shrink.
* a valid value for width for flex-basis.

Source MDN

27
Q

What does align-items property do?

A

In flexbox, it controls the alignment of items on the Cross Axis. In Grid Layout, it controls the alignment of items on the Block Axis within their grid area.

Source MDN

28
Q

What are the possible values of align-items property?

A

The align-items property accepts 5 different values:

  • flex-start: cross-start margin edge of the items is placed on the cross-start line
  • flex-end: cross-end margin edge of the items is placed on the cross-end line
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align
  • stretch (default): stretch to fill the container (still respect min-width/max-width)

Note: If you check MDN you’ll see that there are other possible values but, the ones listed above are most common ones.

Source CSS-Tricks

29
Q

What does CSS justify-content property do?

A

The CSS justify-content property defines how the browser distributes space between and around content items along the main-axis of a flex container, and the inline axis of a grid container.

Source MDN

30
Q

What are the possible values of justify-content property?

A

The justify-content property accepts five different values:

  • flex-start (default): items are packed toward the start line
  • flex-end: items are packed toward to end line
  • center: items are centered along the line
  • space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
  • space-around: items are evenly distributed in the line with equal space around them
  • space-evenly: items are distributed so that the spacing between any two adjacent alignment subjects, before the first alignment subject, and after the last alignment subject is the same

Note: If you check MDN you’ll see that there are other possible values. But the ones above are the most common ones.

Source CSS-Tricks

31
Q

What sets the base size of a flex-item?

A
  1. Is flex-basis set to auto, and does the item have a width set? If so, the size will be based on that width.
  2. Is flex-basis set to auto or content (in a supporting browser)? If so, the size is based on the item size.
  3. Is flex-basis a length unit, but not zero? If so this is the size of the item.
  4. Is flex-basis set to 0? if so then the item size is not taken into consideration for the space-sharing calculation.

Source MDN

32
Q

How can you control the size and flexibility of the flex-items long the main axis?

A

With the following CSS properties:

  • flex-grow: How much of the positive free space does this item get?
  • flex-shrink: How much negative free space can be removed from this item?
  • flex-basis: What is the size of the item before growing and shrinking happens?

You can use the flex shorthand property to define them all at once.

Source MDN

33
Q

What should you keep in mind when working with a vertical flexbox?
(flex-direction: column/column-reverse)

A

In CSS, working with height is fundamentally different than working with widths. A flex container will be 100% the available width, but the height is determined naturally by its contents. This behaviour does not change when you rotate the main axis.

The flex container’s height is determined by its flex items. They fill it perfectly. In a vertical flexbox, flex-grow and flex-shrink applied to the items will have no effect unless something else forces the height of the flex container to a specific size.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

34
Q

What does CSS align-content property do?

A

If you enable wrapping (using flex-wrap), this property controls the spacing of each row inside the flex container along the cross axis.

Note: This property has no effect on single line flex containers (i.e. ones with flex-wrap: nowrap).

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

35
Q

what are the possible values of align-content property?

A

The align-content property most common values are:

  • flex-start: lines packed to the start of the container
  • flex-end: lines packed to the end of the container
  • center: lines packed to the center of the container
  • space-between: lines evenly distributed; the first line is at the start of the container while the last one is at the end
  • space-around: lines evenly distributed with equal space between them
  • stretch (default): lines stretch to take up the remaining space

NOTE: According to MDN there are other possible values. But the ones above are the most common ones.

Source CSS-Tricks

36
Q

What does CSS order property do?

A

By using the order property, you can change the order the items are stacked. You may specify any integer, positive or negative. If multiple flex items have the same value, they’ll appear according to source order.

Initially, all flex items have an order of 0. Specifying a value of -1 to one item will move it to the beginning of the list, and a value of 1 will move it to the end. The numbers don’t necessarily need to be consecutive.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

37
Q

What are the concerns of order property?

A

Accessibility concerns
Using the order property will create a disconnect between the visual presentation of content and DOM order. This will adversely affect users experiencing low vision navigating with the aid of assistive technology such as a screen reader. If the visual (css) order is important, then screen reader users will not have access to the correct reading order.

User navigation concerns
Navigation using the Tab key will still follow the source order in most browsers, which can be confusing.

Source MDN

38
Q

What is the full-page layout problem FOUC?

A

As the browser loads content, it progressively renders it to the screen, even as it continues to download the remainder of the page. Assume you have a three-column layout, built using a flexbox (flex-direction: row). If the content for two of these columns loads, the browser might render them before it loads the content for the third column. Then, when the rest of the content loads, the browser recalculates the sizes of each flex item and renders the page again. The user will see a two-column layout momentarily, then the columns will resize (perhaps drastically), and the third column will appear.

This effect is also known as FOUC (Flash of Unstyled Content). Jake Archibald, a developer advocate for Google Chrome, has written about this at https://jakearchibald.com/2014/dont-use-flexbox-for-page-layout/.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

39
Q

flex-start and flex-end compared to start and end

A

flex-start and flex-end take into account the flex direction while start and end take into account the writing mode and script direction

For example:

.content {
 display: flex;
 flex-direction: row;
}

Both justify-contents: flex-start; and justify-contents: start; will distribute the flex-item starting from the main axis start edge (left side) . But with the following css.

.content {
 display: flex;
 flex-direction: row-reverse;
}

justify-contents: flex-start; will distribute the flex-item starting from the main axis start edge (right side). While and justify-contents: start; will distribute the items from the writing mode and script direction start (left side for english speakers)

Note: on reverse direction the order of the items will always be reversed it is just the alignment that will change between flex-start and start.