Using The Ampersand With Compass

While much of working with Compass to generate the CSS for your site is straightforward, there are a few ways to use Compass the provide great power, but are not as easy to understand at first glance. This article discusses one such way, hopefully making it easier to understand.

The operator that we will be looking at first is the & operator. The & as part of a selector in Compass allows you to take the entire selector string at a higher nesting level than the & currently resides upon, and replace the & with that selector string. Typically, when this precedes a selector with a space between the & and the selector, it would operate normally as if the & were not present. However, if you remove the space, it allows you to select elements with multiple classes assigned to them as part of a single selector token.

.menu {
	li {
    	width: 100%;
        color: blue;
        &:hover {
        	color: white;
        }
    }
}

The above snippet of Compass would be converted to the following CSS.

.menu .li {
	width: 100%;
    color: blue;
}
.menu .li:hover {
	color: white;
}

While this is a simple usage of the &, there is a much more powerful way to utilize it. That would be to put the & at the end of the selector. This placement causes the complete parent selector chain to be added to the end of the selector. While it may just seem that you would properly nest your selectors instead, there are a few ways that this becomes important. The most impactful reason that I utilize this form of nesting is due to the need to occasionally target specific Modernizr classes that are added to the HTML element. As a result, if I want to check to see whether flexbox is supported in the browser with the above example, it would turn into something like this.

.menu {
	li {
    	width: 100%;
        color: blue;
        &:hover {
        	color: white;
        }
        .no-flexbox & {
        	padding: 0 10px;
        }
    }
}

The generated CSS would look similar to the following.

.menu .li {
	width: 100%;
    color: blue;
}
.menu .li:hover {
	color: white;
}

.no-flexbox .menu .li {
	padding: 0 10px;
}

Related Posts

Jan 8, 2015
3 minutes

Write Bulletproof JavaScript

While display issues have long been the bane of a web developer’s existence, current web development projects tend to have much more client side interactivity, focusing ever more attention on the reliability and resilience of the JavaScript you write to deliver the complete interactive experience. Many things can cause unexpected errors in your carefully crafted code.

However, there are a few things that you can do to make sure that your site degrades gracefully and still provides a basic level of functionality when something in the browser goes wrong. The following snippet of code illustrates a few best-practices for defining your JavaScript namespaced modules.

Mar 20, 2015
One minute

Google To Begin Rewarding Mobile-Friendly Websites

Google recently announced that beginning April 21, 2015, they would start slightly rewarding websites that are mobile-friendly at the expense of sites that are not. There are several things that Google looks at to determine whether or not a site is easy for a user on a mobile device to view and navigate. Some of the things that Google looks for include the following:

  1. Fonts that are big enough to be legible
  2. Users don’t have to scroll left and right to see content
  3. Links are big enough and have enough space around them to be clickable with a touch of a finger.
  4. Avoids technologies that are not present on mobile devices, like Flash.

If you make sure that you follow the above guidelines, your site will be prepared for the upcoming change in Google’s search results. To find out more, check out Google’s blog post.

Jul 7, 2014
2 minutes

AddThis Can Cause Your Site To Not Load

Over the last few days, I have run across quite a few websites that seem to never finish loading. After waiting for 20 seconds or more, I give up, realizing that whatever content was on the site wasn’t worth it anymore for me to stare at a blank white browser until it loaded however much longer. Unfortunately for those websites, they are losing traffic that they will never get back.