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;
}
comments powered by Disqus

Related Posts

JavaScript Can Have An Interesting Interpretation of Order

There is an interesting little quirk with the way in which JavaScript decides which function it should execute next. You see, while the JavaScript engine has a single thread of execution, it creates the illusion of multiple simultaneous processes running at once by utilizing a queue of functions to execute. This means that every time you make a call to a function in your JavaScript, there is no absolute guarantee that it will be the next piece of code run, as there may have been other events triggered that beat your custom function onto the execution queue.

Read More

Ignore Whitespace Not Available in BitBucket Pull Requests

When looking for an online location to use as the host for source code, many people by default look at GitHub , as it seems to be the most well-known option out there, and is free for open source projects. However, if you would like your source code to be kept private, or would rather use Mercurial instead of Git, GitHub may not be the place for you. Instead, I would suggest BitBucket as your source code repository provider.

Read More

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.

Read More