2

I am using a jquery accordion as following:

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript">
    $(document).ready(function () { 
     $("#accordion").accordion();
    }
</script>

<style>
.ui-accordion-content{
        margin:0;
        padding:0;
}
</style>

JQuery creates a div.ui-accordion-content and applies the styles from jquery-ui.css to it. Unfortunately my own css from the style tags is not applied.

Why?

9
  • Do you need the jquery-ui.css? Maybe just remove it? I never use that when using the standard jquery accordion. Commented Sep 5, 2012 at 13:50
  • Using !important in your CSS usually means you're narcissistic & selfish or lazy. Respect the devs to come... - css-tricks.com/when-using-important-is-the-right-choice Commented Sep 5, 2012 at 13:53
  • @anonymousdownvotingislame Here is another point of view: webcredible.co.uk/user-friendly-resources/web-dev/…. Commented Sep 5, 2012 at 13:59
  • @VisioN sorry! WE CAN'T FIND THE PAGE YOU'RE AFTER... Commented Sep 5, 2012 at 14:00
  • @anonymousdownvotingislame Strange. Here is the shorten link: bit.ly/74aQU0. It should work. Commented Sep 5, 2012 at 14:02

3 Answers 3

2

The correct approach

You need to understand CSS precendence, read up on it http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/.

Using this your code would be updated to the following:

.ui-accordion .ui-accordion-content{
    margin: 0;
    padding: 0;
}

Since it is defined exactly the same as in the linked jquery ui css and after it in the DOM it will therefor have precedence.

The quick and dirty fix

Fix by adding !important to your own style like this:

.ui-accordion-content{
    margin:0 !important;
    padding:0 !important;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thats why I added the link to precedence and inheritance first. Most people here though are lazy and will always take the easy fix instead of actually learning the WHY. :/
Agreed, so why not demonstrate the best way and not only the easy fix? +1 if you do.
In hindsight I agree, I was not answering it properly. My answer has been updated accordingly.
1

One option is to use !important:

.ui-accordion-content {
    margin: 0 !important;
    padding: 0 !important;
}

3 Comments

Using !important in your CSS usually means you're narcissistic & selfish or lazy. Respect the devs to come... css-tricks.com/when-using-important-is-the-right-choice
@anonymousdownvotingislame Well, I don't really agree. Sometimes it is useful, especially when you need a fast result. By all means this is one option.
@anonymousdownvotingislame. HA?! !important means it's important not you're narcissistic & selfish or lazy. Funny isn't it? Why did the css designers choose that name for this attribute???
0

Download the jquery-ui.css, change the relevant style rules inside it and use that one instead of the public version.

OR

Try using !important after your css rules, like:

margin:0!important;

1 Comment

Using !important in your CSS usually means you're narcissistic & selfish or lazy. Respect the devs to come... css-tricks.com/when-using-important-is-the-right-choice

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.