Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override a #id ul li behaviour with a class definition

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?

like image 399
thoni56 Avatar asked Jul 13 '10 15:07

thoni56


People also ask

What does @override mean in Java?

The @Override annotation is one of a default Java annotation and it can be introduced in Java 1.5 Version. The @Override annotation indicates that the child class method is over-writing its base class method.

Can we override a protected method in Java?

Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier.

Can we override a class?

private, static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.

Can we override private variable in Java?

No, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.


4 Answers

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.

like image 88
ScottS Avatar answered Oct 19 '22 11:10

ScottS


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

Should do the trick :)

like image 23
HurnsMobile Avatar answered Oct 19 '22 09:10

HurnsMobile


Use pseudo fake :not ID

.myclass:not(#f) ul li {
    margin-left: 20px;
}

#hello .hello-in{
  color:red;
}

.hello-in:not(#f){
  color:blue;
}
<div id="hello">
  <div class="hello-in">
   Hello I am red
  </div>
</div>

you can even use :not(#♥) or any html4/5 ( depends on page type ) character

like image 36
Benn Avatar answered Oct 19 '22 09:10

Benn


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

like image 20
SWilk Avatar answered Oct 19 '22 10:10

SWilk