CSS Only accordion - How to collapse content











up vote
0
down vote

favorite
1












I'm following this CodePen to create a pure css accordion. I like the case on the right of the screen. It opens and closes tab content using an input of type radio. I'd like to modify it so that when you click on a tab that's open, the content can collapse. Currently, you can only collapse it by opening another tab, a functionality which I'd like to keep. But I also wanna be able to close it by clicking directly on it. Only one tab should be open at once, or none at all.



I tried adding a default max-height of 0 to the tab content like this, but that didn't really do much, and I believe it's redundant:



.tab input ~ .tab-content {
max-height: 0;
}

/* :checked */
.tab input:checked ~ .tab-content {
max-height: 100vh;
}


Any suggestions?



Edit: I've realized that the reason I can't collapse it is because the default behavior of radio buttons doesn't allow me to 'uncheck' them just by clicking on them. But the reason this accordion works in the way I want (close the other tabs when one opens) is because it's using radio inputs instead of checkbox inputs. So my solution will have to keep the best of both worlds, perhaps by using javascript to uncheck the radio buttons when clicked. However, I was trying to keep this as a pure CSS accordion. Still at a loss...










share|improve this question
























  • I think your Edit and realization is right on the mark. You'll need just a tiny bit of javascript so you can "uncheck" a radio button when it's already checked.
    – Stephen P
    Nov 9 at 23:59















up vote
0
down vote

favorite
1












I'm following this CodePen to create a pure css accordion. I like the case on the right of the screen. It opens and closes tab content using an input of type radio. I'd like to modify it so that when you click on a tab that's open, the content can collapse. Currently, you can only collapse it by opening another tab, a functionality which I'd like to keep. But I also wanna be able to close it by clicking directly on it. Only one tab should be open at once, or none at all.



I tried adding a default max-height of 0 to the tab content like this, but that didn't really do much, and I believe it's redundant:



.tab input ~ .tab-content {
max-height: 0;
}

/* :checked */
.tab input:checked ~ .tab-content {
max-height: 100vh;
}


Any suggestions?



Edit: I've realized that the reason I can't collapse it is because the default behavior of radio buttons doesn't allow me to 'uncheck' them just by clicking on them. But the reason this accordion works in the way I want (close the other tabs when one opens) is because it's using radio inputs instead of checkbox inputs. So my solution will have to keep the best of both worlds, perhaps by using javascript to uncheck the radio buttons when clicked. However, I was trying to keep this as a pure CSS accordion. Still at a loss...










share|improve this question
























  • I think your Edit and realization is right on the mark. You'll need just a tiny bit of javascript so you can "uncheck" a radio button when it's already checked.
    – Stephen P
    Nov 9 at 23:59













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I'm following this CodePen to create a pure css accordion. I like the case on the right of the screen. It opens and closes tab content using an input of type radio. I'd like to modify it so that when you click on a tab that's open, the content can collapse. Currently, you can only collapse it by opening another tab, a functionality which I'd like to keep. But I also wanna be able to close it by clicking directly on it. Only one tab should be open at once, or none at all.



I tried adding a default max-height of 0 to the tab content like this, but that didn't really do much, and I believe it's redundant:



.tab input ~ .tab-content {
max-height: 0;
}

/* :checked */
.tab input:checked ~ .tab-content {
max-height: 100vh;
}


Any suggestions?



Edit: I've realized that the reason I can't collapse it is because the default behavior of radio buttons doesn't allow me to 'uncheck' them just by clicking on them. But the reason this accordion works in the way I want (close the other tabs when one opens) is because it's using radio inputs instead of checkbox inputs. So my solution will have to keep the best of both worlds, perhaps by using javascript to uncheck the radio buttons when clicked. However, I was trying to keep this as a pure CSS accordion. Still at a loss...










share|improve this question















I'm following this CodePen to create a pure css accordion. I like the case on the right of the screen. It opens and closes tab content using an input of type radio. I'd like to modify it so that when you click on a tab that's open, the content can collapse. Currently, you can only collapse it by opening another tab, a functionality which I'd like to keep. But I also wanna be able to close it by clicking directly on it. Only one tab should be open at once, or none at all.



I tried adding a default max-height of 0 to the tab content like this, but that didn't really do much, and I believe it's redundant:



.tab input ~ .tab-content {
max-height: 0;
}

/* :checked */
.tab input:checked ~ .tab-content {
max-height: 100vh;
}


Any suggestions?



Edit: I've realized that the reason I can't collapse it is because the default behavior of radio buttons doesn't allow me to 'uncheck' them just by clicking on them. But the reason this accordion works in the way I want (close the other tabs when one opens) is because it's using radio inputs instead of checkbox inputs. So my solution will have to keep the best of both worlds, perhaps by using javascript to uncheck the radio buttons when clicked. However, I was trying to keep this as a pure CSS accordion. Still at a loss...







html css css3 css-transitions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 23:28

























asked Nov 9 at 23:05









user2030942

10011




10011












  • I think your Edit and realization is right on the mark. You'll need just a tiny bit of javascript so you can "uncheck" a radio button when it's already checked.
    – Stephen P
    Nov 9 at 23:59


















  • I think your Edit and realization is right on the mark. You'll need just a tiny bit of javascript so you can "uncheck" a radio button when it's already checked.
    – Stephen P
    Nov 9 at 23:59
















I think your Edit and realization is right on the mark. You'll need just a tiny bit of javascript so you can "uncheck" a radio button when it's already checked.
– Stephen P
Nov 9 at 23:59




I think your Edit and realization is right on the mark. You'll need just a tiny bit of javascript so you can "uncheck" a radio button when it's already checked.
– Stephen P
Nov 9 at 23:59












1 Answer
1






active

oldest

votes

















up vote
0
down vote













This is not possible with pure CSS. You will need javascript to change the checked property on the radio button to false if it is already checked when clicking on it.



Javascript example:



var radios = document.querySelectorAll('input[type="radio"]');

for(i=0; i<radios.length; i++ ) {
radios[i].onmousedown = function() {
let checked = this.checked;

this.onclick = function() {
if (checked) {
this.checked = false;
}
}
}
}


jQuery example:



$('input[type="radio"]').on('mousedown', function() {
let checked = this.checked;

$(this).on('click', function() {
if (checked) {
this.checked = false;
}
})
})


How radio buttons work is they change only onclick and not onmouseup, and by the time onclick is fired the checked property will already be set to true. So if you only have a listener for onclick, checked will always be true and you won't be able to activate the radio at all. Therefore, the mousedown listener is necessary before onclick to see if the radio is already checked so you can properly uncheck it when the click event is fired.



It's much easier to understand when you see, so here's a fiddle showing what I mean.



http://jsfiddle.net/d6uLqy0c/4/






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234382%2fcss-only-accordion-how-to-collapse-content%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This is not possible with pure CSS. You will need javascript to change the checked property on the radio button to false if it is already checked when clicking on it.



    Javascript example:



    var radios = document.querySelectorAll('input[type="radio"]');

    for(i=0; i<radios.length; i++ ) {
    radios[i].onmousedown = function() {
    let checked = this.checked;

    this.onclick = function() {
    if (checked) {
    this.checked = false;
    }
    }
    }
    }


    jQuery example:



    $('input[type="radio"]').on('mousedown', function() {
    let checked = this.checked;

    $(this).on('click', function() {
    if (checked) {
    this.checked = false;
    }
    })
    })


    How radio buttons work is they change only onclick and not onmouseup, and by the time onclick is fired the checked property will already be set to true. So if you only have a listener for onclick, checked will always be true and you won't be able to activate the radio at all. Therefore, the mousedown listener is necessary before onclick to see if the radio is already checked so you can properly uncheck it when the click event is fired.



    It's much easier to understand when you see, so here's a fiddle showing what I mean.



    http://jsfiddle.net/d6uLqy0c/4/






    share|improve this answer

























      up vote
      0
      down vote













      This is not possible with pure CSS. You will need javascript to change the checked property on the radio button to false if it is already checked when clicking on it.



      Javascript example:



      var radios = document.querySelectorAll('input[type="radio"]');

      for(i=0; i<radios.length; i++ ) {
      radios[i].onmousedown = function() {
      let checked = this.checked;

      this.onclick = function() {
      if (checked) {
      this.checked = false;
      }
      }
      }
      }


      jQuery example:



      $('input[type="radio"]').on('mousedown', function() {
      let checked = this.checked;

      $(this).on('click', function() {
      if (checked) {
      this.checked = false;
      }
      })
      })


      How radio buttons work is they change only onclick and not onmouseup, and by the time onclick is fired the checked property will already be set to true. So if you only have a listener for onclick, checked will always be true and you won't be able to activate the radio at all. Therefore, the mousedown listener is necessary before onclick to see if the radio is already checked so you can properly uncheck it when the click event is fired.



      It's much easier to understand when you see, so here's a fiddle showing what I mean.



      http://jsfiddle.net/d6uLqy0c/4/






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is not possible with pure CSS. You will need javascript to change the checked property on the radio button to false if it is already checked when clicking on it.



        Javascript example:



        var radios = document.querySelectorAll('input[type="radio"]');

        for(i=0; i<radios.length; i++ ) {
        radios[i].onmousedown = function() {
        let checked = this.checked;

        this.onclick = function() {
        if (checked) {
        this.checked = false;
        }
        }
        }
        }


        jQuery example:



        $('input[type="radio"]').on('mousedown', function() {
        let checked = this.checked;

        $(this).on('click', function() {
        if (checked) {
        this.checked = false;
        }
        })
        })


        How radio buttons work is they change only onclick and not onmouseup, and by the time onclick is fired the checked property will already be set to true. So if you only have a listener for onclick, checked will always be true and you won't be able to activate the radio at all. Therefore, the mousedown listener is necessary before onclick to see if the radio is already checked so you can properly uncheck it when the click event is fired.



        It's much easier to understand when you see, so here's a fiddle showing what I mean.



        http://jsfiddle.net/d6uLqy0c/4/






        share|improve this answer












        This is not possible with pure CSS. You will need javascript to change the checked property on the radio button to false if it is already checked when clicking on it.



        Javascript example:



        var radios = document.querySelectorAll('input[type="radio"]');

        for(i=0; i<radios.length; i++ ) {
        radios[i].onmousedown = function() {
        let checked = this.checked;

        this.onclick = function() {
        if (checked) {
        this.checked = false;
        }
        }
        }
        }


        jQuery example:



        $('input[type="radio"]').on('mousedown', function() {
        let checked = this.checked;

        $(this).on('click', function() {
        if (checked) {
        this.checked = false;
        }
        })
        })


        How radio buttons work is they change only onclick and not onmouseup, and by the time onclick is fired the checked property will already be set to true. So if you only have a listener for onclick, checked will always be true and you won't be able to activate the radio at all. Therefore, the mousedown listener is necessary before onclick to see if the radio is already checked so you can properly uncheck it when the click event is fired.



        It's much easier to understand when you see, so here's a fiddle showing what I mean.



        http://jsfiddle.net/d6uLqy0c/4/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 0:22









        putipong

        1809




        1809






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234382%2fcss-only-accordion-how-to-collapse-content%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini