
本文详解为何使用 float 实现的选项卡会破坏页面流布局,导致后续区块被覆盖,并提供完整、可复用的修复方案——通过清除浮动(clear: both)和结构优化,确保选项卡容器具备独立文档流边界。
本文详解为何使用 `float` 实现的选项卡会破坏页面流布局,导致后续区块被覆盖,并提供完整、可复用的修复方案——通过清除浮动(`clear: both`)和结构优化,确保选项卡容器具备独立文档流边界。
你遇到的“新添加的 TESTTEST1212 文字区域被选项卡内容覆盖”问题,并非代码逻辑错误,而是 CSS 浮动(float)引发的经典文档流塌陷。当前 .tab 和 .tabcontent 均使用 float: left,导致它们脱离标准文档流,父容器高度无法自动包裹子元素,后续
✅ 正确解法:为选项卡容器添加清除浮动 + 结构封装
只需在选项卡 HTML 结构外部包裹一个带清除样式的容器,并微调 CSS,即可彻底解决:
<!-- ✅ 推荐:用 .tabs-wrapper 封装整个选项卡区域 -->
<div class="tabs-wrapper">
<center>
<h2><font size="6">Diamond Shapes</font></h2>
<p>Explore the enchanting world of diamond cutting and diamond shapes. Click to learn more about each of the diamond shapes and the unique features they have to offer.</p>
</center>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Round')" id="defaultOpen">Round</button>
<button class="tablinks" onclick="openCity(event, 'Princess')">Princess</button>
<button class="tablinks" onclick="openCity(event, 'Oval')">Oval</button>
<button class="tablinks" onclick="openCity(event, 'Test')">Test Test 1212</button>
</div>
<div id="Round" class="tabcontent">...</div>
<div id="Princess" class="tabcontent">...</div>
<div id="Oval" class="tabcontent">...</div>
<div id="Test" class="tabcontent">
<center><h3>Test</h3></center>
<p>Test Test 1212</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill6712" title="Wechat HTML Publisher"><img
src="https://img.php.cn/upload/skill/000/000/081/179109368394970.jpg" alt="Wechat HTML Publisher" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill6712" title="Wechat HTML Publisher" class="overflowclass">Wechat HTML Publisher</a>
<p class="overflowclass">直接上传HTML富文本到微信公众号草稿箱。支持完整的HTML格式,无需Markdown转换。</p>
</div>
<a rel="nofollow" href="/xiazai/skill6712" title="Wechat HTML Publisher" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
</div>
</div>
<!-- ✅ 后续新增区块将正常换行显示 -->
<section><h3>Our Diamond Collection</h3>
<p>This section now appears cleanly below the tabs — no overlap!</p>
</section>
对应新增 CSS(放入
.tabs-wrapper {
overflow: hidden; /* 清除内部浮动,推荐写法 */
margin-bottom: 2rem; /* 可选:增加底部间距 */
}
/* 其余原有样式保持不变,但建议移除固定 height(如 height: 800px) */
.tab {
float: left;
border: 1px solid #ccc;
background-color: #fff; /* 修正:#white 无效,应为 #fff 或 white */
width: 30%;
/* height: 800px; ← 删除固定高度,让内容自适应 */
}
.tabcontent {
float: left;
padding: 12px;
border: 1px solid #ecedee;
width: 70%;
border-left: none;
/* height: 800px; ← 同样删除,避免内容截断 */
}
⚠️ 关键注意事项:
- ❌ 不要将新内容(如 TESTTEST1212)直接写在 .tabcontent 外部却未包裹在 .tabs-wrapper 中;
- ❌ 避免对 .tab / .tabcontent 设置固定 height,否则长文本会被裁剪;
- ✅ 使用 overflow: hidden 是现代清除浮动最简洁可靠的方式(比
更语义化);- ✅ 将 <script> 移至 </script>
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!










