0

Using CSS only, how can i hide NAV when another element ID exists in the following element?

<nav>...</nav>
<div id="main">
    <div id="page-calendar">...</div>
</div>

I want to hide NAV when the #main element contains #page-calendar.

Thanks in advance :)

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
David G
  • 301
  • 3
  • 14

1 Answers1

0

No, you can not do. There is no clean way to select a previous element in CSS.

Is there a "previous sibling" selector?


But if you change the flow of the elements you can do. Albeit I do not recommend it because it is much cleaner and easier to do in javascript.

Use nav inside #main after #page-calendar and position it fixed or aboslute before the #main element. Then, you can select it using next-sibling combinator

<div id="main">
    <div id="page-calendar">Calendar</div>
    <nav>Nav</nav>
</div>

#main {
 position: relative;
 top: 30px;
}


nav {
 position: fixed;
 top: 20px;
}

#page-calendar + nav {
  display: none;
}
<div id="main">
    <div id="page-calendar">Calendar</div>
    <nav>Nav</nav>
</div>

mahan
  • 12,366
  • 5
  • 48
  • 83