Add transform centering snippet

This commit is contained in:
Sean Vinci
2018-03-30 10:59:28 -04:00
committed by GitHub
parent 41aabe66de
commit 26b5e9ed11

View File

@ -0,0 +1,67 @@
### Transform Centering
Vertically and horitonally 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.
#### HTML
```html
<div class="parent">
<div class="child">Centered content</div>
</div>
```
#### CSS
```css
.parent {
border: 1px solid #333;
height: 250px;
position: relative;
width: 250px;
}
.child {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
}
```
#### Demo
<div class="snippet-demo">
<div class="snippet-demo__parent">
<div class="snippet-demo__child">Centered content</div>
</div>
</div>
<style>
.snippet-demo__parent {
border: 1px solid #333;
height: 250px;
position: relative;
width: 250px;
}
.snippet-demo__child {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
#### Explanation
1. `position: absolute` on the child element allows it to be positioned based on its containing block.
2. `left: 50%` and `top: 50%` offsets the child 50% from the left and top edge of its containing block.
3. `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: Fixed height and width on parent element is for the demo only.
#### Browser support
<span class="snippet__support-note">⚠️ Requires prefix for full support.</span>
* https://caniuse.com/#search=transform
<!-- tags: layout -->