1

I'm desperately trying to getting this thing working. I saw an awesome show / hide solution with css only on this thread: Show hide divs on click in HTML and CSS without jQuery

Now I have the following problem: If you click anywhere or on a link inside a div that is now shown, the div closes itself.

Watch here in Collapse 2: http://jsbin.com/juceja/2/edit

Does anyone know a solution? It's driving me nuts! If you have a better solution for this problem or a better code anyway, let me know.

Thanks in advance! Michael

Community
  • 1
  • 1
michael
  • 35
  • 9
  • 1
    What exactly do you want to link to? If it's an external link, you wouldn't have a problem, and if you link to somewhere else on the same page, you can't expect the same element to stay focused, so you would have to implement the same using javascript. – cdMinix Apr 13 '15 at 09:44
  • Using JavaScript would really be cleaner, they call it the checkbox *hack* for a reason. – Shikkediel Apr 13 '15 at 10:05
  • @cdMinix I'll use it to open an intern link to a lightbox. – michael Apr 13 '15 at 18:22
  • @Shikkediel What would you recommend to use? – michael Apr 13 '15 at 18:23
  • I'd take it on with some jQuery myself... seems like the perfect tool for it. Thing with the checkbox is a nice trick but CSS really is meant for layout in my idea. – Shikkediel Apr 14 '15 at 02:10

2 Answers2

4

The problem

The problem with your link is that the "open" div loses its focus when you click it. The browser resets the focus when navigating the anchor link. Since the whole solution is based on the focus being on the div, it will break if the focus goes elsewhere. Using the "tab"-key will also move the focus, causing the div to close.

Alternative solution #1

You can instead try out the first solution in the thread you linked to. There, checkboxes are instead used to let users toggle open the divs. The drawback there is that the checkboxes are independent of each other, so toggling the second div will make no change to the first divs state.

Alternative solution #2

If the last point above is important, then a slightly modified version using radio buttons might work: http://jsbin.com/noxozedofo/2/edit

In this version, we're using two radio buttons (as opposed to checkboxes, which was used in the answer you linked to). Labels wrap the clickable headers to make them target the radio buttons when clicked. When a div is clicked, the radio button will become checked, which makes it possible to use the :checked pseudo selector to style the div. Anchor links are no longer a problem since the radio buttons will keep their state.

The drawback here is that you cannot click a radio button again to "uncheck" it. The only way to do that is to click the other div.

Here's what this solution looks like in code:

<label class="collapse" for="_1">
  <h2>Collapse 1</h2>
</label>
<input id="_1" type="radio" name="test">
<div>Content 1</div>

<label class="collapse" for="_2">
  <h2>Collapse 2</h2>
</label>
<input id="_2" type="radio" name="test">
<div>
  Content 2...
  <a href="#link">link</a>
</div>


.collapse{
  display:block;
}
.collapse + input{
  display:none;
}
.collapse + input + *{
  display:none;
}
.collapse+ input:checked + *{
  display:block;
}
Johan Persson
  • 1,425
  • 10
  • 11
  • Thanks a lot for your very good answer! I wish I'd be able to choose your answer as the best, too. It seemd that @bunion's answer below solved my specific problem. Not because your code was bad, but because I tried implementing it in a wordpress theme, I didn't program myself. Strangely, your solutions resulted in displaying just the radio buttons. I solved it (to be honest a little bit badly) with the :hover-solution. Thanks a lot for your help! – michael Apr 13 '15 at 11:39
  • @michael - are you sure that solves the problem? If I update your jsbin with the suggested extra css in the other answer, I still have the same problem with the div closing. The div continues to stay open until I remove the mouse from the div, then it abruptly closes. Here's the bin: http://jsbin.com/turifujeku/2/edit – Johan Persson Apr 13 '15 at 12:37
  • Well it's a start. As I'm implementing it into another theme, it made some strange things and didn't work. I would love to take your code (it's better, cleaner), but I can't get it working. Maybe I could send you the link to the website (it's not public yet, so I would rather not post it here). Would you have a look over it? – michael Apr 13 '15 at 18:21
  • Send me the link and I'll see if I have time to take a look. – Johan Persson Apr 14 '15 at 12:25
1

have you tried experimenting with

.collapse > * + *:hover {
  display: block !important;
}
Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
plarner7
  • 150
  • 6