Skip to content

Instantly share code, notes, and snippets.

@patrickmarabeas
Last active December 15, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickmarabeas/5328361 to your computer and use it in GitHub Desktop.
Save patrickmarabeas/5328361 to your computer and use it in GitHub Desktop.
LESS nesting for parental examination - In this example we wish to assist our media queries with JavaScript to determine if the device is a mobile device or not. Our JavaScript will place a class or attribute on the body tag. When we get to our media queries, we only want to apply the code if body has the class or attribute confirming the device…
body {
/* this has the possible class or data attribute of 'mobile' */
/* this is not included in the nest as you can't climb at will */
/* the class or attribute (via our JavaScript) could be applied
to any element that sits above and outside of `#level-1` in
this example */
}
#level-1 {
#level-2 {
#level-3 {
#level-4 {
body:not(.mobile) & {
/* produces the following CSS */
/* body:not(.mobile) #level-1 #level-2 #level-3 #level-4 { } */
/* rules in this block will only be executed if body does not have a mobile class */
@media screen and (max-width:) {
}
}
body:not([data-device=mobile]) & {
/* produces the following CSS */
/* body:not([data-device="mobile"]) #level-1 #level-2 #level-3 #level-4 { } */
/* rules in this block will only be executed if body does not have a mobile data-device attribute */
@media screen and (max-width:) {
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment