2022-09-15

Radio Button Method - HTML Accordion

I would like some assistance with my accordion code,

My idea is to get something like this:

The Radio Button Method adds a hidden radio input and a label tag to each accordion tab.

The logic is straightforward:

  • when a user selects a tab, they essentially check the radio button associated with that tab.
  • when a user clicks the next tab in the accordion, the next radio button is selected, and so on. Only one tab can be open at a time using this method.

I'd like some advice on how to incorporate this into my current accordion code.

<!doctype html>

<html>
<head>
    <style>
        input {
            display: none;
        }

        label {
            display: block;    
            padding: 8px 22px;
            margin: 0 0 1px 0;
            cursor: pointer;
            background: #6AAB95;
            border-radius: 3px;
            color: #FFF;
            transition: ease .5s;
            position: relative; /* ADDING THIS IS REQUIRED */
        }

        label:hover {
            background: #4E8774;
        }

        label::after {
            content: '+';
            font-size: 22px;
            font-weight: bold;
            position: absolute;
            right: 10px;
            top: 2px;
        }

        input:checked + label::after {
            content: '-';
            right: 14px;
            top: 3px;
        }

        .content {
            background: #E2E5F6;
            padding: 10px 25px;
            border: 1px solid #A7A7A7;
            margin: 0 0 1px 0;
            border-radius: 3px;
        }

        input + label + .collapse {
            display: none;
        }

        input:checked + label + .collapse {
            display: block;
        }
    </style>
</head>

<body>
    <input type="checkbox" id="title1" />
    <label for="title1">Accordion 1</label>

    <div class="collapse">
        <p>Your content goes here inside this division with the class "content".</p>
    </div>

    <input type="checkbox" id="title2" />
    <label for="title2">Accordion 2</label>

    <div class="collapse">
        <p>Your content goes here inside this division with the class "content".</p>
    </div>
</body>
</html>


No comments:

Post a Comment