add a rule to an element only if it has a certain child

不羁岁月 提交于 2019-11-29 14:46:21

If it is not possible with CSS, then it is not possible with LESS, which compiles to plain old vanilla CSS.

The problem is, that the LESS is compiled without knowledge of the HTML it is applied to, and without any client side scripting. Therefore, it is impossible to achieve anything with LESS that can not be achieved by using CSS alone.

You could achieve what you want by using client side javascript, or you could wait until CSS4 comes out, and use the parent selectors to the same effect as what you want.

This isn't possible in CSS or LESS.

I would suggest your best bet is jQuery.

If your parent element is .parent and your child is .child, you could do:

$('.parent:has(.child)').addClass('myClass');

See Example: http://jsfiddle.net/d8Lz5/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!