【问题标题】:CSS current page nav bar color changeCSS 当前页面导航栏颜色变化
【发布时间】:2017-02-17 01:01:21
【问题描述】:

我正在处理一个具有垂直导航栏的页面的作业,其中每个链接都包含在一个框中。该分配要求将当前链接的框更改为红色。我尝试了几种不同的方法,但似乎都没有奏效。这就是我所拥有的:

   /* the styles for the elements */
* {
	margin: 0;
	padding: 0;
}
html {
	background-color: white;
}
body {
	font-family: Arial, Helvetica, sans-serif;
    font-size: 100%;
    width: 850px;
    margin: 0 auto;
    border: 3px solid #931420;
    background-color: #fffded;
}
a:focus, a:hover {
	font-style: italic;
}
/* the styles for the header */
header {
	padding: 1.5em 0 2em 0;
	border-bottom: 3px solid #931420;
		background-image: -moz-linear-gradient(
	    30deg, #f6bb73 0%, #f6bb73 30%, white 50%, #f6bb73 80%, #f6bb73 100%);
	background-image: -webkit-linear-gradient(
	    30deg, #f6bb73 0%, #fffded 30%, white 50%, #fffded 80%, #f6bb73 100%);
	background-image: -o-linear-gradient(
	    30deg, #f6bb73 0%, #fffded 30%, white 50%, #fffded 80%, #f6bb73 100%);
	background-image: linear-gradient(
	    30deg, #f6bb73 0%, #fffded 30%, white 50%, #fffded 80%, #f6bb73 100%);
}
header h2 {
	font-size: 175%;
	color: #800000;
}
header h3 {
	font-size: 130%;
	font-style: italic;
}
header img {
	float: left;
	padding: 0 30px;
}
.shadow {
	text-shadow: 2px 2px 2px #800000;
}
/* the styles for the main content */
main {
	clear: left;
}

/* the styles for the section */
section {
	width: 575px;
	float: right;
	padding: 0 20px 20px 20px;
}
section h1 {
	color: #800000;
	font-size: 150%;
	padding-top: .5em;
	margin: 0;
}
section p {
	padding-bottom: .5em;
}

#boldp{
	font-weight: bold;
}
#navlist .current{
	background-color: #800000;
}
#navlist ul li{
	margin: 5px;
	background-image: url("../images/right.jpg");
	background-repeat: no-repeat;
	background-position: 95%;
	border: 2px solid black;
	border-radius:25px;
	padding: 10px;
	width: 175px;
	height: 35px;
	box-shadow: 5px 5px 2px #800000;

}

#navlist ul{
	list-style-type: none;
	padding: 10px;
}

/* the styles for the article */
article {
	padding: .5em 0;
}
article h2 {
	font-size: 115%;
	padding: 0 0 .25em 0;
}
article img {
	float: right;
	margin: .5em 0 1em 1em;
	border: 1px solid black;
}

/* the styles for the aside */
aside {
	width: 215px;
	float: right;
	padding: 0 0 20px 20px;
}
aside h2 {
	color: #800000;
	font-size: 130%;
	padding: .6em 0 .25em 0;
}

aside h3 {
	font-size: 105%;
	padding-bottom: .25em;
}
aside img {
	padding-bottom: 1em;
}

/* the styles for the footer */
footer {
	background-color: #931420;
	clear: both;

}
footer p {
	text-align: center;
	color: white;
	padding: 1em 0;
}
<aside>
			<h2>Guest speakers</h2>
			<nav id ="navlist">
				<ul>
					<li><h3><a href="c06x_toobin.html">October<br>Jeffrey Toobin</a></h3></li>
					<li><h3><a href="#">November<br>Andrew Ross Sorkin</a></h3></li>	
					<li><h3><a href="#">January<br>Amy Chua</a></h3></li>		
					<li class ="current"><h3><a href="c07x_sampson.html">February<br>Scott Sampson</a></h3></li>
				</ul>
			<h3><a href="../index.html">Return to Home page</a></h3>
				
			</nav>
		</aside>

【问题讨论】:

    标签: html css nav


    【解决方案1】:

    #navlist .current 将针对 .current 类,您可以将 background 或任何它应用于该类。

    #navlist .current {
      background: red;
    }
    #navlist ul li{
    	margin: 5px;
    	background-image: url("../images/right.jpg");
    	background-repeat: no-repeat;
    	background-position: 95%;
    	border: 2px solid black;
    	border-radius:25px;
    	padding: 10px;
    	width: 175px;
    	height: 35px;
    	box-shadow: 5px 5px 2px #800000;
    
    }
    
    #navlist ul li.sam {
    	background-color: #800000;
    	}
    
    #navlist ul{
    	list-style-type: none;
    	padding: 10px;
    }
    <aside>
    			<h2>Guest speakers</h2>
    			<nav id ="navlist">
    				<ul>
    					<li><h3><a href="c06x_toobin.html">October<br>Jeffrey Toobin</a></h3></li>
    					<li><h3><a href="#">November<br>Andrew Ross Sorkin</a></h3></li>	
    					<li><h3><a href="#">January<br>Amy Chua</a></h3></li>		
    					<li class ="current"><h3><a href="c07x_sampson.html">February<br>Scott Sampson</a></h3></li>
    				</ul>
    			<h3><a href="../index.html">Return to Home page</a></h3>
    				
    			</nav>
    		</aside>

    【讨论】:

    • 我的 css 文件中必须有其他东西覆盖它,因为这些修复都不起作用。我会把其余的添加到原帖中
    • @KaitJardine 背景更改在您的示例中起作用,不是吗?
    • snipett 正在这里工作,但是当我应用这些更改并将其上传到服务器时,它不会更改网页。这就是为什么我认为在 css 中必须有一些东西覆盖它以及为什么我编辑问题以包含整个 css
    • @KaitJardine 我的意思是它在 你的 sn-p 中工作,而不是我的。
    【解决方案2】:

    嘿,如果我理解你的权利,我认为你只需要为当前课程添加背景

    .current {
      background-color:red;
    }
    

    【讨论】: