Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS show/hide next elements

Tags:

css

I'd like to hide the second id with value my2 via CSS:

<div class="node1">
  <div class="xxx"> blablabla </div>
</div>
<div id="my2">
  <div class="xxx"> blablalbla </div>
</div>
<div class="node2">
  <div class="xxx"> blablabla </div>
</div>
<div id="my2">
  <div class="xxx"> blablalbla </div>
</div>

Is it possible as to hide the second my2 div?

E.g.

.node2 ? #my2 {
     display:none;
}

Thank you!

like image 434
epus Avatar asked Jan 29 '16 13:01

epus


People also ask

How do you hide the next element in CSS?

Use the ~ selector in your CSS make the association. Then toggle the display between none and block . NOTE: You could use <div> instead of <a> for the buttons if you wish.

How do you hide and show elements?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").

How do I make a div visible on clicking a link using only HTML and CSS?

You can find <div> by id , look at it's style. display property and toggle it from none to block and vice versa.

How do I show display none in CSS?

The default display value for most elements is block or inline . This panel contains a <div> element, which is hidden by default ( display: none ). It is styled with CSS, and we use JavaScript to show it (change it to ( display: block ).


4 Answers

Yes possible:

Using ~ you can select all siblings with that class(id your case).

.node2 ~ #my2{ 
  display:none; 
}

You can also use + to select the immediate sibling.

.node2 + #my2{ 
   display:none; 
 }

Hope it helps!

like image 122
Sumit Sahay Avatar answered Nov 22 '22 11:11

Sumit Sahay


In order to target the #my2 element after .node2, you'll need to add a + sign between them.

.node2 + #my2 {
  display: none;
}

But as mentioned before, using multiple id's is invalid markup.

like image 22
Alexander Erlandsson Avatar answered Nov 22 '22 11:11

Alexander Erlandsson


this will hide #my2 which is right after .node2

.node2 + #my2{
  display:none;
}

NOTE: dont use same id for multiple elements

like image 20
Pepo_rasta Avatar answered Nov 22 '22 11:11

Pepo_rasta


Link each button id to each show id IE: #button1 links to #show1. Use the ~ selector in your CSS make the association. Then toggle the display between none and block.

Demo

HTML

  <a id="button1">Button 1</a>
  <a id="button2">Button 2</a>
  <a id="button3">Button 3</a>

  <div class="text">
    <div class="hidden text" id="show1">This is 1</div>
    <div class="hidden text" id="show2">This is 2</div>
    <div class="hidden text" id="show3">This is 3</div>
  </div>

CSS Use the ~ selector to reference each id

CSS

.hidden {
  display: none;
}

#button1:hover ~ .text #show1 {
    display: block;
}

#button2:hover ~ .text #show2{
    display: block;
}

#button3:hover ~ .text #show3{
    display: block
}

.text {
    position: relative;
    top: 10px;
}

NOTE: You could use <div> instead of <a> for the buttons if you wish.

like image 42
matthew Avatar answered Nov 22 '22 09:11

matthew