html - 如何使用 CSS 更改侧边栏中所选选项的颜色?
问题描述
我无法弄清楚如何更改边栏中所选选项的颜色。例如,我有 4 个选项,我单击第一个选项,我希望侧边栏中的第一个选项始终具有白色背景色。我现在只有一个悬停选项,它会改变背景颜色。我可以只使用 CSS HTML 吗?根据我的研究,我在 JavaScript 和 PHP 中看到了一些,但想知道我是否可以只使用 CSS HTML 来做到这一点。以下是我当前的代码:
.flexContainer {
display: flex;
}
.flexContainer > div {
margin: 10px;
padding: 20px;
font-size: 20px;
text-align: center;
}
.sidebarContainer {
flex-shrink: 1;
}
.sidebar {
width: 200px;
background-color: #dd1d5e;
overflow: auto;
border-radius: 30px;
}
.sidebar a {
display: block;
color: white;
padding: 16px;
text-decoration: none;
}
.sidebar a:hover:not(.active) {
background-color: #ffffff;
color: #dd1d5e;
}
<div class="flexContainer">
<div class="sidebarContainer">
<div class="sidebar">
<a href="category.php">Holiday</a>
<a href="category.php">Vegetarian</a>
<a href="category.php">Dessert</a>
<a href="category.php">Cultural Cuisine</a>
</div>
</div>
</div>
解决方案
如果你想让它在没有 PHP 的独立 HTML 中工作。首先,您应该有 4 个 HTML 文件,每个文件应该有不同的名称,例如 cat-holiday.htm、cat-vegetarian.htm、cat-dessert.htm 和 cat-culture.htm。
之后,每个 HTML 文件都有一个 ONE,<a>
其中包含一个特殊的 CSS 类名,例如:.selected
. cat-holiday.htm 样本
<div class="sidebar">
<a href="category.php" class="selected">Holiday</a>
<a href="category.php">Vegetarian</a>
<a href="category.php">Dessert</a>
<a href="category.php">Cultural Cuisine</a>
</div>
最后,您可以在 CSS 文件中添加相应的代码。
.sidebar a.selected {
background-color: #ff0000;
color: #00ffff;
}
推荐阅读
- codenameone - Codenameone - 表格上的透明度或样式问题
- c++ - C ++中文本文件中的重复单词
- java - Java 如何显示然后隐藏 (GONE) 视图-Android
- javascript - 在过滤表数据中选择多个值或数据
- python - 用另一个整数列表离散一个列表
- c# - C# SvnClient 获取授权用户名
- gdb - 为什么 gdb 需要为每个命令按两次 enter btn?
- phpmailer - php mailer yahoo 显示已发送的电子邮件
- php - PHP从内存中的curl调整图像大小
- alfresco - Alfresco:如何使用“lucensearch”搜索文件的 mimeType