1

I have the following code structure:-

<li class="container"> <div class="wrap"> <div class="green">...</div> </div> </li> <li class="container"> <div class="wrap"> <div class="xyz">...</div> </div> </li> .. .. so on 

Now li has the following css :-

margin-top:35px 

I don't want to inherit the above margin for li which contains the class green but I want it for the class xyz.

I have tried using operator > in css but it doesn't work.

2
  • Can you post more CSS? You have only provided one rule which don't help much. Commented May 20, 2015 at 14:23
  • You'll need selectors level 4 to do that in pure CSS Commented May 20, 2015 at 14:26

4 Answers 4

1

If you dont want margin-top:35px to be applied (inherit from parent li) to div.green then here is the solution

CSS

.green { margin-top:-35px; } 

HTML

<li class="container"> <div class="wrap"> <div class="green">...</div> </div> </li> <li class="container"> <div class="wrap"> <div class="xyz">...</div> </div> </li> 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use :has() jQuery pseudo selector:

$('.container:has(.xyz)').css(...); 

Comments

0

With this code structure, assuming a parent container:

<ul> <li class="container"> <div class="wrap"> <div class="green">...</div> </div> </li> <li class="container"> <div class="wrap"> <div class="xyz">...</div> </div> </li> </ul> 

You could do this:

ul li:nth-child(2) { margin: 0; } 

https://developer.mozilla.org/en/docs/Web/CSS/:nth-child

But a rule to "If I have inside a div with xyz class then..." I think it is impossible in pure css.

I hope it helps.

Comments

-1

There is no parent selector in css but you can use javascript:

$('.xyz').closest('.container').css('margin-top', '35px'); 

6 Comments

I think you mean jquery (not javascript). alternatively, you can use .parent()
well, you can also do it in js without jquery, of course
@tribe84 jQuery is javascript.
No, it's not. Its a framework or library build on top of javascript to make accessing the DOM more manageable. It's like calling bootstrap css.
It's still not javascript. Javascript is a language. When you provide your answer, someone might be mistaken to think that the $ selector is part of the javascript language, which it is not. In fact, $, .closest, .css are all element of jquery which is not something that is inherently javascript.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.