--- title: Creating A Theme Using SASS order: 78 layout: page --- [[creating-a-theme-using-sass]] = Creating a theme using SASS Vaadin 7 comes with built in support for Sass, which can be thought of as a preprocessor for CSS. From the Sass homepage: _Sass makes CSS fun again. Sass is an extension of CSS3, adding nested rules, variables, mixins, selector inheritance, and more._ Sass looks like CSS with some added features, and is compiled into CSS before being sent to the browser. The compilation is either done beforehand, or (during development) on-the-fly by the servlet. In Vaadin 7 you can make use of Sass in any of your CSS, and as usual there are more than one way to arrange this. The recommended way if you do not have a specific reason not to do so, is to compile your theme into one CSS file (that is: without any CSS @include), but we'll start with the getting-your-feet-wet approach that looks exactly as before.It’s worth noting that you can continue to use CSS without Sass just as before, if you prefer. [[getting-your-feet-wet]] Getting your feet wet ^^^^^^^^^^^^^^^^^^^^^ In Vaadin 7 you set the theme in use by specifying the `@Theme` annotation on your UI, e.g `@Theme(“themename”)`. Ignoring Sass for a second, you would then create a `mytheme/styles.css` that typically `@import` the Reindeer theme (in case you forgot, your theme should be located in `WebContent/VAADIN/themes//styles.css`). You can start using Sass with this approach, by renaming your `styles.css` to `styles.scss` and importing `legacy-styles.css` instead of `styles.css` - the resulting CSS will be exactly as the same as before, BUT now you're free to use Sass in your theme: [source,scss] .... @import url(../reindeer/legacy-styles.css); $color : green; .v-button-caption { color: $color; } .... Here we just define a Sass variable to use as color for button captions. *NOTE* that this way (using legacy-styles) you still lose one important new feature: you can't have multiple themes on the same page when using the legacy-styles.css -approach. To gain this feature, which is crucial if you intend to run multiple applications with different themes embedded in the same page (e.g portals), you must use Sass. [[compiling]] Compiling ^^^^^^^^^ Provided you’re in development mode (not production), the scss will automatically be translated into CSS. You can also compile the scss manually (and MUST do so for production). To do this you should run `com.vaadin.sass.SassCompiler` with the Vaadin jars on the classpath and give it your scss file and output file as arguments. If you have the jars readily available, you could do something like this in the command line: [source,bash] .... > java -cp '../../../WEB-INF/lib/*' com.vaadin.sass.SassCompiler styles.scss styles.css .... Another way would be to save the auto-compiled styles.css from the browser. Support has been added to the Eclipse plugin through the _Compile Vaadin Theme_ button . NOTE that if you're using Ivy (the default if you're using the Eclipse plugin), you must make sure to get the appropriate dependencies on your classpath some other way (since they are not present in `WEB-INF/lib`). In Eclipse, use the Run -dialog to inherit the classpath from your project. You'll notice that the resulting theme still uses `@import` to 'extend' the Reindeer theme: [source,scss] .... @import url(../reindeer/legacy-styles.css); .... This approach is an easy way to get started with Sass, but will cause two requests (one for our theme, one for Reindeer). Let’s have a look at the recommended approach next. [[going-deeper]] Going deeper ^^^^^^^^^^^^ Instead of using CSS `@import` to base your application theme on, you can (and probably should) use Sass `@import` to make a monolithic theme (one CSS file, one request when using the application). Just `@import reindeer.scss`, and `@include` it: [source,scss] .... // mytheme.scss @import "../reindeer/reindeer.scss"; .mytheme { @include reindeer; $color : yellow; .v-button-caption { color: $color; } } .... This produces a styles.css that contains all the styles for Reindeer as well as your custom styles (note that this makes your final CSS quite big to scroll trough, so you might not want to do this when just learning the Sass syntax). There is no `@import` in the compiled CSS, so it will not cause additional requests. Additionally, due to the way Vaadin Sass is structured, this opens up for many possibilities to customize, mix-and-match themes, and leave unused stuff out. One important thing to notice, is that we wrapped everything in `.themename {}`, in this case `.mytheme {}`. This is the magic sauce that makes it possible to have multiple themes on one page. _It is crucial that the name matches your themename, or your styles will not be applied._ Some of the nice features you get with Sass include variables, selector nesting, mixins (optionally with parameters), selector inheritance. For more information of what you can do with Sass, you should refer to the official documentation at http://sass-lang.com Please note that the Vaadin Sass compiler only supports the “SCSS”, which is the “new main syntax” (the original Sass also supports another, older syntax).The Vaadin version aims to be completely compatible, though initially there will be some limitations (and actually some added functionality). Please let us know if you find something is not working as expected. [[one-more-thing-recommended-structure]] One more thing: Recommended structure ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In Vaadin 7, all Vaadin core themes are using Sass. The _reindeer/styles.css_ we included first, is the compiled Reindeer theme, including the stuff from the Base theme that Reindeer extends. The Sass for the Reindeer theme is in _reindeer/reindeer.scss_, and contains one big mixin that will include the whole theme, unless you specifically tell it to leave out some parts. The themes are further divided into smaller parts, that can be left out, or separately included and renamed - providing a powerful way to customize and mix-and-match themes. *It is recommended* that you go ahead an divide your own theme into at least two files as well: *styles.scss* and *themename.scss* (where 'themename' is the name of your theme). This is will make your theme extendable, and also has the nice benefit that file you usually edit is uniquely named (themename.scss) instead of a generic styles.scss that you might have many of. For a theme named 'mytheme', this would look as follows: `mytheme/styles.scss:` [source,scss] .... @import "mytheme.scss"; .mytheme { @include mytheme; } .... `mytheme/mytheme.scss`: [source,scss] .... @import "../reindeer/reindeer.scss"; @mixin mytheme { // your styles go here @include reindeer; } .... This is the exact structure Vaadin core themes are using, and the way the Eclipse plugin will set things up for you (not yet in beta 10). Of course, you're still free to arrange your theme in another way if you prefer. Upcoming tutorials will address specific use-cases! n87'>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 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879