Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<head>
    <title>目次</title>
</head>

<body>
    <h1>目次</h1>

    <div class="table_of_content_box">
        <p id="table_of_content_title">目次</p>
        <div id="headinglist_box"><!-- ここに目次が表示されます --></div>
</div>
    <section id="content_heading_box">
        <div style="background-color: gray; padding:10px;">
            <h2>見出し1つめ</h2>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 400px;"></div>
        </div>
        <div style="background-color:greenyellow; padding:10px;">
            <h2>見出し2つめ</h2>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 100px;"></div>
            <h3>h3だよ 1つめ</h3>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 100px;"></div>
            <h3>h3だよ 2つめ</h3>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 400px;"></div>
        </div>
        <div style="background-color: aquamarine; padding:10px;">
            <h2>見出し3つめ</h2>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 100px;"></div>
            <h3>h3だよ 3つめ</h3>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 400px;"></div>
        </div>

        <div style="background-color: aquamarine; padding:10px;">
            <h2>見出し4つめ</h2>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 100px;"></div>
            <h3>h3だよ 4つめ</h3>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
            <div style="height: 400px;"></div>
            <h2>見出し5つめ</h2>
            <h3>h3だよ 5つめ</h3>
            <p>本文本文本文本文本文本文本文本文本文本文本文本文</p>
        </div>
    </section>
</body>

</html>
              
            
!

CSS

              
                @charset "utf-8";

html {
    scroll-behavior: smooth;
}

h1{
    font-size: 40px;
    margin: 10px;
}

h2{
    font-size: 24px;
    margin: 10px;
}

h3{
    font-size: 18px;
    margin: 10px;
}

.h2link_class {
    text-decoration: none;
    margin: 10px;
    display: block;
}

.h3link_class {
    text-decoration: none;
    margin: 10px 0 10px 30px;
    display: block;
}



/* -------------練習css ------------ */

.table_of_content_box{
    border: solid red 2px;
    width: 600px;
    margin: 30px auto 50px;
    padding: 10px 20px;
    box-shadow: 5px 5px 1px 0px black;
}


#table_of_content_title {
    font-size: 15px;
    line-height: 40px;
    position: relative;
    cursor: pointer;
}


#table_of_content_title::before {
    content: "";
    width: 5px;
    height: 5px;
    border-top: solid red 2px;
    border-right: solid red 2px;
    transform: rotate(-45deg);
    position: absolute;
    top: 19px;
    right: 65px;
    transition: 0.5s;
}


#table_of_content_title.active::before {
    transform: rotate(135deg);
}


#table_of_content_title::after {
    content: "非表示";
    color: red;
    position: absolute;
    top: 3px;
    right: 0;
    transition: 1s;
}


#table_of_content_title.active::after {
    content: "表示";
}


#headinglist_box {
    padding: 10px 0;
    height: 100%;
    opacity: 1;
}

#headinglist_box.active {
    padding: 0;
    overflow: hidden;
    transition: 0.5s;
    height: 0;
    opacity: 0;
}

.table_of_content_h2,
.table_of_content_h3 {
    text-decoration: none;
    margin: 10px 0 10px 30px;
    display: block;
    color: #333333;
    position: relative;
}

.table_of_content_h2:hover,
.table_of_content_h3:hover {
    color: #ff0000;
}

.table_of_content_h2 {
    font-size: 15px;
}

.table_of_content_h2::before {
    content: "";
    width: 10px;
    height: 10px;
    background: #333333;
    border-radius: 50%;
    position: absolute;
    left: -15px;
    top: 5px;
}

.table_of_content_h3 {
    font-size: 12px;
    margin-left: 50px;
}

.table_of_content_h3::before {
    content: "";
    width: 5px;
    height: 5px;
    background: #333333;
    border-radius: 50%;
    position: absolute;
    left: -10px;
    top: 7px;
}

.table_of_content_h2:hover.table_of_content_h2::before,
.table_of_content_h3:hover.table_of_content_h3::before {
    background: #ff0000;
}
              
            
!

JS

              
                let content_ID = document.getElementById('content_heading_box');
let h2_elements = content_ID.querySelectorAll('h2,h3');
let i = 1;
for (let h2_element of h2_elements) {
    h2_element.setAttribute("id", "aaa" + i);
    i++;
}
for (let h2_element of h2_elements) {
    let h2Text = h2_element.textContent;
    let headingList_text = document.createElement('a');
    headingList_text.href = ('#' + h2_element.id);
    if (h2_element.tagName === 'H2') {
        headingList_text.setAttribute("class", "table_of_content_h2");
    }
    if (h2_element.tagName === 'H3') {
        headingList_text.setAttribute("class", "table_of_content_h3");
    }
    headingList_text.append(document.createTextNode(h2Text));
    let headingList = document.getElementById('headinglist_box');
    headingList.append(headingList_text);
}


document.getElementById('table_of_content_title').onclick = function() {
    document.getElementById('table_of_content_title').classList.toggle('active');
    document.getElementById('headinglist_box').classList.toggle('active');
}


              
            
!
999px

Console