Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Attribute Selector: Apply class if custom attribute has value? Also, will it work in IE7+?

I'd like to use CSS Attribute Selector to apply a class to some spans, but only if they have content. I have a custom attribute that will only have a value if the span has content: "entityvalue". When using the CSS below, it does not apply the class to any elements, even if entityvalue='1634' is in the HTML.

.ApplicationName[entityvalue="*"]  {
                 display: inline;
                 background-image: url("../images/icon_application.PNG");
                 background-repeat: no-repeat;
                 background-position: left;
                 padding-left: 12px;
                 }

I tried .ApplicationName[entityvalue!=""] as found on some sites but apparently it's not in the CSS standards (doesn't work in Chrome) which really sucks, because .ApplicationName[entityvalue=""] does exactly the opposite of what I need...

Here are examples of a span/div WITH content (that should be styled) and a span/div WITHOUT content (which should NOT be styled):

<div class="ApplicationName Redirect" entitytype="Application" entityvalue=""></div>

<div class="ApplicationName Redirect" entitytype="Application" entityvalue="1234">Microsoft Outlook</div>
like image 522
THE JOATMON Avatar asked Jun 20 '11 19:06

THE JOATMON


People also ask

When working with attribute selectors how can you select elements which contain a particular attribute value?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

What CSS selector is used to select elements with a specified attribute and value?

The [attribute=value] selector is used to select elements with the specified attribute and value.

Can we use contains in CSS selector?

A custom built CSS selector for more advanced users can be built using the "contains" selector. The "contains" selector is useful in cases where we are looking for an element that "contains" some text (case sensitive).

Can classes be used as selectors for specific elements with CSS?

The .class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.


1 Answers

Omit the value part:

.ApplicationName[entityvalue]

This works with IE7+.

If you're paranoid about entityvalue being set to emptiness, and you don't want to include those elements:

.ApplicationName[entityvalue]:not([entityvalue=""])

This does not work with IE7+.

If you do need to cater to that, well, you have some options:

  • Define an override/reset style for .ApplicationName[entityvalue=""], so you have one rule with the first selector above, and another rule with this one.

  • Use JavaScript to look for elements with the empty attribute and add a class which you can style.

  • If you can modify server-side code to output that attribute differently, that's an even easier route to take.

like image 188
BoltClock Avatar answered Oct 11 '22 19:10

BoltClock