Cara Membuat Table of Content Otomatis (TOC) Dalam Postingan Blog (Seo Friendly + Hirarki)

Kak Her

<div id='postMiddle'>
<div class='tableOfContent'>
<input aria-label='Daftar Isi' class='toctoggle' id='toctoggle' role='button' type='checkbox'/>
<span class='toctogglespan'><label class='toctogglelabel' for='toctoggle'/></span>
<div id='tocContent'></div>
</div>
<data:post.body/>
<script>tockeren();</script>
</div>

Nah, selanjutnya tinggal tambahkan kode CSS berikut ini untuk memperindah tampilan Daftar isinya.

/* TOC Otomatis by idnxmus.com */
.post-body .tableOfContent {
padding:12px 15px;
margin:20px 0;
background-color:#f8f9fa;
border:1px solid #e4e9ef;
border-radius:4px;
font-size:16px;
line-height:1.5em;
}
.post-body .tableOfContent #tocContent {
margin:5px 0;
}
.post-body .tableOfContent #tocContent ol, .post-body .tableOfContent #tocContent ul {
margin:0 0 10px;
padding:10px 0 0 17px;
position:relative;
}
.post-body .tableOfContent #tocContent ol ol, .post-body .tableOfContent #tocContent ul ul, .post-body .tableOfContent #tocContent ol ul, .post-body .tableOfContent #tocContent ul ol {
padding-left:30px;
}
.post-body .tableOfContent #tocContent li {
position:relative;
margin:0 0 10px;
}
.post-body .tableOfContent #tocContent ol ul:before, .post-body .tableOfContent ol ol:before, .post-body .tableOfContent #tocContent ul ol:before, .post-body .tableOfContent ul ul:before {
content:'';
display:block;
width:1px;
height:calc(100% - 10px);
position:absolute;
left:-11px;
border-left:1px dashed rgba(0,0,0,.2);
top:0;
}
.post-body ol.point2 li:before {
content:'';
display:block;
height:1px;
width:15px;
border-top:1px dashed rgba(0,0,0,.2);
position:absolute;
left:-38px;
top:10px;
}
.post-body .tableOfContent #tocContent a {
display:inline-table;
margin-left:5px;
}
.post-body .tableOfContent #tocContent .point2 a {
margin:0;
}
.post-body .tableOfContent #tocContent a:hover {
text-decoration:underline;
}
.post-body .tableOfContent .toctogglelabel {
display:block;
cursor:pointer;
}
.post-body .tableOfContent .toctogglespan i.icon.icon-dropdown {
float:right;
margin:0;
}
ol.point2 li {
list-style-type:circle;
position:relative;
}
:not(:checked) > .toctoggle {
display:inline !important;
position:absolute;
opacity:0;
}
.tableOfContent .toctogglespan .toctogglelabel:before {
content:'Daftar Isi :';
font-weight:600;
}
.toctoggle:checked ~ #tocContent {
display:none;
}
label.toctogglelabel:after {
background-image:url("data:image/svg+xml,<svg viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'><path d='M256 294.1L383 167c9.4-9.4 24.6-9.4 33.9 0s9.3 24.6 0 34L273 345c-9.1 9.1-23.7 9.3-33.1.7L95 201.1c-4.7-4.7-7-10.9-7-17s2.3-12.3 7-17c9.4-9.4 24.6-9.4 33.9 0l127.1 127z' fill='%2309204C'/></svg>");
content:'';
margin-top:3px;
width:16px;
height:16px;
background-size:cover;
background-repeat:no-repeat;
background-position:center center;
float:right;
transition:all .3s ease;
-webkit-transition:all .3s ease;
}
.toctoggle:checked + .toctogglespan .toctogglelabel:after {
-webkit-transform:rotate(180deg);
transform:rotate(180deg);
}
.post-body ol.point2 {
padding-top:10px;
}
:target::before {
content:"";
display:block;
height:60px;
margin-top:-60px;
visibility:hidden;
}
@media screen and (max-width:480px) {
.post-body .tableOfContent {
font-size:15px;
}
}
Informasi :
Jika menggunakan header melayang, atur tinggi header pada code css :target::before { yang sudah saya tandai. Jika ti
dak, silahkan hapus code tersebut.

Jangan lupa klik tombol Simpan Tema untuk melihat hasilnya.

2. TOC Semi-Otomatis (Recomended)

Table Of Content yang satu ini sangat saya rekomendasikan, kenapa? karena, TOC ini bisa ditampilkan di dalam artikel sesuai keinginan anda.

Artikel Terkait

Bagikan:

Tinggalkan komentar