Advertisement
Guest User

scss example 3

a guest
Jul 18th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 6.30 KB | None | 0 0
  1. addition to project from last time:
  2. ---------------------------------------
  3.  
  4. index.html
  5. -----------
  6.  
  7. <!doctype html>
  8. <html>
  9.         <head>
  10.                 <meta charset="utf-8" />
  11.                 <link rel="stylesheet" type="text/css" href="css/main.css">
  12.  
  13.                 <title>SASS / SCSS</title>
  14.         </head>
  15.  
  16.         <body>
  17.  
  18.                 <input type="button" class="myBtn" value="click me" />
  19.                 <h1> this is the h1</h1>
  20.  
  21.                 <h2></h2>
  22.  
  23.                 <h3>some h3 sort of stuff</h3>
  24.  
  25.                 <input type="button" class="btn-primary" value="primary" />
  26.                 <input type="button" class="btn-secondary" value="secondary" />
  27.  
  28.                 <div class="shadows">my shadowy div </div>
  29.  
  30.                 <p class="main-red"> para red</p>
  31.                 <p class="main-green"> para green</p>
  32.                 <p class="main-blue"> para blue</p>
  33.  
  34.                 <h4 class="big-dimen"></h4>
  35.                 <h4 class="medium-dimen"></h4>
  36.                 <h4 class="small-dimen"></h4>
  37.                 <h4 class="tiny-dimen"></h4>
  38.  
  39.  
  40.                 <h4 class="cls-3"></h4>
  41.  
  42.                 <h4 class="while-cls-3"></h4>
  43.  
  44.                 <h4 class="testCondTrue"> some test text</h4>
  45.         </body>
  46. </html>
  47.  
  48.  
  49. ---------------------
  50.  
  51. main.scss
  52. ---------------------
  53.  
  54.  
  55.  
  56. $btn-bg: #009fff;
  57. $btn-color: #ffffff;
  58.  
  59. .myBtn{
  60.   background: $btn-bg;
  61.   color: $btn-color;
  62.  
  63.   &:hover{
  64.     background: $btn-bg * 1.2; // larger than 1 -> lighter, smaller than 1 -> darker
  65.     color: darken($btn-color, 15%); // there is lighten as well
  66.   }
  67. }
  68.  
  69. h1{
  70.   background: #00ff00 + #00f999;
  71.   color: rgba(32, 56, 250, 0.5) + rgba(16, 39, 240, 0.5); // the alpha must be the same
  72. }
  73.  
  74.  
  75. @mixin font-check($font){
  76.     &:after{
  77.  
  78.         @if(type-of($font)==string){
  79.             content: 'the font is #{$font}'; // #{} - string interpolation
  80.             font-family: $font;
  81.         }
  82.         @else{
  83.           content: 'font type error';
  84.         }
  85.  
  86.     }
  87. }
  88.  
  89.  
  90. h2{
  91.   //@include font-check(sans-serif);
  92.   @include font-check(30px);
  93. }
  94.  
  95. $my-padding: 30px;
  96. h3{
  97.   @if($my-padding < 20px){
  98.     padding: $my-padding;
  99.     color: green;
  100.   }
  101.   @else if($my-padding < 40px){
  102.     padding: $my-padding / 2;
  103.     color: blue;
  104.   }
  105.   @else{
  106.     padding: ($my-padding - 20) / 2;
  107.     color: red;
  108.   }
  109. }
  110.  
  111. .btn{
  112.   &-primary{
  113.     background: blue;
  114.     color: lighten(blue, 50%);
  115.   }
  116.   &-secondary{
  117.     background: green;
  118.     color: lighten(green, 50%);
  119.   }
  120. }
  121.  
  122.  
  123. @mixin box-shadow($shadows...){ // ... is varargs - can pass more than one argument
  124.   -moz-box-shadow: $shadows;
  125.   -webkit-box-shadow: $shadows;
  126.   -ms-box-shadow: $shadows;
  127.   box-shadow: $shadows;
  128. }
  129.  
  130. .shadows{
  131.   @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
  132. }
  133.  
  134.  
  135. // lists
  136. $margin-list: 0px 5px 10px 15px; // can seperate using commas as well
  137.  
  138. // for each
  139. @each $curr-margin in $margin-list { // for each element in $margin-list
  140.   $i: index($margin-list, $curr-margin); // get the index of the current margin in the list
  141.  
  142.   .margin-#{$i} { // make a new class (whose name is margin-index)
  143.     margin: $curr-margin; // set the margin of the class to the current margin
  144.   }
  145. }
  146.  
  147.  
  148. // dictionary
  149. $primary-colors: (
  150.   'red': #ff0000,
  151.   'green': #00ff00,
  152.   'blue': #0000ff
  153. );
  154.  
  155. @each $color-name, $color-code in $primary-colors{
  156.   .main-#{$color-name}{
  157.     color: $color-code;
  158.   }
  159. }
  160.  
  161. // each over a list/dictionary literal
  162. @each $sizeName, $sizeAmount in (big:100, medium:75, small:50, tiny:25){
  163.   .#{$sizeName}-dimen{
  164.       width: $sizeAmount + px;
  165.       height: $sizeAmount + px;
  166.       background: red;
  167.       &:after{
  168.         content: "#{$sizeName}";
  169.       }
  170.     }
  171. }
  172.  
  173.  
  174. // for loop
  175. @for $i from 1 through 3{
  176.   .cls-#{$i}{
  177.       &:after{
  178.         content: "#{$i}";
  179.       }
  180.     }
  181. }
  182.  
  183. //while loop
  184. $counter: 1;
  185. @while $counter < 4 {
  186.   .while-cls-#{$counter}{
  187.       &:after{
  188.         content: "the counter is #{$counter}";
  189.       }
  190.     }
  191.     $counter: $counter + 1;
  192. }
  193.  
  194.  
  195. // single line condition
  196. @mixin testCondition($condition){
  197.   $color: if($condition, blue, red); // if $condition is true then $color is blue, else it is red
  198.   color: $color;
  199. }
  200.  
  201. .testCondTrue{
  202.   @include testCondition(true);
  203. }
  204.  
  205. .testCondFalse{
  206.   @include testCondition(false);
  207. }
  208.  
  209.  
  210. ========================================================
  211. ========================================================
  212. ========================================================
  213.  
  214. new project from this lesson:
  215. -------------------------------
  216.  
  217. index.html
  218. ---------------
  219. <!doctype html>
  220. <html>
  221.         <head>
  222.                 <meta charset="utf-8" />
  223.                 <link rel="stylesheet" type="text/css" href="css/main.css">
  224.  
  225.                 <title>SASS / SCSS</title>
  226.         </head>
  227.  
  228.         <body>
  229.  
  230.                 <div class="profile-pic"></div>
  231.                 <p class="profile-info"></p>
  232.  
  233.                 <hr/>
  234.  
  235.                 <div class="myClass"></div>
  236.  
  237.                 <div class="myContainer">
  238.                         <div class="inside"></div>
  239.                 </div>
  240.  
  241.         </body>
  242. </html>
  243.  
  244.  
  245. --------------------
  246.  
  247. main.scss
  248. --------------------
  249. @import "profile"; // getting the infomation in the partial
  250.  
  251. // note: since we imported "profile" in this file, we can use everything defined in that file
  252.  
  253. @mixin visible-div($width, $height, $color){
  254.   width: $width;
  255.   height: $height;
  256.   background-color: $color;
  257. }
  258.  
  259. .myClass{
  260.   @include visible-div(30px, 50px, orange);
  261.   @include border-radius(10px); // using the mixin defined in the "profile" file
  262. }
  263.  
  264. // functions
  265. @function get-percentage($target, $container){
  266.   @return ($target / $container) * 100%;
  267. }
  268.  
  269. .myContainer{
  270.   padding: get-percentage(10px, 100px);
  271.   @include visible-div(20px, 20px, cyan);
  272.  
  273. }
  274.  
  275. .inside{
  276.   @include visible-div(20px, 20px, red);
  277. }
  278.  
  279.  
  280. ---------------------------
  281.  
  282. _profile.scss
  283. ---------------------------
  284.  
  285. // this is a 'partial'. it starts with an _
  286. // a .css file will NOT be created for this file.
  287.  
  288.  
  289. $bg-color: lightblue;
  290.  
  291. @mixin border-radius($rad){
  292.     border-radius: $rad;
  293.     -webkit-border-radius: $rad;
  294.     -moz-border-radius: $rad;
  295.     -ms-border-radius: $rad;
  296. }
  297.  
  298.  
  299. .profile{
  300.   &-pic{
  301.       width: 500px;
  302.       height: 500px;
  303.       background-color: $bg-color;
  304.       @include  border-radius(9999px);
  305.       border:{
  306.         width: thick;
  307.         color: $bg-color * 0.9;
  308.         style: solid;
  309.       }
  310.       margin: auto;
  311.  
  312.   }
  313.  
  314.   &-info{
  315.     &:after{
  316.       content: 'some amazing info about my profile';
  317.     }
  318.     font:{
  319.       weight: bold;
  320.       family: 'David';
  321.       size: 30px;
  322.     }
  323.     text:{
  324.       decoration: underline;
  325.       align: center;
  326.     }
  327.  
  328.   }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement