0

I am customizing a jQuery theme to add custom icons for some buttons. Here is a jsfiddle - http://jsfiddle.net/Apeksha82/nctPc/2 (My custom CSS definitions are at the bottom)

What I have done is taken some CSS definitions which were already there, and made them more specific like so:

.black-tie .lnkHeader .ui-state-hover .ui-icon, .black-tie .lnkHeader .ui-state-focus .ui-icon {
background-image: url("http://jqueryui.com/resources/images/themeroller/st-stephens.jpg");

}

"lnkHeader" is a class defined by me. Some of my custom styles are not being applied and I cannot figure out why. e.g.

.black-tie .lnkHeader .ui-icon .menu-home { background-position: 50px 50px; }

I have made sure the button has all these classes either assigned to itself or to its parents. I have tried switching around the order of the classes in the CSS definition file. If I remove "lnkHeader" from the above definitions, the CSS for hover image works, but background-position and margin still does not.

.black-tie .lnkHeader .ui-icon .menu-newPage { background-position: 10px 10px; } .black-tie .lnkHeader .ui-button-text-icon-primary .ui-icon, .black-tie .lnkHeader .ui-button-text-icons .ui-icon { margin-top: -14px; }

I need lnkHeader to be part of the definition so that my customizations do not affect other buttons.

I believe I have considered the two factors that affect CSS - inheritance & specificity, in the way I have defined my custom CSS. But obviously I am missing something. Please help. TIA!

1 Answer 1

2

In your html, you have this item:

<div class="black-tie"><a class="lnkHeader">
<span class="ui-button-icon-primary ui-icon menu-home"></span>
</a></div>

What it looks like you need to select this <span> is

.black-tie .lnkHeader .ui-icon.menu-home { ... }

(with no space between .ui-icon and .menu-home)

And then similarly for the other elements:

.black-tie .lnkHeader .ui-icon.menu-newPage { ... }
.black-tie .lnkHeader.ui-button-text-icon-primary .ui-icon { ... }

I don't see anything with the class .ui-button-text-icons in the fiddle, so can't say what the last selector's supposed to match.


  • Whitespace between selectors means children; eg. .ui-icon .menu-home means all tags with class=menu-home that are children (direct or descended) of tags with class=ui-icon

  • No whitespace means same DOM element and is equivalent to a logic AND; e.g. .ui-icon.menu-home means all tags which have both classes: class="ui-icon menu-home";

    You can also combine different types of selectors this way. For example, the following selector: div.my-class[data=my-data] would only match <div class=my-class data=my-data>

  • Direct children are selected with the greater than sign -- and with this, whitespace doesn't matter; eg. .lnkHeader > .ui-icon means all tags with class=ui-icon which are one level below tags with class=lnkHeader

You can find a full reference here: http://www.w3schools.com/cssref/css_selectors.asp

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

3 Comments

Thanks! That helped me get the background-position applied. Still trying to get the other attributes to work. The class .ui-button-text-icons is automatically applied as a result of the call to jQuery button widget.
Update: I have been able to resolve all issues using the same technique, of removing the space between two CSS class names. Thanks a bunch for that! Does removing the space imply "direct child"? I would appreciate it if you could elaborate on why it works the way it does.
@Apeksha Edited into answer; also included a link to the w3schools page

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.