如何更改最后一个li和ul之间的背景颜色?

时间:2017-01-01 00:04:31

标签: html css html-lists

click here to see image!

如何更改背景颜色 正如你在上面的图片中看到的那样?我希望在上次li之后和ul之后更改颜色。我不想添加新的li,只需为ul设置整体背景颜色,然后更改图像中提到的背景颜色。

* {
	margin: 0;
	padding: 0;
}

body {
	font: 76% Verdana; 
	line-height: 1.4em;
}

a {
	text-decoration: none;
	font-weight: bold;
	color: #467aa7;
}

a:hover {
	color: #80B0DA;
	text-decoration: none;
}

/* Header */

.header {
	height: 110px;
	width: 758px;
	color: white;
	margin: 0 1px;
	background-color: #4279A5;
}

.header h1 {
	font-size: 2.4em;
	font-weight:normal;
	padding: 35px 0 0 20px;
}

.header h2 {
	font-size: 1.4em;
	font-weight:normal;
	margin: 10px 0 0 40px;
}

/* Navigation */

.nav {
	width: 758px;
	height: 2.2em;
	line-height: 2.06em;
	margin: 0 1px;
	background-color: #4279A5;
}

.nav ul {
	list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border-top: 1px solid white; 
}

.nav li {
	float: left;
	border-right: 1px solid white;
	list-style-type: none;
}

.nav li a {
	text-decoration: none;
	color: white;
	font-size: x-small;
	padding: 5px 10px 10px 5px;
}

.nav .selected, .nav li:hover {
	background-color: #80B0DA;
}
<!DOCTYPE html>
<html>
<head>
	<title>Hello</title>
	<link rel="stylesheet" type="text/css" href="kk.css">
</head>
<body>
	<div class="header">
		<h1>Hello World</h1>
		<h2>Slogan!</h2>
	</div>
		
	<div class="nav">
		<ul>
			<li class="selected"><a href="#first">FIRST PAGE</a></li>
			<li><a href="#second">SECOND</a></li>
			<li><a href="#third">THIRD</a></li>
			<li><a href="#forth">FORTH</a></li>
			<li><a href="#fifth">FIFTH</a></li>
			<li><a href="#last">AND THE LAST ONE</a></li>
		</ul>
	</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我知道你想做这样的事情:

li:last-child{
  background-color: #80B0DA;
}

这不是最好的答案,但你可以从这里解决问题:

* {
	margin: 0;
	padding: 0;
}

body {
	font: 76% Verdana; 
	line-height: 1.4em;
}

a {
	text-decoration: none;
	font-weight: bold;
	color: #467aa7;
}

a:hover {
	color: #80B0DA;
	text-decoration: none;
}

/* Header */

.header {
	height: 110px;
	width: 758px;
	color: white;
	margin: 0 1px;
	background-color: #4279A5;
}

.header h1 {
	font-size: 2.4em;
	font-weight:normal;
	padding: 35px 0 0 20px;
}

.header h2 {
	font-size: 1.4em;
	font-weight:normal;
	margin: 10px 0 0 40px;
}

/* Navigation */

.nav {
	width: 758px;
	height: 2.2em;
	line-height: 2.06em;
	margin: 0 1px;
	background-color: #4279A5;
}

.nav ul {
	width: 758px;
	list-style-type: none;
	margin: 0;
	padding: 0;
	overflow: hidden;
	border-top: 1px solid white; 
}

li:last-child {
	width: 332px;
	height: calc(100%-20px);
	background-color: #80B0DA;
}

.nav li {
	float: left;
	border-right: 1px solid white;
	list-style-type: none;
}

.nav li a {
	text-decoration: none;
	color: white;
	font-size: x-small;
	padding: 5px 10px 10px 5px;
}

.nav .selected, .nav li:hover {
	background-color: #80B0DA;
}
<!DOCTYPE html>
<html>
<head>
	<title>Hello</title>
	<link rel="stylesheet" type="text/css" href="kk.css">
</head>
<body>
	<div class="header">
		<h1>Hello World</h1>
		<h2>Slogan!</h2>
	</div>
		
	<div class="nav">
		<ul>
			<li class="selected"><a href="#first">FIRST PAGE</a></li>
			<li><a href="#second">SECOND</a></li>
			<li><a href="#third">THIRD</a></li>
			<li><a href="#forth">FORTH</a></li>
			<li><a href="#fifth">FIFTH</a></li>
			<li><a href="#last">AND THE LAST ONE</a></li>
			<li><a></a></li>
		</ul>
	</div>
</body>
</html>

答案 1 :(得分:0)

background-color样式添加.nav ul(例如红色):

.nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border-top: 1px solid white; 
    background-color: red;
}

并在.nav .selected, .nav li:hover样式之前添加此内容:

.nav li:not(.selected) {
    background-color: #4279A5;
}


实施例:

&#13;
&#13;
* {
    margin: 0;
    padding: 0;
}

body {
    font: 76% Verdana; 
    line-height: 1.4em;
}

a {
    text-decoration: none;
    font-weight: bold;
    color: #467aa7;
}

a:hover {
    color: #80B0DA;
    text-decoration: none;
}

/* Header */

.header {
    height: 110px;
    width: 758px;
    color: white;
    margin: 0 1px;
    background-color: #4279A5;
}

.header h1 {
    font-size: 2.4em;
    font-weight:normal;
    padding: 35px 0 0 20px;
}

.header h2 {
    font-size: 1.4em;
    font-weight:normal;
    margin: 10px 0 0 40px;
}

/* Navigation */

.nav {
    width: 758px;
    height: 2.2em;
    line-height: 2.06em;
    margin: 0 1px;
    background-color: #4279A5;
}

.nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border-top: 1px solid white;
    background-color: red;
}

.nav li {
    float: left;
    border-right: 1px solid white;
    list-style-type: none;
}

.nav li a {
    text-decoration: none;
    color: white;
    font-size: x-small;
    padding: 5px 10px 10px 5px;
}

.nav li:not(.selected) {
    background-color: #4279A5;
}

.nav .selected, .nav li:hover {
    background-color: #80B0DA;
}
&#13;
<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
    <link rel="stylesheet" type="text/css" href="kk.css">
</head>
<body>
    <div class="header">
        <h1>Hello World</h1>
        <h2>Slogan!</h2>
    </div>
        
    <div class="nav">
        <ul>
            <li class="selected"><a href="#first">FIRST PAGE</a></li>
            <li><a href="#second">SECOND</a></li>
            <li><a href="#third">THIRD</a></li>
            <li><a href="#forth">FORTH</a></li>
            <li><a href="#fifth">FIFTH</a></li>
            <li><a href="#last">AND THE LAST ONE</a></li>
        </ul>
    </div>
</body>
</html>
&#13;
&#13;
&#13;