html - Css:检查显示应该隐藏的容器
问题描述
我想在我的页面上做简单的标签,所以我有 3 个标签和 3 个部分。问题是在第一部分我可以看到所有部分容器,在第二个 2 和最后一个。它应该是简单的一个选项卡的一个部分。
我错过了什么?
我的 html 和代码https://codepen.io/wojsza/pen/XWMOXXm:
.display__tabs {
display: flex;
flex-wrap: wrap; }
.display__tabs--tab {
display: none; }
.display__tabs--tab:checked ~ .display__tabs--label ~ .display__tabs--content {
display: block; }
.display__tabs--label {
cursor: pointer;
padding: 25px;
color: #2e2e2e; }
.display__tabs--label:hover {
color: #aeaeae;
background-color: #2e2e2e;
font-weight: bold;
text-decoration: underline; }
.display__tabs--content {
width: 100%;
padding: 20px;
order: 1;
display: none; }
和html:
<div class="display__tabs">
<input type="radio" class="display__tabs--tab" id="display-module-info" name="module" checked="checked" />
<label class="display__tabs--label" for="display-module-info">Info</label>
<div class="display__tabs--content">
<p>Module</p>
</div>
<input type="radio" class="display__tabs--tab" id="display-module-wsu" name="module" />
<label for="display-module-wsu" class="display__tabs--label">Wsu</label>
<div class="display__tabs--content">
<p>
WSU
</p>
</div>
<input type="radio" class="display__tabs--tab" id="display-module-sections" name="module" />
<label for="display-module-sections" class="display__tabs--label">Sections</label>
<div class="display__tabs--content">
<p>
SECTION
</p>
</div>
</div>
解决方案
你快到了!只需要将“~”改为“+”即可。
你想在这部分改变你的CSS:
从
.display__tabs--tab:checked ~ .display__tabs--label ~ .display__tabs--content {
display: block;
}
至
.display__tabs--tab:checked + .display__tabs--label + .display__tabs--content {
display: block;
}
这是由 ~ 的 css 选择器导致的,它选择了一般同级,而您想使用 +,因为设置为隐藏和阻塞的单选元素是相邻同级。参考https://levelup.gitconnected.com/understanding-use-of-the-and-symbols-in-css-selectors-95552eb436f5
推荐阅读
- javascript - 在提供文件之前如何等待 Python shell 执行
- python - 在多个类中自动更新变量
- visual-studio-code - 如何设置 VS Code Live Server 扩展以在 Linux 上使用 Firefox Developer?
- javascript - React:为什么我在 React 组件中的链接不像在纯 HTML 页面中那样工作?
- python - 如何从零开始按升序获取bar y轴
- python - 如何在 python 中水合推文 ID 的数据框
- python-3.x - 如何以编程方式触发 Windows、Linux 和 Mac 的鼠标和键盘事件
- javascript - 如何从通过 Fetch API 发送的后端 (localhost) 获取数据?(PHP、Fetch、React Native、POST 请求)
- flutter - 作为等值对象成员的等值对象列表
- asp.net-core - ASP.NET Core Web 应用程序中电子邮件确认链接的默认有效期是多少?