views:

108

answers:

4

I have an area that is identified by a #id and there is a CSS like:

#id ul li {
    margin:0;
}

can I, for a specific UL in that area, override the margin-setting? I understand that #id creates very high priority in evaluating the formatting.

I have tried:

.myclass ul li {
    margin-left: 20px;
}

and

#id ul.myclass {

as well as

#id li.myclass {

Is it even possible?

+1  A: 
.myclass ul li {
    margin-left: 20px !important;
}

Should do the trick :)

HurnsMobile
Avoid using !important. This clause was developed for other purposes and as GaVrA sated. It would work, but WILL have severe consequences. It will be impossible to overwrite with any other selector. This is hard to debug and is very probable, that it will interfere with other selectors. Especially if you will try to change css in few months from now, when you will forget there was an !important clause in some place. You will get much more problems with overriding it in future than you have now with a selector containgh #id.
SWilk
I will admit that it is a bit hacky, but hard to debug? Firebug makes it pretty apparent which properties are coming from where...
HurnsMobile
A: 

http://www.w3.org/TR/WCAG10-CSS-TECHS/#user-override

In order to ensure that users can control styles, CSS2 changes the semantics of the "!important" operator defined in CSS1. In CSS1, authors always had final say over styles. In CSS2, if a user's style sheet contains "!important", it takes precedence over any applicable rule in an author's style sheet. This is an important feature to users who require or must avoid certain color combinations or contrasts, users who require large fonts, etc. For instance, the following rule specifies a large font size for paragraph text and would override an author rule of equal weight:

P { font-size: 24pt ! important }
GaVrA
+1  A: 

Avoid using !important. This is hard to debug and is very probable, that it will interfere with other selectors. Especially if you will try to change css in few months from now, when you will forget there was an !important clause in some place.

You need to put more specific selector than the previous one. Just use the class and id parts in one selector.

Try using either

#id .myclass ul li {
    margin-left: 20px;
}

or

.myclass #id  ul li {
    margin-left: 20px;
}

depending on where the element with "myclass" class is located in the DOM tree - if it is the parent of the #id element use first example, otherwise the second. If you want to be independent of the #id element, try to use:

#id .myclass ul li,
.myclass #id ul li,
.myclass ul li {
    margin-left: 20px;
}

This will work for all li's inside ul inside .myclass element, and it will not matter whether there is any #id element in the tree.

Best regards, SWilk

SWilk
Thanks for the explanation. As described above in my comment the #id is outside of the classed li. Also as described when I try either of these, Firebug does not show that definition in the CSS-inheritance. So there must be something else going on. If only I had a clue as to what...
Thomas Nilsson
I had to add ul.latestnews to make Firebug display it as part of the inheritance. Isn't that strange? I suppose that means that the definitions are not picked up like I think it should. Not even .latestnews ul li seems to be working...
Thomas Nilsson
So this is what Firebug finds for my li: #ja-col1 ul li, #ja-col2 ul li { margin:0; padding-left:15px; } #ja-col2 .latestnews ul li, .latestnews #ja-col2 ul li, .latestnews ul li, ul.latestnews li.latestnews { list-style:disc outside url("../images/bullet.gif"); margin-left:15px; padding-left:15px; } ul li { line-height:180%; margin-left:30px; }So why don't your suggestions come first? On the next line it says "inherited from ul.latestnews"... "!important" works and does what I want... So unless someone have some brilliant idea, I'll have to go with that...
Thomas Nilsson
Darn, no formatting in comments...
Thomas Nilsson
See my edit to my previous answer as it should fix your problem.
Scott
+3  A: 

I agree with SWilk, avoid !important if possible (and it is possible here). Some other solutions that SWilk did not offer is:

#id ul.myclass li {

or...

#id ul li.myclass {

The key is increasing the specificity of the selector, which the above, and SWilk's solutions do. The reason your original solutions did not work is that you did not include the other tag (ul or li) nor the #id with your addition of the .myclass.

Added after your comment that showed structure:

If your html is this (as you stated in your comment):

<div id="ja-col2">
  <div>.... 
    <ul class="latestnews">
      <li class="latestnews">

And your current css is (as stated in another comment):

#ja-col1 ul li, 
  #ja-col2 ul li { 
    margin:0; padding-left:15px; 
  } 
#ja-col2 .latestnews ul li, /*does not exist*/
  .latestnews #ja-col2 ul li, /*does not exist*/
  .latestnews ul li, /*does not exist*/
  ul.latestnews li.latestnews { 
     list-style:disc outside url("../images/bullet.gif"); 
     margin-left:15px; padding-left:15px; 
  } 
ul li { line-height:180%; margin-left:30px; }

The reason you are not seeing any change is because three of your selector paths do not exist in your html structure, and the one that wins by specificity is the very first group. You need:

#ja-col2 ul.latestnews li

To override the #ja-col2 ul li.

Scott
Thank you very much! This works perfectly (so I don't have to resort to !important ;-) What also helped me out was this: http://htmldog.com/guides/cssadvanced/specificity/ according to which the specificity of the original "#ja-col2 ul li" is 102 and the one you suggested "#ja-col2 ul.latestenews li" is 112, if I understand correctly. The only thing I probably missed is the use of ".latestnews" and how that made the three selectors not exist in the DOM. Probably it doesn't do what I think...
Thomas Nilsson