Spaces:
Sleeping
Sleeping
File size: 18,763 Bytes
d467ea7 | 1 2 3 4 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | {
"cells": [
{
"cell_type": "markdown",
"id": "ce15239f",
"metadata": {},
"source": [
"# Padding & Margin & Spacing, Oh my! (MonsterUI Spacing Guide)\n",
"\n",
"This guide will cover some essentials about how to properly space apps and what the differences are between:\n",
"\n",
"- Padding\n",
"- Margin\n",
"- Spacing\n",
"- Gap\n",
"\n",
"Manipulating the space between components can make a huge difference to the percieved quality of the page. Being able to tweak the spacing can have a big impact!\n",
"\n",
"> Tip: I find it works best to get everything on the page without adjusting spacing much, and adjusting spacing at the end. \n",
"\n",
"## Abreviations:\n",
"\n",
"First a few abbreviations that are helpful to know with tailwind (and a convention we follow in `MonsterUI`).\n",
"\n",
"- `t`, `b`, `l`, `r` = top, bottom, left, right\n",
"- `p`, `m` = padding, margin\n",
"- `x`, `y` = horizontal, vertical\n",
"\n",
"That means:\n",
"\n",
"- `mt` means margin on top of the element\n",
"- `px` means padding on the x axis (both left and right)\n",
"- `space-y` means apply spacing on the y axis (both top and bottom)\n",
"\n",
"## Padding vs Margin\n",
"\n",
"Margin applies space to the left of the component, and padding applies space on the left inside of the component. \n",
"\n",
"Please reference the example with cards below:\n",
"\n",
"- `ml-20` applies space to the left of the card (outside the card)\n",
"- `pl-20` applies space on the left inside of the card (inside the card)\n",
"\n",
"This means that if you want to move the whole thing but keep the actual container unchanged, use margin. If you want to change the container by adding space inside of it, use padding."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "aef6844a",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<div class=\"grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 gap-4\">\n",
" <div class=\"uk-card ml-20\">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \" style=\"background-color: red\">A Simple Card with ml-20</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card pl-20\">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \" style=\"background-color: red\">A Simple Card with pl-20</h4>\n",
" </div>\n",
" </div>\n",
"</div>\n",
"\n",
"```"
],
"text/plain": [
"div((div((div((h4(('A Simple Card with ml-20',),{'class': 'uk-h4 ', 'style': 'background-color: red'}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card ml-20'}), div((div((h4(('A Simple Card with pl-20',),{'class': 'uk-h4 ', 'style': 'background-color: red'}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card pl-20'})),{'class': 'grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 gap-4'})"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Grid(\n",
" Card(H4(\"A Simple Card with ml-20\",style='background-color: red'), \n",
" cls='ml-20'),\n",
" Card(H4(\"A Simple Card with pl-20\", style='background-color: red'),\n",
" cls='pl-20'))"
]
},
{
"cell_type": "markdown",
"id": "59ed6d0d",
"metadata": {},
"source": [
"## Space vs gap\n",
"\n",
"Spacing and gap are both about setting the space between components.\n",
"\n",
"+ Spacing applies margin to every element except for the first element in a group. \n",
"+ Gap creates a gap between every element in flexbox elements and grids.\n",
"\n",
">Rule of thumb: Use Gap when using grids.\n",
"\n",
"Let's take a look at some grid examples."
]
},
{
"cell_type": "markdown",
"id": "22ad2226",
"metadata": {},
"source": [
"#### Grid"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a296d044",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<div class=\"grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 \">\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
"</div>\n",
"\n",
"```"
],
"text/plain": [
"div((div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '})),{'class': 'grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 '})"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Grid(\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" cls='')"
]
},
{
"cell_type": "markdown",
"id": "286c245f",
"metadata": {},
"source": [
"#### Grid with gap"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ed24f5d3",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<div class=\"grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 gap-4\">\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
"</div>\n",
"\n",
"```"
],
"text/plain": [
"div((div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '})),{'class': 'grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 gap-4'})"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Grid(\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" cls='gap-4')"
]
},
{
"cell_type": "markdown",
"id": "7bb43e6d",
"metadata": {},
"source": [
"#### Grid with space"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "44130942",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<div class=\"grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 space-x-4 space-y-4\">\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
" <div class=\"uk-card \">\n",
" <div class=\"uk-card-body space-y-6\">\n",
" <h4 class=\"uk-h4 \">A Simple Card</h4>\n",
" </div>\n",
" </div>\n",
"</div>\n",
"\n",
"```"
],
"text/plain": [
"div((div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '}), div((div((h4(('A Simple Card',),{'class': 'uk-h4 '}),),{'class': 'uk-card-body space-y-6'}),),{'class': 'uk-card '})),{'class': 'grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 space-x-4 space-y-4'})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Grid(\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" Card(H4(\"A Simple Card\")),\n",
" cls='space-x-4 space-y-4')"
]
},
{
"cell_type": "markdown",
"id": "50c48f82",
"metadata": {},
"source": [
"### Grid with no gap or space\n",
"\n",
"The first example has no gap or not space applied. As expected this means the cards are flush with each other. Often this is not what you want, because a little space between cards looks much nicer.\n",
"\n",
"### Grid with gap\n",
"\n",
"The second example has the same grid but with gap applied. As youc an see, this gives constent space between all elements of the grid looks great!\n",
"\n",
"### Grid with space\n",
"\n",
"The third example has the same grid but with space applied. As you can see, it's not really what we want. However it's a really good illustration of how space works so let's notice a few things about it:\n",
"\n",
"**X Axis**\n",
"\n",
"- The first card is flush with the left side of the page (no margin)\n",
"- The card below it isn't flush with the left side of the page (has margin)\n",
"\n",
"**Y Axis**\n",
"\n",
"- The first card is flush with the heading immediately above it (no margin)\n",
"- The card top it's right isn't flush with the heading above it (has margin)\n",
"\n",
"So `space` applies margin to every element except for the first element in a group!\n",
"\n",
"> Tip: Use your browser developer tools to inspect the examples\n",
"\n",
"Next, let's look at a form example where `gap` isn't a good choice but `space` works beautifully!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36d1949c",
"metadata": {},
"outputs": [],
"source": [
"Form(DivCentered(H3(\"My Form\")),\n",
" LabelInput(\"Name\"),\n",
" Grid(LabelInput(\"Phone\"), LabelInput(\"Email\"), cols=2),\n",
" cls='')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8c5d280",
"metadata": {},
"outputs": [],
"source": [
"Form(DivCentered(H3(\"My Form with gap\")),\n",
" LabelInput(\"Name\"),\n",
" Grid(LabelInput(\"Phone\"), LabelInput(\"Email\"), cols=2),\n",
" cls='gap-y-5')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b2566530",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<form enctype=\"multipart/form-data\" class=\"space-y-5\"> <div class=\"uk-flex uk-flex-column uk-flex-middle uk-flex-center space-y-4\">\n",
" <h3 class=\"uk-h3 \">My Form with Spacing</h3>\n",
" </div>\n",
" <div class=\"space-y-2\">\n",
"<label class=\"uk-form-label \">Name</label> <input class=\"uk-input \">\n",
" </div>\n",
" <div class=\"grid grid-cols-2 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-2 gap-4\">\n",
" <div class=\"space-y-2\">\n",
"<label class=\"uk-form-label \">Phone</label> <input class=\"uk-input \">\n",
" </div>\n",
" <div class=\"space-y-2\">\n",
"<label class=\"uk-form-label \">Email</label> <input class=\"uk-input \">\n",
" </div>\n",
" </div>\n",
"</form>\n",
"```"
],
"text/plain": [
"form((div((h3(('My Form with Spacing',),{'class': 'uk-h3 '}),),{'class': 'uk-flex uk-flex-column uk-flex-middle uk-flex-center space-y-4'}), div((label(('Name',),{'for': '', 'class': 'uk-form-label '}), input((),{'id': '', 'class': 'uk-input '})),{'class': 'space-y-2'}), div((div((label(('Phone',),{'for': '', 'class': 'uk-form-label '}), input((),{'id': '', 'class': 'uk-input '})),{'class': 'space-y-2'}), div((label(('Email',),{'for': '', 'class': 'uk-form-label '}), input((),{'id': '', 'class': 'uk-input '})),{'class': 'space-y-2'})),{'class': 'grid grid-cols-2 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-2 gap-4'})),{'enctype': 'multipart/form-data', 'class': 'space-y-5'})"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Form(DivCentered(H3(\"My Form with Spacing\")),\n",
" LabelInput(\"Name\"),\n",
" Grid(LabelInput(\"Phone\"), LabelInput(\"Email\"), cols=2),\n",
" cls='space-y-5')"
]
},
{
"cell_type": "markdown",
"id": "85e821ad",
"metadata": {},
"source": [
"### Form with no gap or space\n",
"\n",
"The top form looks a bit scrunched with defaults, but it's certainly passable. There is a bit of a space between the label and it's associated input because of the defaults in MonsterUI.\n",
"\n",
"### Form with gap\n",
"\n",
"The second form with gap is identical to the first. Because we're not in a flex element or a grid, it doesn't do anything at all!\n",
"\n",
"### Form with space\n",
"\n",
"`Space-y-5` adds vertical space between each child which really spreads out the form for a nice aesthetic. If you recall from the grid example, it does not apply this margin to the first element - but in this situation (and many others) we do not want the spacing above the top element (heading) to be the same as the spacing between the form elements.\n",
"\n",
"> Tip: Use your browser developer tools to inspect the examples\n"
]
},
{
"cell_type": "markdown",
"id": "83d0f4ea",
"metadata": {},
"source": [
"# Further reading\n",
"\n",
"For further reading, check out the [Tailwind CSS guide](https://tailwindcss.com/docs/margin#adding-margin-to-a-single-side), which other users have found to be a useful as an additional guide."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|