All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the authors, nor Packt Publishing and its dealers and distributors, will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2015
Production reference: 1250115
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-148-9 www.packtpub.com
CuuDuongThanCong.com
www.it-ebooks.info
/>
Credits Authors
Copy Editors
Bass Jobsen
Gladson Monteiro
Amin Meyghani
Sarang Chari
Reviewers
Project Coordinator
Fahad Ibnay Heylaal
Milton Dsouza
Dave Poon Proofreaders
Steve Workman
Lauren E. Harkins Paul Hindle
Commissioning Editor Ashwin Nair
Amy Johnson
Acquisition Editors
Indexer
Richard Brookes-Bland
Monica Ajmera Mehta
Richard Harvey Production Coordinator Content Development Editor
Foreword I became interested in Less after starting work on a project to expand a complex web application that had just two large CSS files (one went over IE's limit for a number of selectors in a file), and it used a regex replacement for theme variables—it was a nightmare. There was no link between colors that were clearly visually linked and numbers that were clearly related, and the connection was not obvious and copy/pasted blocks of CSS, scattered throughout the file. At first, it looked manageable, but then as we made changes and fixed bugs, we found that changing something at one place meant having to find several other places where change was required. What was worse, perhaps, was that developers were just adding yet another more complex selector to override another specific place—ad infinitum—until there were older, simpler selectors that weren't even used anywhere. I thought there must be a better way and found Less, which is a superset of CSS, focused on fixing these kind of maintainability problems by making CSS more declarative. I started off with my involvement by porting Less.js to dotless and then became an integral part of the team, taking over from Alexis in maintaining and expanding Less.js to keep up with the demands of the ever-evolving WWW, third-party library usage, and new ideas for CSS management. I still consider that the primary job of Less is to allow the web developer to have maintainable CSS in their project. For this, abstraction of variables, splitting them up into separate files, and abstraction of common selectors and properties is the most important task (though a long way from what Less can do). We do not implement every feature request but instead try and choose those that have the biggest impact. We are generally against a feature that just provides a different way of doing something. Sometimes this means the solution to problems is not the most obvious one. I hope this will encourage developers to create code with consistent patterns, and I would urge them to try and keep their Less code simple and consistent and ensure it follows the same kind of generally accepted maintainability
approaches that are applied to more traditional programming languages. Where common problems would be better served with new Less features, I hope we identify them and always welcome input, discussions, and help to our Github repository. However, it was a recent aim of the project to enable plugins for Less so that projects that need it can implement their own extensions without burdening the core project with support, for instance, functions for 10 different color models.
CuuDuongThanCong.com
www.it-ebooks.info
/>
One plugin I would really like to push people to use is Less-plugin-autoprefixer. In the future, older browsers will not be in use and old polyfills (such as SVG Gradient backgrounds) and prefixed properties will be, if not a thing of the past, a less common occurrence. By using this plugin, you can write your CSS in a forward-thinking way and do not have to bloat it with mixins for polyfills that will be repeated across every project you will work on. The features most asked for, which I've implemented over the last couple of years, have tended to focus on using libraries. I think this reflects the rise of people using CSS frameworks such as Bootstrap to get a head start at the beginning of a project in order to avoid reimplementing the bare bones. This I think is very positive as it promotes reuse and reduces the number of ways in which fundamentals are done. The biggest problem that remains with libraries is around picking out the bits you want to keep and customizing the library into a project's particular need. Hopefully, Less' import reference feature, not Sass-like extension will help us with this. As with any language, problems always present themselves out of nowhere, and it always helps to get a head start on good solutions. So keep your solutions maintainable and elegant and enjoy reading this book. Luke Page
Technical Lead Developer, Scott Logic Ltd. ( /> page/35/81b/3b6)
CuuDuongThanCong.com
www.it-ebooks.info
/>
About the Author Bass Jobsen has been programming for the Web since 1995, covering everything from C to PHP, and is always on the hunt to find the most accessible interfaces. Based in Orthen, the Netherlands, he has also written Less Web Development Essentials, Packt Publishing, which is a fast-paced tutorial that covers the fundamentals of Less (Leaner CSS) when used in web development. Bass uses Less in his daily job for web design tasks, WordPress theme development, and other Twitter Bootstrap apps. He is always happy to help those with questions ( />users/1596547/bass-jobsen), and he writes a blog you can find at />Also, check out his Bootstrap WordPress starters theme (JBST) and other projects at GitHub ( />This book is for Colinda, Kiki, Dries, Wolf, and Leny. Writing this book wasn't possible without the support of my family, Caroliene, and the people of Vivent. Richard Harvey is a patient and excellent motivator and critical reader. Akashdeep Kundu helped me to dot the i's and cross the t's. I'd also like to thank the reviewers of this book, Dave Poon, Steve Workman, and Fahad Heylaal, for their critical and valuable suggestions that made this book even better. Last but not least, I should not forget to thank the Less core Team: Alexis Sellier (@cloudhead), Jon Schlinkert (@jonschlinkert), Luke Page (@lukeapage), Marcus Bointon (@Synchro), Mária Jurčovičová (@sommeri), Matthew Dean (@matthew-dean),
Max Mikhailov (@seven-phases-max), and all the other contributors who have made coding Less possible in the first place.
CuuDuongThanCong.com
www.it-ebooks.info
/>
Amin Meyghani is a designer and developer currently working at HD MADE
( making automation tools, websites, and apps. He is also a lead developer at Flitti ( leading the team to make next-generation gamification apps. In addition to arts and technology, Amin has always been passionate about teaching. He takes advantage of every opportunity to share his knowledge with the world through books, blogs, or videos. You can find his works and blogs at http://meyghani. com/. When Amin is not coding, he is either enjoying Persian food or mastering his Persian calligraphy techniques. I would like to thank my family for always supporting me and filling my life with love and hope. I owe them this book, as they have always been there for me even in the most difficult times.
CuuDuongThanCong.com
www.it-ebooks.info
/>
About the Reviewers Fahad Ibnay Heylaal is a developer who hails from Bangladesh and is currently living and
working in Amsterdam. Mostly known for being the creator of Croogo (a CMS based on the CakePHP framework), he has progressed to become more of a frontend developer over the last couple of years. If he isn't coding, chances are high that he will be seen cycling around the beautiful canals of Amsterdam.
Dave Poon is a UX/UI designer, web developer, and entrepreneur based in Sydney. He
graduated from Central Queensland University with a degree in multimedia studies and a master's degree in IT. He began his career as a freelance graphics and web designer in 1998 and currently works with web development agencies and medium-sized enterprises. He began his love affair with Drupal afterward and worked for a variety of companies using Drupal. Now, he is evangelizing good user experience and interaction design practices to start-ups and enterprises.
Currently, he is a design lead at Suncorp, one of the biggest financial institutions in Australia. He is the cofounder of Erlango (), a digital product development and design startup located in Sydney and Hong Kong that creates user-centered digital products for clients and users. He is also the cofounder of SpikeNode () which is, a platform for DevOps automation. He is the author of Drupal 7 Fields/CCK Beginner's Guide, Packt Publishing. Also, he is the technical reviewer of the books Drupal Intranets with Open Atrium, Advanced Express Web Application Development, and Mastering Web Application Development with Express, all by Packt Publishing. I would like to thank my wife, Rita, for her endless patience and support. Without her, whatever I do would be meaningless. I would also like to thank my father for his continued encouragement.
CuuDuongThanCong.com
www.it-ebooks.info
/>
Steve Workman is a frontend web engineer and an organizer of the London Web Standards group. He is a champion at creating high-performance sites with the latest web technologies and making developers' lives easier with tools and new languages. I'd like to thank the whole Less community for creating this great language and my wife, Emily, for always being there.
CuuDuongThanCong.com
www.it-ebooks.info
/>
www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com, and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. TM
/> Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe? ff
Fully searchable across every book published by Packt
ff
Copy and paste, print, and bookmark content
ff
On demand and accessible via a web browser
Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
CuuDuongThanCong.com
www.it-ebooks.info
/>
CuuDuongThanCong.com
www.it-ebooks.info
/>
Table of Contents Preface1 Chapter 1: Getting to Grips with the Basics of Less 9 Introduction9 Downloading, installing, and integrating less.js 10 Installing the lessc compiler with npm 14 Using less.js with Rhino 16 Declaring variables with Less for commonly used values 17 Setting the properties of CSS styles with mixins 20 Writing more intuitive code and making inheritance clear with nested rules 24 Creating complex relationships between properties 26 Using the built-in functions of Less 28 Using namespaces to make your code reusable and portable 31
Chapter 2: Debugging and Documenting Your Less Code
33
Chapter 3: Using Variables and Mixins
55
Introduction33 Debugging your code with less.js 34 Using CSS source maps to debug your code 37 Using Chrome Developer Tools to debug your code 41 Commenting your code in Less 44 Building style guides with tdcss.js 46 Building style guides with StyleDocco 50 Introduction55 Deriving a set of variables from a single base variable 56 Value escaping with the ~"value" syntax 60 Using variable interpolation 63 Redeclaring variables based on lazy loading 66 Using mixins to set properties 68
CuuDuongThanCong.com
www.it-ebooks.info
/>
Table of Contents
Declaring a class and mixin at once Using selectors inside mixins Using parametric mixins
71 74 77
Chapter 4: Leveraging the Less Built-in Functions
81
Introduction82 Converting units with the convert() function 82 Using the default() function 84 Embedding images with data URIs 86 Formatting strings 88 Replacing a text within a string 89 Working with lists 92 Using mathematical functions
94 Using the color() function 96 Evaluating the type of a value 99 Creating color objects with RGB values 100 Getting information about a color 105 Creating a color variant with the darken() and lighten() functions 107 Creating overlays of two colors with Less 110
Chapter 5: Extending and Referencing
113
Chapter 6: Advanced Less Coding
141
Introduction113 Referencing parent selectors with the & operator 114 Referencing to the parent selector more than once 118 Changing the selecting order with the & operator 121 Using extend to merge selectors
124 Using extend inside a ruleset 126 Extending with the all keyword 128 Extending with media queries 132 Using extend to reduce the compiled CSS size 135 Using extend as an alternative for a mixin 138 Introduction142 Giving your rules importance with the !important statement 142 Using mixins with multiple parameters 144 Using duplicate mixin names 147 Building a switch to leverage argument matching 150 Avoiding individual parameters to leverage the @arguments variable 153 Using the @rest... variable to use mixins with a variable number of arguments 155 Using mixins as functions 156 Passing rulesets to mixins 161 ii
CuuDuongThanCong.com
www.it-ebooks.info
/>
Table of Contents
Using mixin guards (as an alternative for the if…else statements) Building loops leveraging mixin guards Applying guards to the CSS selectors Creating color contrasts with Less Changing the background color dynamically Aggregating values under a single property
165 168 173 175 180 185
Chapter 7: Leveraging Libraries with Prebuilt Mixins
187
Chapter 8: Building a Layout with Less
245
Chapter 9: Using Bootstrap with Less
277
Introduction188 Importing and downloading prebuilt mixin libraries 189 Using namespacing with prebuilt libraries 192 Creating background gradients 195 Building unlimited gradients with Less Hat 199 Building a layout with the CSS3 flexbox module 202 Getting retina ready with Preboot 208 Generating font-face declarations with Clearless 211 Improving your website's SEO with 3L mixins 215 Leveraging sprite images with Pre 218 Creating bidirectional styling without code duplication 222 Creating animations with animations.css 226 Creating animations with More.less 229
Building semantic grids with semantic.gs 233 Building an alternative for fluid grids with Frameless 236 Building a fluid responsive grid system 240 Introduction245 Using CSS Reset with Less 247 Importing and organizing your Less files 250 Importing files with the @import directive 253 Building a grid with grid classes 256 Creating responsive grids 261 Building a semantic grid with mixins 264 Applying the flexbox grid on your design 266 Integrating a navigation menu in the layout 270 Repositioning your content 273 Introduction277 Downloading and installing Bootstrap 278 Customizing Bootstrap with variables 280
Making custom buttons 283 Making custom panels 285 iii
CuuDuongThanCong.com
www.it-ebooks.info
/>
Table of Contents
Making custom navigation bars Extending components using :extend Reusing Bootstrap's grid Using Bootstrap classes and mixins Extending Bootstrap with your own mixins Making custom color schemes with 1pxdeep Autoprefixing Bootstrap's CSS
287 292 294 296 299 301 303
Chapter 10: Less and WordPress
307
Chapter 11: Compiling Less Real Time for Development Using Grunt
327
Introduction307 Installing WordPress 308 Developing your WordPress theme with Less 310 Integrating Bootstrap into your WordPress theme 313 Using Semantic UI to theme your WordPress website 316 Customizing Roots.io with Less 319 Building a WordPress website with the JBST theme 322 Introduction328 Installing Node and Grunt 328 Installing Grunt plugins 331 Utilizing the Gruntfile.js file 335 Loading Grunt tasks 337
Adding a configuration definition for a plugin 338 Adding the Less compiler task 340 Creating CSS source maps with the Less compiler task 344 Cleaning and minimizing your code 346 Adding the watch task 347 Adding the connect and open task 350 Adding the concurrent task 353 Analyzing your code with CSS Lint 355 Removing unused CSS using Grunt 358 Compiling style guides with Grunt 359 Automatically prefix your code with Grunt 361 Installing the Grunt LiveReload plugin 363
Index367
iv
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface CSS has dramatically changed since its very first emergence, and it is continuing to evolve. In particular, the emergence of CSS3 has added many new features to CSS, including gradients and animations. Along with this are many new opportunities to build websites using only CSS and HTML. Developers are no longer dependent on techniques such as Flash and other tricks to build interactive and fancy websites. CSS3 has played an integral role in building responsive websites, where CSS media queries have made it possible to apply some styles dependent on the width of the browser's viewport only. Despite this improvement, CSS is inherently, at its core, a simple style sheet language that lacks some fundamental programming features such as variables, functions, and operators. The need for more maintainable CSS, especially with the explosion of complex web apps, has made CSS preprocessors such as Less a necessity in enabling us to write more readable and manageable versions without breaking cross-browser compatibilities. Although Less cannot magically change CSS, it certainly provides us with the tools to help structure, modularize, debug, and maintain small or large CSS projects more easily. By extending CSS with variables, functions, and mixins; nesting CSS selectors; and allowing you to follow the don't repeat yourself (DRY) principle of software programming, Less behaves more like a programming language in a way that CSS never was. Despite some of the programming characteristics of Less, you should not be put off by this; by being built as a superset of CSS, its features are implemented in the CSS way and it follows W3C standards where possible. Designers and developers who are familiar with CSS will find coding in Less very natural. Because Less fixes these shortcomings of CSS, the best time to start using Less is now!
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface In this book, you are going to explore the Less preprocessor, most of its core, and some of its less frequently used features. Through these very easy-to-follow and practical recipes, you will learn how to write more maintainable and scalable CSS. You will explore making components and structures through reusable mixins and extends. We will also learn about frameworks that are based on Less, exploring their features and how they can be seamlessly integrated into your own projects. In addition, you will learn how to use prebuilt mixin libraries for your current or upcoming projects. Finally, you will look at debugging techniques that have been available for other preprocessors and are now available to Less through source maps. By the end of this book, you will have an extended knowledge and a good understanding of the power of Less, its libraries, and the important features it has to offer to make writing your CSS more natural, productive, and intuitive.
What this book covers Chapter 1, Getting to Grips with the Basics of Less, shows you how to install the Less compiler for client- and server-side usage. After the installation, you will be shown how to make use of the basic features of Less: using variables, mixins, operations, built-in functions, and namespaces; how to nest your rules will also be on the menu here! Chapter 2, Debugging and Documenting Your Less Code, shows you how to debug your Less code using your CSS source maps and browser developer tools. You will also be introduced to style guides and learn how to properly comment your code. Chapter 3, Using Variables and Mixins, covers the advanced usage of variables and mixins in Less. After reading this chapter, you will know how to use variables to create reusable Less code and use mixins to make your CSS properties interactive.
Chapter 4, Leveraging the Less Built-in Functions, explains the different types of built-in functions of Less. You will find examples of each type of function, including functions for color manipulation and mathematical operations. Chapter 5, Extending and Referencing, shows you how to extend and reference selectors and properties to help you write better CSS and reduce the size of the compiled CSS code. You will learn to change the order of selectors and merge them. Chapter 6, Advanced Less Coding, walks you through the process of parameterized mixins and shows you how to use guards. A guard can be used with as if-else statements and make it possible to construct interactive loops in Less.
2
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface Chapter 7, Leveraging Libraries with Prebuilt Mixins, explains how to install and use the libraries of prebuilt mixins. You will explore different libraries such as Less Elements, Less hats, and Preboot to build background gradients, grids, and animations in Less, among others. Chapter 8, Building a Layout with Less, takes you through the process of creating a complete website layout with Less. The layout will be built with a responsive and semantic grid and will include a vertical menu. Finally, you will also learn how to use iconic fonts with Less. Chapter 9, Using Bootstrap with Less, shows you how to customize Bootstrap and its components using Bootstrap's Less source files. You will also learn how to use Bootstrap's mixins to make semantic and reusable layouts and components. Chapter 10, Less and WordPress, shows you how to use Less when theming your WordPress
site. This includes examples of the Roots.io, SemanticUI, and JBST WordPress themes. You will be shown how to customize the WooCommerce plugins with Less along with integrating Less into your other WordPress themes and plugins. Chapter 11, Compiling Less Real Time for Development Using Grunt, shows you how to set up a Less compiler using Grunt for real-time compilation. It will also show you how to use several Node modules for your Less development. By the end of this chapter, you will be very comfortable setting up a development environment with Grunt and its plugins.
What you need for this book The latest version of Less.js can be downloaded for free from />You will definitely need a text editor to write your code; however, it does not matter which text editor or operating system you use. You will also need a modern web browser, namely the latest versions of Mozilla Firefox, Google Chrome, or Microsoft Internet Explorer. Please note that some recipes, especially in the final chapter, require some basic familiarity with the command line. All the tools used in the last chapter are cross-platform and are available for free, so you should be fine using your favorite operating system. Grunt plugins can be found by visiting /> Who this book is for This book is mainly intended for web developers and designers who are comfortable with CSS and HTML. If you are someone with some experience with CSS, you will find the learning curve of understanding the Less syntax to be less steep. Although this book is beneficial to those who have had some experience with using Less, web developers and designers who would like to use this book as a gateway to learning the program can still benefit and harness its true power.
3
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface
Sections In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also). To give clear instructions on how to complete a recipe, we use these sections as follows:
Getting ready This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
How to do it… This section contains the steps required to follow the recipe.
How it works… This section usually consists of a detailed explanation of what happened in the previous section.
There's more… This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
See also This section provides helpful links to other useful information for the recipe.
Conventions In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "To start the process, you will have to edit your index.html file."
4
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface A block of code is set as follows: header { color: blue; } section { color: green; } footer { color: purple; }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: href="less/styles.less" /> <script type="text/javascript">less = { env: 'development' };</script>
Any command-line input or output is written as follows: java -jar js.jar -f less-rhino-1.7.0.js lessc-rhino-1.7.0.js example.less example.css
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In the Styles tab of the Developers Tools page, you will find the rules that are applied while selecting a selector." Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback Feedback from our readers is always welcome. Let us know what you think about this book— what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
5
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface To send us general feedback, simply e-mail , and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or
contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.
Downloading the color images of this book We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: />downloads/B01849_Coloredimages.pdf.
Errata Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to />content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
6
CuuDuongThanCong.com
www.it-ebooks.info
/>
Preface
Piracy Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.