![HubSpot CMS HubL Macros [code snippets]](https://i0.wp.com/bootstrapcreative.com/wp-bc/wp-content/uploads/2020/08/code-snippet.png?fit=1024%2C1024&ssl=1)
A collection of HubSpot CMS Macros
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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 | {% set macros = true %} {######################################### ## HubSpot Helper Macros ## #########################################} {# **Prefix Constructor** Utility macro that builds builds vendor prefixed versions of a statement. Usage Inside of you macro, call the prefix macro and pass it the following values in this order, as strings. CSS property to be prefixed Value: Any valid CSS value Prefixes: ms, webkit, moz, o (comma separated) Height: Optional, if not specified, width value will be used and triangle will have equal sides. Example {% macro exampleMacro %} {{ prefix('border-radius', '2px', "webkit") }} {% endmacro %} #} {% macro prefix(property, value, prefixes) %} {% set prefixList = prefixes|split(",", 10) %} {% for prefix in prefixList %} -{{ prefix }}-{{ property }}: {{ value }}; {% endfor %} {{ property }}: {{ value }}; {% endmacro %} {# **Rem Conversion Macro** Safely and simply use rem values for all sizing and spacing. Usage Inside CSS block for an element, call macro with these parameters in this order: Property: Any valid CSS property, as a string Value: A pixel value or values without unit suffix (15 not 15px) Note Values assume CSS convention. You can use from 1 to 4 values seperated by spaces. If you use more than 4, nothing will be returned. If you use more values than a property can accept, your CSS rule will be invalid. Example .my-selector{ {{ remConversion('font-size', '20') }} {{ remConversion('letter-spacing', '.2') }} {{ remConversion('padding', '10 25 5 50') }} } #} {% macro rem(property, value) %} {%if !baselineFontSize %}{% set baselineFontSize = 16 %}{% endif %} {% if (property|length > 0) and (value|length == 0) %} {% set values = property|split(' ') %} {% for v in values %} {% if v|float == 0 %} {% set remValue = v %} {% else %} {% set remValue = v/baselineFontSize %} {% endif %} {% set remArray = remArray ~ remValue ~"," %} {% if loop.last %} {% set remArray = remArray|split(',', 9) %} {% for x in remArray %} {{ x }}{% if x|float != 0 %}rem{% endif %}{% endfor %} {% endif %} {% endfor %} {% else %} {% set values = value|split(' ') %} {% for v in values %} {% if v|float == 0 %} {% set remValue = v %} {% else %} {% set remValue = v/baselineFontSize %} {% endif %} {% set pxArray = pxArray ~ v ~"," %} {% set remArray = remArray ~ remValue ~"," %} {% if loop.last %} {% set pxArray = pxArray|split(',', 9) %} {% set remArray = remArray|split(',', 9) %} {{ property }}:{% for x in pxArray %} {{ x }}{% if x|float != 0 %}px{% endif %}{% endfor %}; {{ property }}:{% for x in remArray %} {{ x }}{% if x|float != 0 %}rem{% endif %}{% endfor %}; {% endif %} {% endfor %} {% endif %} {% endmacro %} {# **Em Conversion Macro** Simple converter for switching pixel values to ems Usage On the assignment side of any appropriate CSS property, call the macro and pass it these parameters in this order: Value: A pixel value or values without unit suffix (15 not 15px) Context: Optional. Defaults to 16. Override if using on nested elements. See notes. Notes We recommend using Rems as the default measurement unit. See Rem Conversion macro for more details. If you use this macro on many nested elements, we strongly recommend a comment and/or indenting convention to keep track of context. Values assume CSS convention. You can use from 1 to 4 values seperated by spaces. If you use more than 4, nothing will be returned. If you use more values than a property can accept, your CSS rule will be invalid. Examples #em-example{ font-size: {{ emConversion('25') }}; } #em-example2{ padding: {{ emConversion('25 10', '18') }}; } #} {% macro em(value, context) %} {%if !context %}{% set context = 16 %}{% endif %} {% set values = value|split(' ') %} {% for v in values %} {% set emValue = v/context %} {% set emArray = emArray ~ emValue ~"," %} {% if loop.last %} {% set emArray = emArray|split(',', 9) %} {% for x in emArray %} {{ x }}em{% endfor %}; {% endif %} {% endfor %} {% endmacro %} {# **Box Shadow** Add drop shadows to your HTML elements. Usage Inside CSS block for element that should have a drop shadow, call macro with standard box-shadow parameters. h-shadow v-shadow blur spread color Example {{ boxShadow('2px') }} #} {% macro boxShadow(shadow) %} {{ prefix('box-shadow', shadow, "webkit") }} {% endmacro %} {# ** Vertical and Horizontal Centering Macro ** Uses the Ghost Vertical Alignment technique to center elements in their container Usage Call macro anywhere in CSS with these parameters in this order: Container: CSS selector for parent of element you'd like to center Element: CSS selector for element you'd like to center Note See: https://css-tricks.com/centering-in-the-unknown/ for original technique #} {% macro verticalAndHorizontalCenter(container, element) %} {{ container }}{ text-align: center; } {{ container }}:before{ content: ''; display: inline-block; height: 100%; vertical-align: middle; margin-right: -0.25em; } {{ element }}{ display: inline-block; vertical-align: middle; } {% endmacro %} {# **Position Macro** Use shorthand for positioning. Usage Inside CSS block for element you'd like to position, call macro with these parameters in this order, all strings. Position: Valid CSS position (absolute, relative, fixed, static) Top, Right, Bottom, Left (standard order for shorthand CSS properties) Note Use 'null' for any value you would like to exclude. The position property is required. Example {{ position('absolute', null, '0', '0', null) }}; #} {% macro position(position, top, right, bottom, left) %} position: {{ position }}; {% if top %} top: {{ top }}; {% endif %} {% if right %} right: {{ right }}; {% endif %} {% if bottom %} bottom: {{ bottom }}; {% endif %} {% if left %} left: {{ left }}; {% endif %} {% endmacro %} {# **Clearfix Macro** Short, easy, clearfix. Usage Call macro anywhere in CSS with selector(s) you'd like to clear. Notes Using this macro once and adding all selectors to it will reduce the size of your stylesheets and is therefore recommended if possible This macro uses the :after pseudo class. Using it for other purposes on elements using this macro will likely break the clear. Example {{ clearfix('.my-div') }}; or {{ clearfix('.my-div, .other-div, .more-divs') }}; #} {% macro clearfix(selector) %} {{ selector }}:after { content: ""; display: table; clear: both; } {% endmacro %} {# **Arrow Macro** Draw an arrow with CSS. Direction, color and size are all variable. Usage Inside CSS block for element that should be an arrow, call macro with these parameters in this order, all strings. Direction: top, right, bottom, left Color: Any valid CSS value Width: Any valid CSS measurement. Height: Optional, if not specified, width value will be used and triangle will have equal sides. Note This technique is commonly used on :before or :after pseudo elements. If using this way, be sure to add the content property in your CSS rule. It is not included in this macro. Example {{ arrow('top','#333','2em', '5em') }} #} {% macro arrow(direction, color, width, height) %} display: block; height: 0; width: 0; {%if !height %}{%set height = width%}{%endif%} {% if direction == 'top' %} border-left: {{ width }} solid transparent; border-right: {{ width }} solid transparent; border-bottom: {{ height }} solid {{color}}; {% elif direction == 'right' %} border-top: {{ width }} solid transparent; border-bottom: {{ width }} solid transparent; border-left: {{ height }} solid {{color}}; {% elif direction == 'bottom' %} border-top: {{ height }} solid {{color}}; border-right: {{ width }} solid transparent; border-left: {{ width }} solid transparent; {% elif direction == 'left' %} border-top: {{ width }} solid transparent; border-right: {{ height }} solid {{color}}; border-bottom: {{ width }} solid transparent; {% endif %} {% endmacro %} {# **Linear Gradient Macro** Use standards gradient notation and get webkit fallback as well Usage Inside CSS block for an element, call macro with these parameters in this order: Direction: Gradient direction. Use standard syntax ('to top', '190deg', etc). Macro will convert for webkit prefixed rule Stops: Any standard stops. They will be passed along directly, refer to documentation for correct syntax Examples #gradient { {{ linearGradient('to top', 'blue, green') }} } #gradient { {{ linearGradient('190deg', 'rgba(200, 200, 190, 1) 20%, rgba(200, 200, 190, 0) 80% ') }} } #} {% macro linearGradient(direction, stops) %} {% set directions = {'to top' : 'bottom', 'to top right': 'bottom left', 'to right top' : 'left bottom', 'to right' : 'left', 'to bottom right' : 'top left', 'to right bottom' : 'left top', 'to bottom' : 'top', 'to bottom left' : 'top right', 'to left bottom' : 'right top', 'to left' : 'right', 'to left top' : 'right bottom', 'to top left' : 'bottom right'} %} {% if direction is defined %} {% set deg = direction|cut('deg') %} {% if deg|int or deg == '0' %} {% set ld = 90 - deg ~ 'deg, ' %} {% else %} {% set ld = directions[direction] ~ ', ' %} {% endif %} {% set d = direction ~ ', ' %} {% endif %} background: -webkit-linear-gradient({{ld}}{{stops}}); background: linear-gradient({{d}}{{stops}}); {% endmacro %} {# **Columns** Add columns to your text. Usage Inside CSS block for element that should be split into columns, call macro with standard values as a string. none|transform-functions|initial|inherit Note Macros for specific properties are also included. Example {{ columns('100px 3') }} {{ columnWidth('100px') }} #} {% macro columns(value) %} {{ prefix('columns', value, "webkit, moz") }} {% endmacro %} {% macro columnWidth(value) %} {{ prefix('column-width', value, "webkit, moz") }} {% endmacro %} {% macro columnGap(value) %} {{ prefix('column-gap', value, "webkit, moz") }} {% endmacro %} {% macro columnRule(value) %} {{ prefix('column-rule', value, "webkit, moz") }} {% endmacro %} {% macro columnRuleColor(value) %} {{ prefix('column-rule-color', value, "webkit, moz") }} {% endmacro %} {% macro columnRuleWidth(value) %} {{ prefix('column-rule-width', value, "webkit, moz") }} {% endmacro %} {% macro columnCount(value) %} {{ prefix('column-count', value, "webkit, moz") }} {% endmacro %} {% macro columnRuleStyle(value) %} {{ prefix('column-rule-style', value, "webkit, moz") }} {% endmacro %} {% macro columnSpan(value) %} {{ prefix('column-span', value, "webkit, moz") }} {% endmacro %} {% macro columnFill(value) %} {{ prefix('column-fill', value, "webkit, moz") }} {% endmacro %} {# **Transforms** Add transforms to your HTML elements. Usage Inside CSS block for element that should be trasnformed, call macro with standard values as a string. none|transform-functions|initial|inherit Note Macros for specific transform properties are also included. Example {{ transform('rotate(7deg)') }} {{ transformStyle('preserve-3d') }} #} {% macro transform(value) %} {{ prefix('transform', value, "webkit, moz, o, ms") }} {% endmacro %} {% macro transformOrigin(axes) %} {{ prefix('transform-origin', axes, "webkit, moz, o, ms") }} {% endmacro %} {% macro transformStyle(value) %} {{ prefix('transform-style', value, "webkit, moz, o, ms") }} {% endmacro %} {% macro perspective(value) %} {{ prefix('perspective', value, "webkit, moz") }} {% endmacro %} {% macro perspectiveOrigin(value) %} {{ prefix('perspective', value, "webkit, moz") }} {% endmacro %} {% macro backfaceVisibility(value) %} {{ prefix('backface-visibility', value, "webkit") }} {% endmacro %} {# **Transitions** Add transitions to your HTML elements. Usage Inside CSS block for element that should be transitioned, call macro with standard values as a string. property duration timing-function delay|initial|inherit Note Macros for specific transition properties are also included. Example {{ transition('width .35s ease-in-out') }} {{ transitionDuration('.35s') }} #} {% macro transition(properties) %} {% set prefixes = ["webkit", "moz"] %} {% for prefix in prefixes %} {{ "-" ~ prefix ~ "-transition: " ~ properties|replace('transform', '-' ~ prefix ~ '-transform') }}; {% endfor %} {{ "transition: " ~ properties }}; {% endmacro %} {% macro transitionProperty(value) %} {% set prefixes = ["webkit", "moz"] %} {% for prefix in prefixes %} {{ "-" ~ prefix ~ "-transition-property: -" ~ prefix ~ "-" ~ value }}; {% endfor %} {{ "transition-property: " ~ value }}; {% endmacro %} {% macro transitionDuration(value) %} {{ prefix('transition-duration', value, "webkit, moz") }} {% endmacro %} {% macro transitionDelay(value) %} {{ prefix('transition-delay', value, "webkit, moz") }} {% endmacro %} {% macro transitionTimingFunction(value) %} {{ prefix('transition-timing-function', value, "webkit, moz") }} {% endmacro %} {# **Animations** Animate your HTML elements. Usage Inside CSS block for element that should be animated, call macro with standard values as a string. name duration timing-function delay iteration-count direction fill-mode play-state Note Macros for specific animation properties are also included. Example {{ animation('mymove 5s infinite') }} {{ animationDuration('2s') }} #} {% macro animation(animations) %} {{ prefix('animation', animations, "webkit") }} {% endmacro %} {% macro animationName(names) %} {{ prefix('animation-name', names, "webkit") }} {% endmacro %} {% macro animationDuration(times) %} {{ prefix('animation-duration', times, "webkit") }} {% endmacro %} {% macro animationTimingFunction(motions) %} {{ prefix('animation-timing-function', motions, "webkit") }} {% endmacro %} {% macro animationIterationCount(values) %} {{ prefix('animation-iteration-count', values, "webkit") }} {% endmacro %} {% macro animationDirection(directions) %} {{ prefix('animation-direction', directions, "webkit") }} {% endmacro %} {% macro animationPlayStates(states) %} {{ prefix('animation-play-states', states, "webkit") }} {% endmacro %} {% macro animationDelay(times) %} {{ prefix('animation-delay', times, "webkit") }} {% endmacro %} {% macro animation-fill-mode(modes) %} {{ prefix('animation-fill-mode', modes, "webkit") }} {% endmacro %} {# **Border Radius** Add rounded corners to your HTML elements. Usage Inside CSS block for element that should be an arrow, call macro with value and unit. Note Macros for specific corners are also included. Example {{ borderRadius('2px') }} #} {% macro borderRadius(radius) %} {{ prefix('border-radius', radius, "webkit") }} {% endmacro %} {% macro borderTopLeftRadius(radius) %} {{ prefix('border-bottom-left-radius', radius, "webkit") }} {% endmacro %} {% macro borderTopRightRadius(radius) %} {{ prefix('border-top-right-radius', radius, "webkit") }} {% endmacro %} {% macro borderBottomRightRadius(radius) %} {{ prefix('border-bottom-right-radius', radius, "webkit") }} {% endmacro %} {% macro borderBottomLeftRadius(radius) %} {{ prefix('border-bottom-left-radius', radius, "webkit") }} {% endmacro %} {# **Miscellaneous Prefixes** Below are some common and simple CSS properies. Usage Inside CSS block for element that the propert should affect, call macro with standard values as a string. #} {# User Select #} {% macro userSelect(value) %} {{ prefix('user-select', value, "webkit, moz, ms") }} {% endmacro %} {# Calc #} {% macro calc(property, value) %} {{ property }}: -webkit-calc({{ value }}); {{ property }}: calc({{ value }}); {% endmacro %} {# Hyphenation #} {% macro hyphens(value) %} {{ prefix('hyphens', value, "webkit, moz, ms") }} {% endmacro %} {# Image Rendering #} {% macro imageRendering(mode) %} {% if mode == "crisp-edges" %} -ms-interpolation-mode: nearest-neighbor; // IE8+ image-rendering: -moz-crisp-edges; image-rendering: -o-crisp-edges; image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; {% else %} image-rendering: {{ mode }}; {% endif %} {% endmacro %} {# Appearance #} {% macro appearance(value) %} {{ prefix('appearance', value, "webkit, moz, o") }} {% endmacro %} {# Box Sizing #} {% macro boxSizing(value) %} {{ prefix('box-sizing', value, "webkit, moz") }} {% endmacro %} {# Filter #} {% macro filter(value) %} {{ prefix('filter', value, "webkit") }} {% endmacro %} {# Placeholder #} {% macro placeholder() %} {% set prefixes = ['::-webkit-input','::-moz',':-moz',':-ms-input'] %} {% for each in prefixes %} {{ each }}-placeholder { {{ caller() }} } {% endfor %} {% endmacro %} {# Selection, include selector if references a css selector #] {% macro selection(selector) %} {{ selector }}::-moz-selection { {{ caller() }} } {{ selector }}::selection { {{ caller() }} } {% endmacro %} |