|
| 1 | +--- |
| 2 | + |
| 3 | +path: '/css-spacing' |
| 4 | +date: "2020-04-28" |
| 5 | +title: 'CSS and Spacing 📏' |
| 6 | +tags: ['css', 'design', 'UI'] |
| 7 | +excerpt: 'From the article: Spacing in CSS' |
| 8 | +link: 'https://ishadeed.com/article/spacing-in-css/' |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | + |
| 13 | +Spacing can be divided into two groups: |
| 14 | +- inner spacing (`padding`) |
| 15 | +- outer spacing (`margin`) |
| 16 | + |
| 17 | +## What's a margin collapse? |
| 18 | +Margin collapse happens when two vertical elements have a margin, and one of them has a greater margin than the other. In that case, the greater margin will be used, and the other will be ignored. |
| 19 | + |
| 20 | +🔨 To fix: Use a single direction margin, e.g.: |
| 21 | +```css |
| 22 | +.element:not(:last-child) { |
| 23 | + margin-bottom: 1rem; |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +🤔 If both the parent and the child have a margin, the child's margin will be collapsed. |
| 28 | + |
| 29 | +🔨To fix: |
| 30 | +- Add a `border` to the parent element OR |
| 31 | +- Change the child element `display` to `inline-block` OR |
| 32 | +- Add `padding-top` to the parent element. |
| 33 | + |
| 34 | + |
| 35 | +## Padding is not working |
| 36 | +Vertical padding doesn’t work with elements that have display: inline, like `<span>` or `<a>`. |
| 37 | + |
| 38 | +🔨 To fix: need to do `display: inline-block;` |
| 39 | + |
| 40 | +## RTL Styling |
| 41 | + |
| 42 | +Learn about [Right to Left styling](https://rtlstyling.com/posts/rtl-styling#introduction-to-rtl-styling). |
| 43 | + |
| 44 | +## Gaps in Flexbox layout |
| 45 | + |
| 46 | +Flexbox doesn't have a gap property, like `grid-gap`. |
| 47 | + |
| 48 | +🔨 to fix: |
| 49 | +- Add `padding-left` to the grid item AND |
| 50 | +- Add a negative `margin-left` with the same padding-left value to the grid parent. |
| 51 | + |
| 52 | +The reason: because the first card has padding-left while in reality it’s not needed. So it will push the wrapper to the left and cancel that unneeded space ([Demo](https://codepen.io/shadeed/pen/b4abf0f83804991925de43367562d93f?editors=1100)). |
| 53 | + |
| 54 | +## Margin bottom |
| 55 | + |
| 56 | +The last element should not have a bottom margin, as margin should only be between elements. |
| 57 | + |
| 58 | +🔨 To fix: cancel the unneeded spacing by adding a negative margin to the parent element. |
| 59 | + |
| 60 | +## `vmin` |
| 61 | + |
| 62 | +Viewport Minimum (vmin) – A percentage of the viewport width or height, whichever is smaller. 10vmin will resolve to 10% of the current viewport width in portrait orientations, and 10% of the viewport height on landscape orientations. |
| 63 | + |
| 64 | +## `vmax` |
| 65 | + |
| 66 | +Viewport Maximum (vmax) – A percentage of the viewport width or height, whichever is larger. 10vmax will resolve to 10% of the current viewport height in portrait orientations, and 10% of the viewport width on landscape orientations |
| 67 | + |
| 68 | +- `grid-gap: min(2vmax, 32px);` |
| 69 | + |
| 70 | +Use a gap equal to 2vmax, but it shouldn’t go above 32px. |
0 commit comments