利用radio实现纯css选项卡切换

时间:2021-06-29 02:42:44

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtIAAABCCAIAAAD18aaXAAAG70lEQVR4nO3dO5KrPBCGYRY0VV4OEXuZlH04+iNWMdGE3sJJvQX/ARe10MUYPELtfp/onLG5VIlSf0gCNw8AAIAimrNPAAAAWEHsAAAAhRA7AABAIcQOAABQCLEDAAAUQuwAAACFEDsAAEAhb4gdvwAAk45XEFjznthxfCdQ6vf39w7DuAAso/PHDsQOHELVMY4LwDI6f+xA7MAhVB3juAAso/PHDsQOHELVMY4LwDI6f+xA7MAhVB3juAAso/PHDuZix9Bd+pv7762/NN0w/cP7JOrWX5qU+NZDt2G3immrOtf26/vH/ffn+6tpr9M/vE+ifr6/ku0f3/rabtitZrouAJr/vXR1/qiEudjxGLpmDBqPh0gd8dgxdH7Xsmy3Naa8EjvcuWiiq+rc7/dr24yV5n4XZSded66t3/7Ldlvr1Ja649UycQQllF0A1TW/PIq+hKKs80cd7MUOGTwygxciK0TzwNY4sU4u4e7l94gdBbjKk7l7FRXAFSd/J5uKxLp0rXb/8/0ljnVt9SUPdRdATc0/nsK0d/9a0EFd548aWIwdS46Q0SEzeiFjRzKopDLIlnjiogmxo4S5kMjakbl9lXUnWalS5SJfn4KjXlttlUffBVBP86+be+sYSj30df6ogMnYMfESwQuxI8gGuQmXp7Fj6MbQcusvxI6ivJLwQt0Jbn1z1eLFyX2VhefsU9inwuZXGTrP7oGgj63YsQwrhOX99djhz9UcGe1Y9kzs+FvLkHc4kfF63fEH699Td+SYuxKKLoDKm19h4xM7sIet2PF4PJbkkFh0IcRGOPKxI/egS2CVRogdhUylIzHrLsRucfN1J/ekQyBSjljb8feqbP75bHQNddyJHdjFbuxYuPGIxMhEOnZM3976VMvT0yJ2FLAeKHc3pIlb03Tdmb79npmRsWgpCx1qY8eiluaf96TrCtDW+aMKxI5okAhWm+6OHUPXXLpuw6OxxI5CVnUnWkmC5Ya76861bb7aNrIiIDwpZRV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8EjfSaztSm0SXdrC2AwCAbc4e7Vgq9Tx7EQkEsqInHxfxy/w8QRLJKmLfySdZkpukT5knWQAAeObcJaVy/calv8nCnXwFR/q9HXKTbpCZIv2qjeR7O1KbZE45+0oR9wViBwDAqiqeZAEAABYQOwAAQCHEDgAAUAixAwAAFHJm7AAAANacEzsAAAC2IHYAAIBCiB0AAKAQYgcAACiE2AEAAAohdgAAgEKIHQAAoBBiBwAAKITYAQAACiF2AACAQogdAACgkP8BTARl0YPVnRUAAAAASUVORK5CYII=" alt="" />

代码文件如下:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>radio选项卡切换</title>
<style type="text/css">
*{margin:0;padding:0;}
.test-box{width: 50%;margin: 10px auto 0;padding-top: 25px;position: relative;}
.test-box>.test-tab{width:100%;}
.test-box .test-label{background-color: #eee;display: inline-block;width: 25%;text-align: center;font-size: 16px;line-height: 24px;box-sizing: border-box;}
.test-label{position: absolute;top: 0;border:solid #ccc; border-width:1px 0px 1px 1px;}
.test-tab:nth-child(1) .test-label{left: 0;}
.test-tab:nth-child(2) .test-label{left: 25%;}
.test-tab:nth-child(3) .test-label{left: 50%;}
.test-tab:last-child .test-label{border-right-width:1px;}
.test-box .content{background-color: #fff;height: 20px;display: none;border:1px solid #ccc;}
.test-box input.test-radio{display: none;}
.test-box input:checked~.test-label{background-color: #fff;border-bottom: 1px solid #fff;}
.test-box input:checked~.content{display: block;}
</style>
</head>
<body>
<div class="test-box">
<div class="test-tab">
<input type="radio" class="test-radio" id="radio1" name="test-radio" checked>
<label for="radio1" class="test-label">选项卡1</label>
<div class="content"><P>asdasdsad1</P></div>
</div>
<div class="test-tab">
<input type="radio" class="test-radio" id="radio2" name="test-radio">
<label for="radio2" class="test-label">选项卡2</label>
<div class="content"><P>asdasdsad2</P></div>
</div>
<div class="test-tab">
<input type="radio" class="test-radio" id="radio3" name="test-radio">
<label for="radio3" class="test-label">选项卡3</label>
<div class="content"><P>asdasdsad3</P></div>
</div>
</div>
</body>
</html>