├── .gitignore ├── src ├── debug.css ├── index.html └── styles.css ├── package.json ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .cache 2 | node_modules 3 | dist 4 | yarn-error.log -------------------------------------------------------------------------------- /src/debug.css: -------------------------------------------------------------------------------- 1 | /* Add this class see an outline (and description) of HTML elements */ 2 | 3 | .debug * { 4 | outline: 1px solid rgba(0, 0, 255, 0.25); 5 | position: relative; 6 | } 7 | 8 | .debug *::before { 9 | content: attr(class); 10 | position: absolute; 11 | top: 0; 12 | left: 0; 13 | margin-top: -10px; 14 | font-size: 10px; 15 | color: rgba(0, 0, 255, 0.25); 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "css-hstack-vstack", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "parcel src/index.html", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "Jon Quach (https://jonquach.com)", 11 | "license": "MIT", 12 | "dependencies": { 13 | "parcel-bundler": "1.12.4" 14 | } 15 | } -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | HStack / VStack CSS 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |
🌭
13 |
🍕
14 |
15 |
🍟
16 |
17 | 18 |
19 |
20 |
21 | 22 |
23 |
🌭
24 |
🍕
25 |
🍟
26 |
27 | 28 |
29 |
30 |
31 | 32 |
33 |
🌭
34 |
🍕
35 |
🍟
36 |
37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🗄 HStack and VStack in CSS 2 | 3 | > CSS layout components that (basically) horizontally and vertically stack anything. 4 | 5 | ## Usage 6 | 7 | ✌️ Check out the **[code here](https://github.com/ItsJonQ/hstack-vstack-css/blob/master/src/styles.css)**. 8 | 9 | ### Horizontally Stacking 10 | 11 | The `.spacer` is used in the (below) example to push the items apart. 12 | 13 | ```html 14 |
15 |
🌭
16 |
🍕
17 |
18 |
🍟
19 |
20 | ``` 21 | 22 | ### Vertically Stacking 23 | 24 | ```html 25 |
26 |
🌭
27 |
🍕
28 |
🍟
29 |
30 | ``` 31 | 32 | ### Swapping from VStack to HStack (responsively) 33 | 34 | The example below stacks items vertically on mobile. However, the items stack horizontally at the medium breakpoint for tablets. 35 | 36 | ```html 37 |
38 |
🌭
39 |
🍕
40 |
🍟
41 |
42 | ``` 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Jon Quach 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | /* DESIGN TOKENS */ 2 | :root { 3 | --grid: 4px; 4 | } 5 | 6 | /* COMPONENTS */ 7 | /* The primary layout component that powers HStack and VStack */ 8 | .flex { 9 | --spacing: 4px; 10 | --direction: row; 11 | --align: center; 12 | display: flex; 13 | justify-content: space-between; 14 | align-items: var(--align); 15 | flex-direction: var(--direction); 16 | } 17 | 18 | /* Adjust the spacing of all (immediate) inner children */ 19 | .flex > * + * { 20 | margin-left: var(--spacing); 21 | } 22 | 23 | /* Horizontally stack items in line. */ 24 | .hstack { 25 | --direction: row; 26 | } 27 | 28 | /* Vertically stack items on top of each other. */ 29 | .vstack { 30 | --align: stretch; 31 | --direction: column; 32 | } 33 | /* Adjust spacing for vertically stacking items */ 34 | .vstack > * + * { 35 | margin-left: unset; 36 | margin-top: var(--spacing); 37 | } 38 | 39 | /* Adaptive take up space within a stack, used for content or for pushing items. */ 40 | .spacer { 41 | flex: 1; 42 | } 43 | 44 | /* MODIFIERS */ 45 | /* Adjusts spacing between items */ 46 | .--sp1 { 47 | --spacing: calc(var(--grid) * 1); 48 | } 49 | .--sp2 { 50 | --spacing: calc(var(--grid) * 2); 51 | } 52 | .--sp3 { 53 | --spacing: calc(var(--grid) * 3); 54 | } 55 | .--sp4 { 56 | --spacing: calc(var(--grid) * 4); 57 | } 58 | .--sp5 { 59 | --spacing: calc(var(--grid) * 5); 60 | } 61 | .--sp6 { 62 | --spacing: calc(var(--grid) * 6); 63 | } 64 | 65 | /* Adjusts alignment of items within a stack. */ 66 | .--top { 67 | --align: flex-start; 68 | } 69 | .--left { 70 | justify-content: flex-start; 71 | } 72 | .--center { 73 | justify-content: center; 74 | } 75 | .--right { 76 | justify-content: flex-end; 77 | } 78 | .--edge { 79 | justify-content: space-between; 80 | } 81 | 82 | /* Vertically center items in a VStack */ 83 | .--v-center { 84 | --align: center; 85 | } 86 | 87 | /* RESPONSIVE */ 88 | /* Responsive styles to swap between HStack and VStack for tablet/desktop */ 89 | @media (min-width: 40em) { 90 | .vstack\@md { 91 | --align: stretch; 92 | --direction: column; 93 | } 94 | .vstack\@md > * + * { 95 | margin-left: unset; 96 | margin-top: var(--spacing); 97 | } 98 | } 99 | @media (min-width: 60em) { 100 | .vstack\@lg { 101 | --align: stretch; 102 | --direction: column; 103 | } 104 | .vstack\@lg > * + * { 105 | margin-left: unset; 106 | margin-top: var(--spacing); 107 | } 108 | } 109 | 110 | @media (min-width: 40em) { 111 | .hstack\@md { 112 | --direction: row; 113 | } 114 | .hstack\@md > * + * { 115 | margin-top: unset; 116 | margin-left: var(--spacing); 117 | } 118 | } 119 | @media (min-width: 60em) { 120 | .hstack\@lg { 121 | --direction: row; 122 | } 123 | .hstack\@lg > * + * { 124 | margin-top: unset; 125 | margin-left: var(--spacing); 126 | } 127 | } 128 | --------------------------------------------------------------------------------