Update format

This commit is contained in:
Angelos Chalaris
2020-04-30 13:21:04 +03:00
parent d9fd2d23c3
commit 2fbd3e0737
55 changed files with 412 additions and 571 deletions

View File

@ -3,7 +3,13 @@ title: Transform centering
tags: layout,beginner
---
Vertically and horizontally centers a child element within its parent element using `position: absolute` and `transform: translate()` (as an alternative to `flexbox` or `display: table`). Similar to `flexbox`, this method does not require you to know the height or width of your parent or child so it is ideal for responsive applications.
Vertically and horizontally centers a child element within its parent element using `position: absolute` and `transform: translate()` (as an alternative to `flexbox` or `display: table`).
Similar to `flexbox`, this method does not require you to know the height or width of your parent or child so it is ideal for responsive applications.
- `position: absolute` on the child element allows it to be positioned based on its containing block.
- `left: 50%` and `top: 50%` offsets the child 50% from the left and top edge of its containing block.
- `transform: translate(-50%, -50%)` allows the height and width of the child element to be negated so that it is vertically and horizontally centered.
- Note that the fixed height and width on parent element is for the demo only.
```html
<div class="parent">
@ -27,10 +33,3 @@ Vertically and horizontally centers a child element within its parent element us
text-align: center;
}
```
#### Explanation
- `position: absolute` on the child element allows it to be positioned based on its containing block.
- `left: 50%` and `top: 50%` offsets the child 50% from the left and top edge of its containing block.
- `transform: translate(-50%, -50%)` allows the height and width of the child element to be negated so that it is vertically and horizontally centered.
- Note that the fixed height and width on parent element is for the demo only.