80
snippets/display-table-centering.md
Normal file
80
snippets/display-table-centering.md
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
### Display Table Centering
|
||||||
|
|
||||||
|
Vertically and horitonally centers a child element within its parent element using `display: table` (as an alternative to `flexbox`).
|
||||||
|
|
||||||
|
#### HTML
|
||||||
|
|
||||||
|
```html
|
||||||
|
<div class="container">
|
||||||
|
<div class="center">
|
||||||
|
<span>Centered content</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### CSS
|
||||||
|
|
||||||
|
```css
|
||||||
|
.container {
|
||||||
|
border: 1px solid #333;
|
||||||
|
height: 250px;
|
||||||
|
width: 250px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.center {
|
||||||
|
display: table;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.center > span {
|
||||||
|
display: table-cell;
|
||||||
|
text-align: center;
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Demo
|
||||||
|
<div class="snippet-demo">
|
||||||
|
<div class="snippet-demo__container">
|
||||||
|
<div class="snippet-demo__center">
|
||||||
|
<span>Centered content</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.snippet-demo__container {
|
||||||
|
border: 1px solid #333;
|
||||||
|
height: 250px;
|
||||||
|
width: 250px;
|
||||||
|
}
|
||||||
|
.snippet-demo__center {
|
||||||
|
display: table;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.snippet-demo__center > span {
|
||||||
|
display: table-cell;
|
||||||
|
text-align: center;
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
#### Explanation
|
||||||
|
|
||||||
|
1. `display: table` on '.center' allows the element to behave like a `<table>` HTML element.
|
||||||
|
2. 100% height and width on '.center' allows the element to fill the available space within its parent element.
|
||||||
|
3. `display: table-cell` on '.center > span' allows the element to behave like an <td> HTML element.
|
||||||
|
2. `text-align: center` on '.center > span' centers the child element horizontally.
|
||||||
|
2. `vertical-align: middle` on '.center > span' centers the child element vertically.
|
||||||
|
|
||||||
|
The outer parent ('.container' in this case) must have a fixed height and width.
|
||||||
|
|
||||||
|
#### Browser support
|
||||||
|
|
||||||
|
<span class="snippet__support-note">✅ No caveats.</span>
|
||||||
|
|
||||||
|
* https://caniuse.com/#search=display%3A%20table
|
||||||
|
|
||||||
|
<!-- tags: layout -->
|
||||||
86
snippets/offscreen.md
Normal file
86
snippets/offscreen.md
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
### Offscreen
|
||||||
|
|
||||||
|
A bulletproof way to completely hide an element visually and positionally in the DOM while still allowing it to be accessed by JavaScript and readable by screen readers. This method is very useful for accessibility ([ADA](https://adata.org/learn-about-ada)) development when more context is needed for visually-impaired users. As an alternative to `display: none` which is not readable by screen readers or `visibility: hidden` which takes up physical space in the DOM.
|
||||||
|
|
||||||
|
#### HTML
|
||||||
|
|
||||||
|
```html
|
||||||
|
<a class="button" href="http://pantswebsite.com">
|
||||||
|
Learn More
|
||||||
|
<span class="offscreen"> about pants</span>
|
||||||
|
</a>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### CSS
|
||||||
|
|
||||||
|
```css
|
||||||
|
.offscreen {
|
||||||
|
border: 0;
|
||||||
|
clip: rect(0 0 0 0);
|
||||||
|
height: 1px;
|
||||||
|
margin: -1px;
|
||||||
|
overflow: hidden;
|
||||||
|
padding: 0;
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Demo
|
||||||
|
<div class="snippet-demo">
|
||||||
|
<a class="button" href="javascript:;">
|
||||||
|
Learn More
|
||||||
|
<span class="offscreen"> about pants</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.snippet-demo__button {
|
||||||
|
-webkit-appearance: none;
|
||||||
|
appearance: none;
|
||||||
|
background-color: #7983ff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 0.25rem;
|
||||||
|
color: #fff;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-block;
|
||||||
|
font-family: sans-serif;
|
||||||
|
font-size: 1rem;
|
||||||
|
padding: 0.8rem 1rem;
|
||||||
|
text-align: center;
|
||||||
|
text-decoration: none;
|
||||||
|
transition: background-color 0.3s;
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
.snippet-demo__button:hover { background-color: #717aef; }
|
||||||
|
.snippet-demo__offscreen {
|
||||||
|
border: 0;
|
||||||
|
clip: rect(0 0 0 0);
|
||||||
|
height: 1px;
|
||||||
|
width: 1px;
|
||||||
|
margin: -1px;
|
||||||
|
overflow: hidden;
|
||||||
|
padding: 0;
|
||||||
|
position: absolute;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
#### Explanation
|
||||||
|
|
||||||
|
1. Remove all borders.
|
||||||
|
2. Use `clip` to indicate that no part of the element should be shown.
|
||||||
|
3. Make the height and width of the element 1px.
|
||||||
|
4. Negate the elements height and width using `margin: -1px`.
|
||||||
|
5. Hide the element's overflow.
|
||||||
|
6. Remove all padding.
|
||||||
|
7. Position the element absolutely so that it does not take up space in the DOM.
|
||||||
|
|
||||||
|
#### Browser support
|
||||||
|
|
||||||
|
<span class="snippet__support-note">✅ No caveats.</span>
|
||||||
|
|
||||||
|
(Although `clip` technically has been depreciated, the newer `clip-path` currently has very limited browser support.)
|
||||||
|
|
||||||
|
* https://caniuse.com/#search=clip
|
||||||
|
|
||||||
|
<!-- tags: layout, visual -->
|
||||||
67
snippets/transform-centering.md
Normal file
67
snippets/transform-centering.md
Normal 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 -->
|
||||||
Reference in New Issue
Block a user