less.ruby icon indicating copy to clipboard operation
less.ruby copied to clipboard

Reference to parent selector

Open DouweM opened this issue 15 years ago • 11 comments

Is there any way to reference to the parent selector? To do something like this: a.button { height: 20px; width: 100px; background-repeat: no-repeat;

  &.email {
    background-image: url(email.png);
  }
  &.twitter {
    background-image: url(twitter.png);
  }
}

As you can see, I am using the & character to reference to the parent selector. My goal is to apply the specified background-images to a.button.email and a.button.twitter, without having to write a.button again, when I'm already in the a.button block.

DouweM avatar Nov 29 '09 11:11 DouweM

not yet, see http://github.com/cloudhead/less/issues/#issue/109

jtousek avatar Nov 30 '09 16:11 jtousek

Do you mean to accomplish the following?

a.button {
  height: 20px;
  width: 100px;
  background-repeat: no-repeat;
}

a.email {
  background-image: url(email.png)
}

a.twitter {
  background-image: url(twitter.png)
}

Are you trying to use the parent selector to discriminate against as that don't have the button class? If it's not likely that any a tag will have the email or twitter classes and not the button class, this will work as expected.

xdissent avatar Nov 30 '09 17:11 xdissent

more likely

a.button {
height: 20px;
width: 100px;
background-repeat: no-repeat;
}
a.button.email {
background-image: url(email.png)
}
a.button.twitter {
background-image: url(twitter.png)
}

jtousek avatar Nov 30 '09 17:11 jtousek

Wow, I had completely forgotten about chaining class names. You're right, that's far more accurate.

So I guess we need a more compelling use case for this feature, right?

xdissent avatar Nov 30 '09 17:11 xdissent

What jtousek says. I want the & character (or any other special char) to reference to the parent selector, just like pseudo-classes like :hover do by default.

DouweM avatar Nov 30 '09 17:11 DouweM

xdissent: you mean something like this? (it convinced cloudhead) http://github.com/cloudhead/less/issues/#issue/109/comment/77628

jtousek avatar Nov 30 '09 18:11 jtousek

Exactly! Sheesh, I'm behind the times. But for clarification, in Douwe's case, chaining classnames with a period will suffice. Like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>OMGWOW</title>
    <style type="text/css">
        a {
            background-color: #f00; /* Red */
        }

        a.email {
            background-color: #0f0; /* Green */
        }

        a.twitter {
            background-color: #00f; /* Blue */
        }

        a.button {
            background-color: #ff0; /* Yellow */
            border: 1px solid #000;
        }

        a.button.email {
            background-color: #0ff; /* Cyan */
        }

        a.button.twitter {
            background-color: #f0f; /* Magenta */
        }
    </style>
</head>
<body>
    <p><a href="http://example.com">Link</a></p>
    <p><a class="email" href="mailto:[email protected]">Email link</a></p>
    <p><a class="twitter" href="http://twitter.com">Twitter link</a></p>
    <p><a class="button" href="http://example.com">Button</a></p>
    <p><a class="button email" href="mailto:[email protected]">Email button</a></p>
    <p><a class="button twitter" href="http://twitter.com">Twitter button</a></p>
</body>
</html>

Thanks though, I do see how this feature could be very useful now. Maybe merge these issues?

xdissent avatar Nov 30 '09 18:11 xdissent

Well, when LESS would implement some way to reference the parent selector, you could do that like this: a.button { background-color: #ff0; /* Yellow */ border: 1px solid black;

    &.email {
        background-color: #0ff; /* Cyan */
    }

    &.twitter {
        background-color: #f0f; /* Magenta */
    }
}

(Assume & is the special character)

Much cleaner in my opinion ;-)

DouweM avatar Nov 30 '09 18:11 DouweM

Right, but it's possible to accomplish right now, which is cool. I agree this should be implemented, though. It is nicer than the alternative, especially with long selectors.

xdissent avatar Nov 30 '09 18:11 xdissent

Yeah, of course it's possible, everything you can do in LESS is possible in CSS, it's just way easier / cooler / cleaner ;-)

DouweM avatar Nov 30 '09 18:11 DouweM

Technically true. =)

xdissent avatar Nov 30 '09 18:11 xdissent