CSS - opacity_alpha channel&position properties&transition&transform&flexbox&responsive design
Tags: review, css, programming
Published: 2022-06-12 05:56:52

### Contents

- Opacity & Alpha Channel
- position properties
- transition
- transform
- flexbox
- Responsive design

#### 1.Opacity & Alpha Channel

- Opacity
  The opacity CSS property sets the opacity of an element. Opacity is the degree to which content behind an element is hidden. It applies to the element as a whole, including its contents. Value from 0 to 1.
  ```css
  div {
    opacity: 0.33;
  }
  ```

* Alpha Channel
  The alpha channel is a color component that represents the degree of opacity of a color. Value from 0 to 1.
  ```css
  div {
    background-color: rgba(0, 209, 112, 0.33);
  }
  ```

#### 2.position properties

- position: static;
  <mark>The element is positioned according to the normal flow of the document. The `top`, `right`, `bottom`, `left`, and `z-index` properties have no effect. This is the default value.</mark> (Normal document flow is the natural flow of elements on a page before we apply any positional properties to them.)
- position: relative;
  <mark>The element is positioned according to the normal flow of the document, and then offset relative to itself based on the values of `top`, `right`, `bottom`, and `left`.</mark>
- position: absolute;
  <mark>The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest <b>positioned</b> ancestor(no matter what kind of position its ancestor has, only if its ancestor is positioned, then position: absolute will work fine),</mark> if any; otherwise, it is placed relative to the initial contianing block. <mark>Its final position is determined by the values of `top`, `right`, `bottom`, and `left`.</mark>
- position: fixed;
  <mark>The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to the initial containing block established by the viewport. It always stays in the same place even if the page is scrolled.</mark>
- position: sticky;
  <mark>It basically acts like position: relative until an element is scrolled beyond a specific offset, in which case it turns into position: fixed, causing the element to "stick" to its position instead of being scrolled out of view.</mark>

#### 3.transition

The transition property is a shorthand property for transition-property, transition-duration, transition-timing-function, and transition-delay.

- transition-property - It sets the CSS properties to which a transition effect should be applied. e.g. margin-right, color, all, none.etc.

* transition-duration - It sets the length of time a transition animation should take to complete. By default, the value is 0s. We have `s` and `ms` units.

* transition-timing-function - It sets how intermediate values are calculated for CSS properties being affected by a transition effect. e.g. linear, ease-in, ease-out.etc.

* transition-delay - It specifies the duration to wait before starting a property's transition effect when its value changes. We have `s` and `ms` units.

<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transition">transition - MDN</a>

#### 4.transform

The transform property lets you rotate, scale, skew, or translate an element. It modifies the coordinate space of the CSS visual formatting model. e.g. transform: rotate(45deg), transform: scale(2), transform: translate(x-axis, y-axis), transform: skew(10deg, 5deg).

<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform">transform - MDN</a>

#### 5.flexbox

Flexbox is a one-dimensional layout method for arranging items in rows or columns. Items flex to fill additional space or shrink to fit into smaller spaces.

- The flex model
  When elements are laid out as flex items, they are laid out along two axes:
  <img src="https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox/flex_terms.png">
  - main axis: It is the axis running in the direction the flex items are laid out in. The start and end of this axis are called the main start and main end.
  - cross axis: It is the axis running perpendicular to the direction the flex items are laid out in. The start and end of this axis are called the cross start and cross end.
  * flex container: The parent element that has `display: flex` set on it.
  * flex items: The items laid out as flexible boxes inside the flex container.

* flex-direction
  It specifies which direction the main axis runs. By default this is set to row.

  - <mark>row-reverse</mark> - The main-start and main-end points are opposite to the content direction.
  - <mark>column-reverse</mark> - The main-start and main-end are opposite to the content direction.

* flex-wrap
  One issue that arises when you have a fixed width or height in your layout is that eventually your flexbox children will overflow their container, breaking the layout. Here, to solve this, we use flex-wrap property. It defines if flexbox items appear on a single line or on multiple lines within a flexbox container.

  - flex-wrap: nowrap;
    The flexbox items will remain on a single line, no matter what, and will eventually overflow if needed. This is also the default value.
  - flex-wrap: wrap;
    The flexbox items will be distributed among multiple lines if needed.
  - flex-wrap: wrap-reverse;
    <mark>Behaves the same as wrap but cross-start and cross-end are permuted.</mark>

* <mark>flex-flow</mark>
  Shorthand property for flex-direction and flex-wrap. <mark>Format: flex-flow: flex-direction flex-wrap;</mark>

* flex-grow
  Defines how much a flexbox item should grow if there's space available.
  - flex-grow: 0;
    The element will not grow if there's space available. It will only use the space it needs.
  - flex-grow: 1;
    The element will grow by a factor of 1. It will fill up the remaining space if no other flexbox item has a flex-grow value.
  - flex-grow: 2;
    Because the flex-grow value is relative, its behavior depends on the value of the flexbox item siblings.
* flex-shrink
  Defines how much a flexbox item should shrink if there's not enough space available.
  - flex-shrink: 1;
    If there's not enough space available in the container's main axis, the element will shrink by a factor of 1, and will wrap its content.
  - flex-shrink: 0;
    The element will not shrink: it will retain the width it needs, and not wrap its content. Its siblings will shrink to give space to the target element.
  - flex-shrink: 2;
    Because the flex-shrink value is relative, its behavior depends on the value of the flexbox item siblings.
* flex-basis
  Defines the initial size of a flexbox item.

  - flex-basis: auto;
    The element will be automatically sized based on its content, or on any height or width value if they are defined. This is the default value.
  - flex-basis: 80px;
    You can define pixle or (r)em values. The element will wrap its content to avoid any overflow.

* flex - This property is a shorthand for `flex-grow`, `flex-shrink` and `flex-basis`. Format as below:

  - flex: flex-grow flex-shrink flex-basis;
  - flex: flex-grow flex-shrink; flex: flex-grow flex-basis;
  - flex: flex-grow;

* Horizontal and vertical alignment
  You can also use flexbox features to align flex items along the main or cross axis.

  - align-items
    Defines how flexbox items are aligned according to the cross axis, within a line of a flexbox container.
    - align-items: flex-start; - The flexbox items are aligned at the start of the cross axis.
    * align-items: flex-end; - The flexbox items are aligned at the end of the cross axis.
    * align-items: center; - The flexbox items are aligned at the center of the cross axis.
    * align-items: baseline; - The flexbox items are aligned at the baseline of the cross axis. <mark>The baseline value will align flex items along their content's baseline.</mark>
    * align-items: stretch; - The flexbox items will stretch across the whole cross axis.

  * align-self
    Works like align-items, but applies only to a single flexbox item, instead of all of them.
    - align-self: auto; - The target will use the value of align-items.
  * justify-content
    Defines how flexbox items are aligned according to the main axis, within a flexbox container.
    - justify-content: flex-start; - The flexbox items are pushed towards the start of the container's main axis. This is the default value.
    - justify-content: flex-end; - The flexbox items are pushed towards the end of the container's main axis.
    - justify-content: center; - The flexbox items are centered along the container's main axis.
    - justify-content: space-between; - The remaining space is distributed between the flexbox items.
    - justify-content: space-around; - The remaining space is distributed around the flexbox items: this adds space before the first item and after the last one.
  * align-content
    Defines how each line is aligned within a flexbox container. It only applies if flex-wrap: wrap is present, and if there are multiple lines of flexbox items.
    - align-content: stretch; - Each line will stretch to fill the remaining space. This is the default value.
    - align-content: flex-start; - Each line will only fill the space it needs. They will all move towards the start of the flexbox conainer's cross axis.
    - align-content: flex-end; - Each line will only fill the space it needs. They will all move towards the end of the flexbox container's cross axis.
    - align-content: center; - Each line will only fill the space it needs. They will all move towards the center of the flexbox container's cross axis.
    - align-content: space-between; - Each line will only fill the space it needs. The remaining space will appear between the lines.
    - align-content: space-around; - Each line will only fill the space it needs. The remaining space will be distributed equally around the lines: before the first line, between the two, and after the last one.

* order
  Defines the order of a flexbox item without changing the HTML code. Flexbox items will be distributed according to the size of order's value.
  - order: 0; - The order of the flexbox items is the one defined in the HTML code. This is the default value.

<a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox">flexbox - MDN</a>
<a href="https://cssreference.io/flexbox/">flexbox - CSS Reference</a>

#### 6.Responsive design

Because more diverse screen sizes became available, the concept of responsive web design(RWD) appeared, a set of practices that allows web pages to alter their layout and appearance to suit different screen widths, resolutions, etc.

- Media Queries
  Media queries are a key component of responsive design. And they allow us to run a series of tests and apply CSS selectively to style the page appropriately for the user's needs. For example, the following media query tests to see if the current web page is being displayed as screen media and the viewport is at least 800 pixels wide. The CSS for the `.container` selector will only be applied if these two things are true.

```css
@media screen and (min-width: 800px) {
  .container {
    margin: 1em 2em;
  }
}
```

<a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Responsive_Design">Responsive design - MDN</a>