1

Is something like this possible?

div[id^=red]:hover {
        color: (/red/);
    }
<div id="red"></div>

2
  • 2
    This is not possible. CSS does have an attr() function which can access the element's attributes, however you can only use it to affect the content property of a before/after pseudo element. Commented Mar 16, 2016 at 3:19
  • do you know if there is a way to do this with the html bgcolor attribute? I'm trying to do a hover-over-image-overlay effect. Is there a way to work with the opacity? Commented Mar 16, 2016 at 3:47

1 Answer 1

1

You can't do this with CSS only, but if you can edit the HTML there are workarounds.

The first option is to add a class that represents a color and style it accordingly, this is not very efficient, but it does the job. Of course, it wont work if your colors are dynamic:

div {
  width: 100px;
  height: 100px;
  background: grey;
}
.red:hover {
  background: red;
}
.blue:hover {
  background: blue;
}
.green:hover {
  background: green;
}
<div class="red"></div>
<div class="blue"></div>
<div class="green"></div>

Another tricky solution, since you plan to use :hover, is to set the color and override it on the idle state:

div {
  width: 100px;
  height: 100px;
}
div:not(:hover) {
  background: grey !important;
}
<div style="background: red;"></div>
<div style="background: blue;"></div>
<div style="background: #fa8732;"></div>
<div style="background: #bb0099;"></div>
Which is more efficient since you can add any color you want in you HTML and you don't have to worry for the CSS.

Of course this will work for any property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.