Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
LESS WEB DEVELOPMENT COOKBOOK

You're reading from   LESS WEB DEVELOPMENT COOKBOOK Over 110 practical recipes to help you write leaner, more efficient CSS code

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783981489
Length 394 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
 Jobsen Jobsen
Author Profile Icon Jobsen
Jobsen
 Meyghani Meyghani
Author Profile Icon Meyghani
Meyghani
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting to Grips with the Basics of Less FREE CHAPTER 2. Debugging and Documenting Your Less Code 3. Using Variables and Mixins 4. Leveraging the Less Built-in Functions 5. Extending and Referencing 6. Advanced Less Coding 7. Leveraging Libraries with Prebuilt Mixins 8. Building a Layout with Less 9. Using Bootstrap with Less 10. Less and WordPress 11. Compiling Less Real Time for Development Using Grunt Index

Writing more intuitive code and making inheritance clear with nested rules

HTML elements in the hierarchy of the Document Object Model (DOM) of HTML5 documents are nested while CSS, on the other hand, does not reflect this nested structure. Less makes nesting of CSS selectors possible. With the nested selectors being used, your code reflects the nested structure of HTML5.

Getting ready

To get started, you will need to create a valid HTML5 file, including some nested elements. Your HTML, for instance, may look like the following code:

<section role="main">
<h1>heading</h1>
<p>some content</p>
</section>

You will also have to create an empty Less file named project.less. Make sure the head section of your HTML5 document also contains the following code:

  <link rel="stylesheet/less" type="text/css" href="project.less">
  <script src="less.js" type="text/javascript"></script>

How to do it…

In CSS, the section with the nested h1 and p elements can, for instance, be styled with the following CSS code:

section h1 {}
section p {}

However, with Less, you can style the same elements using the following Less code:

section {
  h1 {}
  p{}
}

How it works…

In the preceding example, nesting the selector mimics the nested structure of your HTML code. Nesting makes the code intuitive and so much easier to read and maintain. Less's code will also be more concise than its corresponding CSS code. You should use nesting with care; nesting too much will break your CSS code after small changes in your HTML. You should not try to nest your complete HTML structure, but nesting will be very useful to assign pseudo classes, such as hover, to your elements.

Note that the nested selectors in Less still compile to un-nested selectors in CSS.

To see how this works, use the following Less code:

section {
  h1 {font-size: 20em;}
  p{ padding: 0 10px;}
}

The preceding code will compile into the following CSS code:

section h1 { 
  font-size: 20em;
} 
section p { 
  padding: 0 10px; 
} 

There's more…

Although nesting your selector can make your code more intuitive, it can equally break other things. For instance, considering object-oriented CSS (OOCSS) principles; these do not allow nesting of headings (h1 to h6). Headings are considered to be built-in objects in OOCSS and so their appearance should be consistent across an entire site.

See also

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
LESS WEB DEVELOPMENT COOKBOOK
You have been reading a chapter from
LESS WEB DEVELOPMENT COOKBOOK
Published in: Jan 2015
Publisher:
ISBN-13: 9781783981489
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon