【CSSのみ】コピペで簡単!シンプルなアコーディオンメニューの作り方

シンプルで使いやすく、どんなサイトにも使えそうなアコーディオンメニューを作成しました。

色を変えるなり角丸にするなり、色々変えてお使いください。

もちろんレスポンシブ対応です。

※右側の「▼」は「Font Awesome」のものを使ってます。

目次

シンプルなアコーディオンメニュー

レスポンシブにも対応していますので、ご確認ください。

完成形はこちら

See the Pen accordion by yusuke-morioka (@yusuke-morioka) on CodePen.

コードはこちら

<!-- headタグ省略 -->

<body>

    <input id="acd-check1" class="acd-check" type="checkbox">

    <label class="acd-label" for="acd-check1">メニュー1</label>
    <div class="acd-content">
        <p>hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!
        </p>
    </div>

    <input id="acd-check2" class="acd-check" type="checkbox">
    <label class="acd-label" for="acd-check2">メニュー2</label>
    <div class="acd-content">
        <p>hello.world2!</p>
    </div>

    <input id="acd-check3" class="acd-check" type="checkbox">
    <label class="acd-label" for="acd-check3">メニュー3</label>
    <div class="acd-content">
        <p>hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!hello.world!
        </p>
    </div>

    <input id="acd-check4" class="acd-check" type="checkbox">
    <label class="acd-label" for="acd-check4">メニュー4</label>
    <div class="acd-content">
        <p>hello.world2!</p>
    </div>

</body>
@charset "UTF-8";

.acd-check {
    display: none;
}
.acd-label {
    padding: 10px 15px;
    box-sizing: border-box;
    background: #eee;
    display: flex;
    justify-content: space-between;
    margin: 0 auto 5px;
    width: 60%;
}
.acd-label::after {
    display: flex;
    align-items: center;
    font-family: "Font Awesome 5 Free";
    content: "\f0d7";
    font-weight: 900;
    transition: all 0.3s;
}

.acd-content {
    height: 0;
    opacity: 0;
    transition: 0.3s;
    visibility: hidden;
    width: 60%;
    padding: 0;
    margin: 0 auto;
}
.acd-content p {
    margin: 0;
    padding: 0 20px;
    word-break: break-all;
}
.acd-check:checked + .acd-label + .acd-content {
    height: auto;
    opacity: 1;
    padding: 15px 0;
    visibility: visible;
    display: flex;
    align-items: center;
    word-break: break-all;
}

.acd-check:checked + .acd-label::after {
    transform: rotate(180deg);
}

@media screen and (max-width: 960px) {
    .acd-label {
        width: 95%;
    }
    .acd-content {
        width: 95%;
    }
    .acd-check:checked + .acd-label + .acd-content {
        padding: 10px 0;
    }
}
あわせて読みたい
【コピペOK】便利すぎ!Detailsタグでアコーディオンメニューをつくる方法 コーディングにおける「Detailsタグ」をご存知でしょうか。「Detailsタグ」を使うと、アコーディオンメニュー実装の際、大幅にコードを短くできます。 Detailタグを使え...
あわせて読みたい
コピペで簡単!CSSで作るステップフロー4パターン【縦あり】 登録画面や購入画面、などでよく見る「ステップバー」を4種類つくりました。 すべてレスポンシブ対応なので、コピペしてお好みで装飾し、ご自由にお使いください。 【パ...
あわせて読みたい
【コピペOK】LPで使える!Q&Aデザイン4パターン【CSS】 LPなどでよく見かける、Q&Aセクションの作り方です。 コピペしてお好みで色を変えるなど、ご自由にお使いください。 もちろんレスポンシブにも対応しています。 【...
目次