0

Here is an example of what I have:

<div class="node node-blog">
  <div class="field-item even">
    <img src="image.png"/>
  </div>
</div>

What I would like to do is apply a style to the img element only when inside the node-blog and field-item divs, but this does not work:

.node-blog .field-item img {float:right;}

Sure I am doing something wrong.

Thanks.

3 Answers 3

2

Provided style's fine

Unless you have other errors in your CSS file, this line seems correct. It's most probably something very stupid. Maybe some typo in your actual code if you haven't actually copy-pasted it here...

As long as your img is contained within a subtree of .field-item that is in a subtree of .node-blog this should work. You don't have to provide the whole path.

Other rule overrides yours

Unless you have a different rule on img that sets float to it. CSS style of it

  • Must be more specific to the one provided to override yours.
  • Is less specific but sets float setting as !important.
Sign up to request clarification or add additional context in comments.

Comments

1

you could try:
.node-blog .field-item img {float:right!important;}
...another rule may be more selective

2 Comments

Don't do this please. It spreads like a virus, and soon you will have all your styles set as !important. It is far better to learn about css specificity and create your style sheets such that they work without !important.
I voted this answer up because it's a really quick way to see if specificity is the root of the problem. !important is good for diagnosing the problem, and since there was nothing wrong with the code provided in the question, diagnosis (this answer) was the best place to start. But as @david said, you really aren't going to want to use !important to "solve" the problem unless you're excited about future headaches.
1

All else being equal, it should work. The problem is in a piece of code you aren't sharing with us.

7 Comments

There are actually a couple of other div's in between the field-item and node-blog div, do I have to list each one?
No. You're using descendent selectors, not child selectors.
Just tested it, and yes I had to add all of them in, it is working now.
@Shane Grant: in that case your cascade had lower priority. The !important trick should just as well work. Have you checked which style definition overrode yours? If it works with the complete path then something else has been overriding it that was more exact over your definition.
!important is something of a sledgehammer, and you can't double !important something. It is almost always better to write selectors that are specific enough to override anything you want to override.
|

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.