|
1 |
| -# css-protips |
2 |
| -A collection of useful CSS protips |
| 1 | +# CSS Protips |
| 2 | + |
| 3 | +A collection of tips to help take your CSS skills pro. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +### Use `:not()` to Apply/Unapply Borders on Navigation |
| 8 | + |
| 9 | +```css |
| 10 | +/* instead of putting on the border... */ |
| 11 | +.nav-tab { |
| 12 | + border-right: 1px solid #424242; |
| 13 | +} |
| 14 | + |
| 15 | +/* ...and then taking it off... */ |
| 16 | +.nav-tab:last-child { |
| 17 | + border-right: 0; |
| 18 | +} |
| 19 | + |
| 20 | +/* ...use :not() to only apply to the elements you want */ |
| 21 | +.nav-tab:not(:last-child) { |
| 22 | + border-right: 1px solid #424242; |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +It's clean, readable, and easy to understand without the need for hack-y code. |
| 27 | + |
| 28 | + |
| 29 | +### Vertically Center |
| 30 | + |
| 31 | +```css |
| 32 | +html, body { |
| 33 | + margin: 0; |
| 34 | + height: 100%; |
| 35 | +} |
| 36 | + |
| 37 | +body { |
| 38 | + display: -webkit-flex; |
| 39 | + display: -ms-flexbox; |
| 40 | + display: flex; |
| 41 | + -webkit-align-items: center; |
| 42 | + -ms-flex-align: center; |
| 43 | + align-items: center; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +No, it's not dark magic, you really can center elements vertically. Look how simple this is. |
| 48 | + |
| 49 | + |
| 50 | +### Select Items Using Negative `nth-child` |
| 51 | + |
| 52 | +```css |
| 53 | +li { |
| 54 | + display: none; |
| 55 | +} |
| 56 | +/* select items 1 through 3 and show them */ |
| 57 | +li:nth-child(-n+3) { |
| 58 | + display: block; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Use negative `nth-child` in CSS to select items 1 through n. Well that was pretty easy. |
| 63 | + |
| 64 | + |
| 65 | +### Use SVG...for Everything! |
| 66 | + |
| 67 | +```css |
| 68 | +.logo { |
| 69 | + background: url("logo.svg"); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +There's no reason not to use SVG for icons. SVG been supported in all browsers since IE9, so start ditching your .png, .jpg, and .gif-jif-whatev files. Icon fonts, too, but that's a different protip. |
| 74 | + |
| 75 | + |
| 76 | +### Get Rid of Margin Hacks With Flexbox |
| 77 | + |
| 78 | +```css |
| 79 | +.list-of-people { |
| 80 | + display: flex; |
| 81 | + justify-content: space-between; |
| 82 | + .person { |
| 83 | + flex-basis: 23%; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +You can get rid of `nth-`, `first-`, and `last-child` hacks when work with column gutters by using the `space-between` property in flexbox. Another flexbox win! |
| 90 | + |
| 91 | + |
| 92 | +### Support |
| 93 | + |
| 94 | +These protips work in current versions of Chrome, Firefox, Safari, and Edge, and in IE11. |
| 95 | + |
| 96 | + |
| 97 | +Carl became the Cinderalla story by using CSS protips. |
0 commit comments