0

The Code:

#menu-open:checked ~ .logoib {
  -webkit-filter: invert(100%);
  filter: invert(100%);
}
<div id="logo" class="logoib"></div>

<input type="checkbox" id="menu-open">

<nav class="menu-list">
  <a href="#" class="firsta">Home</a>
  <a href="#">My Works</a>
  <a href="#">Contact</a>
</nav>

<label for="menu-open" class="modmenu logoib">
  <span></span>
</label>

I want to add some CSS of #logo when the checkbox of menu is checked.

2
  • In this context you can't. You have to either settle for using javascript or place the input earlier in the DOM (before the #logo element) Commented Apr 5, 2016 at 21:32
  • yes, i was thinking... I will do this.. thank you anyway! Commented Apr 5, 2016 at 21:34

2 Answers 2

4

css cant affect anything before it, it can only affect stuff after the targeted element change like below

#menu-open + #logo {
  color: #ccc;
  font-style: italic;
} 
#menu-open:checked + #logo {
  color: #f00;
  font-style: normal;
} 
<input type="checkbox" id="menu-open">
<div id="logo" class="logoib">
  my logo here
</div>

<nav class="menu-list">
  <a href="#" class="firsta">Home</a>
  <a href="#">My Works</a>
  <a href="#">Contact</a>
</nav>

<label for="menu-open" class="modmenu logoib">
  <span></span>
</label>

Sign up to request clarification or add additional context in comments.

1 Comment

I tried to move up the input code, it works, but then css of menu-list doesn't
1

You could use pure JS, JQuery or CSS.

Here is a pure JS solution. If you want to set it also on load, you could call myFunction when the DOM is ready.

function myFunction() {
  var cb = document.getElementById('menu-open');
  var logo = document.getElementById('logo');

  if (cb.checked) {
    logo.setAttribute("class", "newClass");
  } else {
    logo.removeAttribute("class");
  }
}
#menu-open:checked~.logoib {
  -webkit-filter: invert(100%);
  filter: invert(100%);
}
.newClass {
  color: red;
}
<div id="logo" class="logoib">Logo

</div>
<input type="checkbox" id="menu-open" onchange="myFunction()">

<nav class="menu-list">
  <a href="#" class="firsta">Home</a>
  <a href="#">My Works</a>
  <a href="#">Contact</a>
</nav>

<label for="menu-open" class="modmenu logoib">
  <span></span>
</label>

2 Comments

Thank you LuLylulu, it works AWESOME :d thank you all
glad I could help :)

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.