<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" /> <title>纯CSS实现tab选项卡切换</title> <style media="screen"> * { box-sizing: border-box; } body { margin: 0; background-color: #2DB7F5; color: #08172F; position: absolute; height: 100%; width: 100%; overflow: hidden; } input[name='toggle'] { display: none; } nav { margin-top: 20px; } nav ul { position: relative; padding: 0; margin: 0; list-style: none; font-size: 0; } nav ul { height: 40px; line-height: 40px; text-align: center; } nav ul li { display: inline-block; width: 33.33%; height: 100%; font-size: 14px; } nav ul li label { display: inline-block; width: 100%; height: 100%; border-top-left-radius: 10px; border-top-right-radius: 10px; background-color: #1F5AA3; color: #fff; } main { position: absolute; height: 200px; width: 100%; background: white; color: #1F5AA3; padding: 10px } .container { display: none; } #tab1:checked~nav label[for='tab1'] { background-color: #fff; color: #1F5AA3; } #tab1:checked~main .tab1-container { display: block; } #tab2:checked~nav label[for='tab2'] { background-color: #fff; color: #1F5AA3; } #tab2:checked~main .tab2-container { display: block; } #tab3:checked~nav label[for='tab3'] { background-color: #fff; color: #1F5AA3; } #tab3:checked~main .tab3-container { display: block; } </style> </head> <body> <input type="radio" name="toggle" id="tab1" checked/> <input type="radio" name="toggle" id="tab2" /> <input type="radio" name="toggle" id="tab3" /> <nav> <ul> <li><label for="tab1">tab1</label></li> <li><label for="tab2">tab2</label></li> <li><label for="tab3">tab3</label></li> </ul> </nav> <main> <section class="container tab1-container"> <p> 这里是第一个tab页的内容 </p> </section> <section class="container tab2-container"> <p> 这里是第二个tab页的内容 </p> </section> <section class="container tab3-container"> <p> 这里是第三个tab页的内容 </p> </section> </main> </body> </html>
一直都是用配合着js实现的,今天看到群里发了这个,还真是眼前一亮,学习了。
来自群里大神的git:https://github.com/Jiasm/qq-share/blob/master/css/tab/index.html