Skip to content
Advertisement

Hide element via css when another element is present on the page

I want to hide an element if one element is already present on the page.

If element with the id:

JavaScript

is present on the page, then hide following element:

JavaScript

So I’ve found a solution how to do this via js. but I need to this via .css, if possible

JavaScript

Advertisement

Answer

Yes you can kind of do this. Here’s an example, but it is not full proof though.

This only works if .tohide follows directly after .item or if .tohide follows .item as a sibling.

JavaScript
JavaScript

So in this example, if .item exists on the page, then .tohide is hidden. Go ahead and remove <div class="item">item</div> and run it and you will see that .tohide appears now. You can play with it in this jsfiddle: https://jsfiddle.net/qzu7hkcg/1/

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement