SASS Variables and Mixins Written by Margaret Rodgers. Variables. Contents. From Web Team. 1 Variables

Similar documents
What do we mean by layouts?

CSS worksheet. JMC 105 Drake University

What is the Box Model?

Introduction to Multimedia. MMP100 Spring 2016 thiserichagan.com/mmp100

CSS.

CISC1600-SummerII2012-Raphael-lec3 1

Styles, Style Sheets, the Box Model and Liquid Layout

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

Assignments (4) Assessment as per Schedule (2)

Introduction to WEB PROGRAMMING

BIM222 Internet Programming

.hedgehog { background-image: url(backyard.jpg); color: #ffff99; height: 6in; width: 12in; } .tube {

epromo Guidelines DUE DATES NOT ALLOWED PLAIN TEXT VERSION

Introduction to Web Programming and Design

Wanted! Introduction. Step 1: Styling your poster. Activity Checklist. In this project, you ll learn how to make your own poster.

The default style for an unordered (bulleted) list is the bullet, or dot. You can change the style to either a square or a circle as follows:

2. Write style rules for how you d like certain elements to look.

{WebCodeChicks} </spartanburg> Beginning Sass. sass-lang.com/guide

Reading 2.2 Cascading Style Sheets

CSS Cascading Style Sheets

CSS means Cascading Style Sheets. It is used to style HTML documents.

IN4MATX 133: User Interface Software

Welcome Please sit on alternating rows. powered by lucid & no.dots.nl/student

HTML and CSS a further introduction

Fundamentals of Web Technologies. Agenda: CSS Layout (Box Model) CSS Layout: Box Model. All HTML elements can be considered as a box or a container

Adding CSS to your HTML

CS 350 Internet Applications I Name: Exam II (CSS) October 29, 2013

Using Dreamweaver CC. Logo. 4 Creating a Template. Page Heading. Page content in this area. About Us Gallery Ordering Contact Us Links

Which is why we ll now be learning how to write in CSS (or cascading sheet style)

CSS: formatting webpages

CSS مفاهیم ساختار و اصول استفاده و به کارگیری

GIMP WEB 2.0 MENUS WEB 2.0 MENUS: HORIZONTAL NAVIGATION BAR CREATING AN HTML LIST

INFS 2150 Introduction to Web Development

INFS 2150 Introduction to Web Development

Positioning in CSS: There are 5 different ways we can set our position:

COMP519 Web Programming Lecture 6: Cascading Style Sheets: Part 2 Handouts

CS134 Web Site Design & Development. Quiz1

Using Dreamweaver CS6

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

Cascading Style Sheet

CSS Cascading Style Sheets

<body bgcolor=" " fgcolor=" " link=" " vlink=" " alink=" "> These body attributes have now been deprecated, and should not be used in XHTML.

Session 3.1 Objectives Review the history and concepts of CSS Explore inline styles, embedded styles, and external style sheets Understand style

Internet Programming 1 ITG 212 / A

CMPT 165: More CSS Basics

3.1 Introduction. 3.2 Levels of Style Sheets. - The CSS1 specification was developed in There are three levels of style sheets

- The CSS1 specification was developed in CSS2 was released in CSS2.1 reflects browser implementations

BORDER IMAGES THE BACKGROUND IMAGE PROPERTIES. Learning Web Design, 5e

Block & Inline Elements

COMS 359: Interactive Media

Sass. The Future of Stylesheets.

Client-Side Web Technologies. CSS Part I

ADDING CSS TO YOUR HTML DOCUMENT. A FEW CSS VALUES (colour, size and the box model)

CSS. Selectors & Measurments. Copyright DevelopIntelligence LLC

Using CSS for page layout

Using Dreamweaver CS6

Study Guide 2 - HTML and CSS - Chap. 6,8,10,11,12 Name - Alexia Bernardo

Page Layout Using Tables

COMM 2555: Interactive Digital Communication / Spring 2018 Lab 9: Basic layout techniques with CSS

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

Creating Layouts Using CSS. Lesson 9

Pragmatic Guide to Sass 3

CSS: Cascading Style Sheets

Zen Garden. CSS Zen Garden

NAVIGATION INSTRUCTIONS

Let s start with the document tree

HTML & CSS. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Getting Started with Eric Meyer's CSS Sculptor 1.0

Cascading Style Sheet. Styles as Tag Attributes. Syntax. <h1>: what font type/size is used? STYLE = SELECTOR {RULES} Attributes such as bgcolor

HTML + CSS. ScottyLabs WDW. Overview HTML Tags CSS Properties Resources

Lab 1: Introducing HTML5 and CSS3

Create a three column layout using CSS, divs and floating

To link to an external stylesheet, the link element is placed within the head of the html page:

INTRODUCTION TO HTML5! CSS Styles!

Web Design and Implementation

Web Programming BootStrap Unit Exercises

Web Programming and Design. MPT Senior Cycle Tutor: Tamara Week 2

APPLIED COMPUTING 1P01 Fluency with Technology

HTMLnotesS15.notebook. January 25, 2015

Introduction to using HTML to design webpages

CSS: The Basics CISC 282 September 20, 2014

Chapter 3 Style Sheets: CSS

Index. alt, 38, 57 class, 86, 88, 101, 107 href, 24, 51, 57 id, 86 88, 98 overview, 37. src, 37, 57. backend, WordPress, 146, 148

In the early days of the Web, designers just had the original 91 HTML tags to work with.

CSS BASICS. selector { property: value; }

ICT IGCSE Practical Revision Presentation Web Authoring

- The CSS1 specification was developed in CSSs provide the means to control and change presentation of HTML documents

CE419 Web Programming. Session 3: HTML (contd.), CSS

Web Engineering CSS. By Assistant Prof Malik M Ali

APEX Developers - Do More With Less!!

Introduction to CSS. 2 Sep 13. Derek Peacock. An introduction to defining CSS style rules using tags, classes and ids.

Creating and Building Websites

Designing the Home Page and Creating Additional Pages

The Benefits of CSS. Less work: Change look of the whole site with one edit

HTML/XML. HTML Continued Introduction to CSS

IDM 222. Web Design II. IDM 222: Web Authoring II 1

Web Design and Development Tutorial 03

HIERARCHICAL ORGANIZATION

AGENDA :: MULTIMEDIA TOOLS :: (1382) :: CLASS NOTES

HTML and CSS COURSE SYLLABUS

Transcription:

SASS Variables and Mixins Written by Margaret Rodgers From Web Team Contents 1 Variables o 1.1 Nested Variables 2 Mixins 3 Inheritance Variables A variable in SASS works exactly the same as a variable in any programming language; it's an empty container waiting for something to be put into it. Once it holds a value, that variable can be called into use anywhere. If a global change needs to be made, that change only needs to be made in the appropriate variable and affects every location where that variable is called. To create a new variable in your SASS document, you simply prefix the variable name with a dollar sign. The syntax is also more similar to CSS than a programming variable because a colon is used to assign value rather than an assignment operator: $toplevel_font To give it a value, add a colon after the name and then your value followed by a semi-colon: $toplevel_font: 30px; The idea is exactly the same as in other types of programming; you create your variable, give it a value and call it from within your script. In the case of SASS, of course, you're going to create a variable whose value is one of the properties of a style. So in the example of toplevel_font, if that were going to be the size of every h1 in the stylesheet you would use the variable in all classes containing an h1 element rather than retyping the property in all those classes. So this: h1 { font-size: $toplevel_font; Rather than how we would normally write it: h1 { font-size: 30px;

If you later decide that all your first level headings should be 25 pixels instead of 30, you simply change that value in the variable declaration, recompile the stylesheet and the change is automatically reflected in every h1. Nested Variables In HTML you have a definite structure where child elements are indented so it s clear who belongs to whom. In CSS, however, everything is pretty much a square block; all at the same level. But SASS offers nesting so it s much easier to see what is subordinate to what. Here s an example using an HTML5 article that contains a header, a paragraph and an unordered list. All of these things have styles applied to them. In typical CSS your rules would look like this: ul.contacts{ background-color:green; list-style-type: none; padding:0; ul.contacts li article { border: solid 5px; background-color:#ff5e00; margin-bottom: 10px; padding:10px; ul.contacts li article header { font-style:italic; ul.contacts li article p { color:white; But with SASS we can nest all of those associated classes under one declaration. ul.contacts{ background-color:green; list-style-type: none; padding:0; li { article { border: solid 5px; background-color:#ff5e00; margin-bottom: 10px; padding:10px;

header { font-style:italic; p { color:white; When rendered in the stylesheet, the nested code looks just like the example we started out with. So the nesting does not affect the end product after compilation it just makes it a little easier to read. CSS3 provides this same type of nesting but earlier versions of CSS do not. Mixins Mixins are a way to eliminate redundant styling and do a lot of other neat stuff. For example, let's say you had 15 classes. 10 of them have three properties that are exactly the same and two additional properties that are the same but have one of two different values. Each also has one additional property that has a different value in every class. Normally you would have to write the same thing over and over again so it appears in all 10 of them. That s a lot of extra writing and if you decide to change something, you have to do it all over again! However, if you use a mixin, you simply create a mixin which holds the three properties which are the same. Then you call the mixin in the 10 classes instead of rewriting everything. For demo purposes, we ll say that our 10 classes have the same background color, font color and padding. They all have a font size property which has one of two values; 1em or 1.5em and a width setting that is different in every one. Because those font sizes and widths are different, we can t include that property in the mixin. Or can we? You can configure your mixin in a couple of different ways. You can either use individual CSS property statements and their values or you can create variables, give them the values and then use them in your mixin. I m going to use variables in my example here. So first up, create the variables and give them values. $bgcolor: #0072ff; $fontcolor: white; $paddingall: 10px 30px 10px 10px;

Since we know one of the other properties will have one of two font size values, we ll go ahead and create some variables to hold those while we re at it. $fontsize: 1em; $fontsizebigger: 1.5em Next step is to build the mixin using the three common variables. You ll notice that the mixin declaration really resembles a function declaration in programming languages there is a name, an argument and opening and closing curly braces. However, unlike a language, SASS uses the @ sign to declare a mixin: //Creating the mixin @mixin toomuchredundancy(){ background-color: $bgcolor; color: $fontcolor; padding: $paddingall; The mixin is called into the stylesheet through the use of a SASS '@include' directive. So you simply replace those three property declarations in all class rules that contain them with an include directive followed by the name of the mixin. Now the code in your SCSS file will look similar to this: @include toomuchredundancy; font-size: 1.5em; So we've pared down the class to three lines instead of six. Once compiled, the styles are rendered like this in the stylesheet: background-color:#0072ff; color: white; padding: 10px 30px 10px 10px; font-size: 1.5em; Well, this is really nice, but there are still a couple of properties that handle font size in all 10 of the classes. Can these be included in the mixin after all? You bet! To handle these, we'll use a 'parameterized mixin'.

Because we have the ability to give the mixin an argument as we would with any function, we will need to provide a parameter so it knows to expect something. There are a couple of ways to do this. Let's look at the bluebutton style for a second. Right now it looks like this in the SASS file: @include toomuchredundancy; font-size:1.5em; First we need to adjust the mixin called toomuchredunancy. When we declared it we gave it the ability to accept a parameter should we decide to give it one. So now we need to do that. Let's give it an empty variable that will be replaced with a value later on. @mixin toomuchredundancy($newfont){ background-color: $bgcolor; color: $fontcolor; padding: $paddingall $newfont:$fontsize; I've given it a parameter called $newfont which has no value, it's just an empty container that s acting as a placeholder. That empty container will be filled using one of the font size variables that we declared earlier in the argument, $fontsize for the smaller font or $fontsizebigger, and with that addition we can eliminate another line of code from the SASS file. @include toomuchredundancy($fontsize); Once compiled the CSS looks as you would expect: background-color:#0072ff; color: white; padding: 10px 30px 10px 10px; font-size: 1em; But there is still one more property to deal with; width. Since the width is different for all 10 of the classes, it isn t really viable to create variables to hold the values. Instead, we can pass the

value of the width property to the mixin as part of the argument. To do this, we'll create another mixin and give it a parameter: @mixin setwidth($bestsize){ width:$bestsize; Now, when we call the setwidth mixin, we'll just place the width in the argument instead of the empty variable $bestsize: @include $setwidth(250px); @include toomuchredundancy($fontsize); So in the SASS file, you now have two lines that define the style of bluebutton instead of the original six. After SASS compiles the stylesheet, the resulting CSS looks as you would expect: background-color: #0072ff; color: white; padding:10px 30px 10px 10px; font-size: 1em; Variables and mixins can be used very effectively with media queries as well. Inheritance If there are classes that inherit properties from other rules, you can take advantage of SASS s ability to handle inheritance. For example, we ll say that there are three buttons, all of them are exactly the same except the border around each is different. Because of that, we need to write a series of rules to assign the border property to each button..wideborder{ border: 5px solid;.narrowborder{ border: 1px solid;

.dottedborder{ border: 5px dotted; Since they all inherit the style of the blue button that we defined earlier, they would need to be included in the bluebutton class declaration. We could write them in or use @extend in SASS to have SASS do the work for us. The border definition classes now look like this:.wideborder{ @extend.bluebutton; border: 5px solid;.narrowborder{ @extend.bluebutton; border: 1px solid;.dottedborder{ @extend.bluebutton; border: 5px dotted; When SASS compiles the stylesheet, the compiled CSS looks like this:.bluebutton,.wideborder,.narrowborder,.dottedborder{ background-color: #0072ff; color: white; padding:10px 30px 10px 10px; font-size: 1em;.wideBorder{ border: 5px solid;.narrowborder{ border: 1px solid;.dottedborder{ border: 5px dotted;

You might ask So what s the advantage? I could have written that myself with about the same amount of effort as I spent setting up the SASS stuff! The difference is in the way you assign the style in the HTML. Where before the HTML would require assignment of both classes,.bluebutton and one of the border classes: <div class= bluebutton wideborder > Using the @extend to cause each border class to inherit the properties of bluebutton, only one class needs to be applied to the CSS. <div class= wideborder > There is a huge amount of functionality that I ve not covered here. But these are a few of the basic things that can be done using SASS and the ones I felt would, most likely be of use on our site.