Book Image

Learning less.js

Book Image

Learning less.js

Overview of this book

Table of Contents (22 chapters)
Learning Less.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Imagine the scene if you will—it's 5 pm, late in the day, and your client wants the impossible…

You know the scene—you've created a kick-ass website, but the client isn't happy with the color of the buttons you've used on the site. They say the color shades used aren't quite there and need tweaking. A reasonable request, right? Except that there are dozens of buttons throughout the site, with most of them using different colors…Oh heck… no chance of an early finish then…

Or, is there? There is—what if we could change a handful of values and it automatically changes each button for you? Sounds crazy, right?

Wrong, it is absolutely possible. Welcome to the world of CSS preprocessors and Less! The power of Less means that we can set a couple of values that can be applied to any number of elements (such as buttons). Rather than having to change all of the buttons manually, we change the values and hit a button to recompile our code. Voilà! The code is instantly updated, and the buttons show the new color.

Throughout this book, we'll meet the Less library, learn how to use it, and apply its power to a number of real-world scenarios, such as updating buttons to build a complete theme for CMS systems, such as WordPress. We'll take a look at the subjects such as animating, color management, abstracting frameworks, and creating media queries for responsive sites.

It's going to be a great journey, full of twists and turns—the question is, are you ready? If so, let's make a start…

What this book covers

Chapter 1, Introducing Less, takes us through the roles that both HTML and CSS play, and examines the inherent limitations of using CSS as a technology. We begin our journey by taking a look at the role CSS preprocessors play and how using Less can act as a solution for some of these limiting issues.

Chapter 2, Building a Less Development Toolkit, is where we get to know Less for the first time, with a look at how we can incorporate it in our code, explore its syntax, and create some basic styles. We'll take a look at the different ways of compiling Less into valid CSS and why it is best to precompile code rather than use it dynamically in the browser.

Chapter 3, Getting Started with Less, delves into the wide range of tools and applications that are available and can be useful for working with Less; the chapter will provide some hints and tips on how to build an effective toolkit for working with Less, which you can integrate into your own development workflow.

Chapter 4, Working with Variables, Mixins, and Functions, continues from where we left off in Chapter 2, Building a Less Development Toolkit, with a look at one of the key concepts of Less, in the form of mixins. We'll take a look at this incredibly powerful tool, which will help you to save a lot of time when developing Less; we will also cover how we can create variables and functions to create our CSS styling when working with Less.

Chapter 5, Inheritance, Overriding, and Nesting in Less, examines how, with a little forethought and careful design, we can use the power of Less to create new styles based on existing ones, but without the need to duplicate the existing code. We'll also see how Less allows us to split style sheets into smaller, more manageable files, where we can group common styles together, making it easier to manage our development.

Chapter 6, Migrating Your Site to Less, contains the answer to the question asked by many developers when starting with Less: how can I incorporate it into existing sites? We'll take a look at some of the tips and tricks that we can use to gradually transition a site to use Less while still maintaining the existing CSS until it has been converted to its Less equivalent.

Chapter 7, Manipulating Fonts with Less, examines how, with some simple tricks, we can easily maintain any font style used within our site with the help of Less; we'll see how, with a little care and planning, we can make minimal changes that will quickly update font styles throughout the whole site.

Chapter 8, Media Queries with Less, takes a look at how we can use Less to quickly and effectively construct responsive sites using the power of media queries. We'll take a brief look at how media queries work and then move on to take a look at how we need to set expectations with clients and decide what should be supported, before using Less to build our queries.

Chapter 9, Working with Less in a CMS, takes us through how Less can be used to great effect when managing styles for any content management system available today. In this chapter, we'll use WordPress as our example to see how Less can first be incorporated directly in code or by using plugins. We'll then move on to the conversion process, with a look at how to transition a WordPress site to use Less, and how we can remove the need to manually compile styles with the use of a Grunt plugin.

Chapter 10, Using Bootstrap with Less, continues our journey through frameworks with a look at the popular Bootstrap system and how it uses Less to create its styles. We'll take a look at its file structure and some of the mixins it uses before configuring it for use on a demo web page as a part of developing a workflow for using Bootstrap with Less.

Chapter 11, Abstracting CSS Frameworks with Less, illustrates one of the pitfalls of using frameworks, where the supplied code can be nonsemantic and inefficient. In this chapter, we'll learn why frameworks aren't always the answer to everything and that they can make it hard to transition to a different solution if we want to change. We'll take a look at how we can use Less to help simplify complex styles, keep our HTML clean, and ultimately make frameworks work for us, and not the other way around.

Chapter 12, Color Processing with Less, covers one of the most important aspects of any website—colors! CSS styling can make maintaining colors difficult. In this chapter, we'll take a look at how we can bring the power of image processing to our CSS development with the use of Less. We'll also learn how, with a little care, we can begin to reduce our reliance on graphic packages such as Photoshop as part of our development workflow.

Chapter 13, Animation with Less, takes us on a journey to show how Less can be used to help simplify the pain experienced when animating elements and objects on a web page. In this chapter, we'll see how animations work, briefly cover the different types of animations available, and see how Less can simplify the markup, before taking a look at using our skills to produce a simple animated menu that could be used on any site.

Chapter 14, Extending and Contributing to Less, is the concluding chapter in our journey through the world of Less with a look at how we can give back to the project and help develop the library further. We'll see how to report bugs, where to find the documentation for the library, and contribute any code fixes or improvements to the library.

Appendix, Color Functions in Less, lists details of each function, within four groups of defining color formats, channeling colors, performing color operations, and blending colors.

What you need for this book

All you need to work through most of the examples in this book is a simple text or code editor, a copy of the Less library, and a browser. I recommend that you install Sublime Text—either Versions 2 or 3—as we will go through how to configure it for use with Less, both for syntax and compilation purposes.

Some of the examples make use of additional software, such as WordPress or Crunch!—the details are included within the appropriate chapter along with links to download the application from the source.

Who this book is for

The book is for frontend developers who need to quickly learn how to use Less in order to write CSS styles more efficiently with less code. To get the most out of this book, you should have a good working knowledge of HTML, CSS, and JavaScript, and ideally be comfortable with using jQuery.

Conventions

In this book, you will find a number of styles of text 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: "In our example, we've added a reference to the Less object and then used the modifyVars method to change the color of the @button-color variable, which we've specified in object.less, to #61783F."

A block of code is set as follows:

header {
  margin-bottom: 25px;
  nav {
    height: 25px;
    a { color: white }
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

.shape1 {
  color: #5cb100;
  border: 1px solid #5cb100;
}

.shape2 {
  background: #fff;
  color: #5cb100;
}

.shape3 {
  border: 1px solid #5cb100;
}

Any command-line input or output is written as follows:

npm install -g grunt-cli

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on Continue to begin the installation."

Note

Warnings or important notes appear in a box like this.

Tip

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

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 would report this to us. By doing so, you can save other readers from frustration and help us to improve subsequent versions of this book.

If you find any errata, please report them by visiting http://www.packtpub.com/support, 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.