单击选项卡时更改背景颜色

时间:2013-02-05 00:27:17

标签: css css3

单击选项卡时,如何将背景更改为完全相同的颜色?目前,它只是保持相同的颜色。当页面加载时,第一个标签的颜色是否与内容区域的背景颜色相同?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSS-TABS | CSS-Only "DOM TABS"</title>

<style type="text/css">

    #pagewidth { width: 760px; margin: 0 auto 0 auto; }

    #navigation { margin: 0 10em 0 0; background: #000000; color: #FFFFFF; }
    #navigation li { display: inline; padding: 0 2em 0 2em; }

    .content { background: #CCCCCC; height: 20em; padding: 1em;}

    a { color: #0066FF; }
    a:hover { color: #00CCFF; }
    a:active { font-weight:bold; }

    #container { height: 20em; overflow: hidden; }

</style>

</head>

<body>

<div id="pagewidth">

    <ul id="navigation">
        <li><a href="#c1">Content Block 1</a></li>
        <li><a href="#c2">Content Block 2</a></li>
        <li><a href="#c3">Content Block 3</a></li>
    </ul>

    <div id="container">

        <div class="content">
            <a name="c1" id="c1"></a>
            <h1>Heading 1</h1>
            <p>Here is some content, I hope that you like it!</p>
        </div>

        <div class="content">
            <a name="c2" id="c2"></a>
            <h2>Heading 2</h2>
            <p>Now that you have read content block 1, you can learn more in this block</p>
        </div>

        <div class="content">
            <a name="c3" id="c3"></a>
            <h3>Heading 3</h3>
            <p>In conclusion, content blocks are fun</p>
        </div>

    </div> <!-- end container -->

</div> <!-- end pagewidth -->

</body>
</html>

1 个答案:

答案 0 :(得分:0)

你只需要更多的HTML,以及更多的CSS,就像这样:

CSS已添加:

.content .sub{ height: 20em; padding: 1em; }
...
#sub1{ background: blue; }
#sub2{ background: green; }
#sub3{ background: yellow; }

<div class="sub" id="sub1">...</div>

...作为标签内容的包装。

最后一页看起来像这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSS-TABS | CSS-Only "DOM TABS"</title>
<style>
    #pagewidth { width: 760px; margin: 0 auto 0 auto; }

    #navigation { margin: 0 10em 0 0; background: #000000; color: #FFFFFF; }
    #navigation li { display: inline; padding: 0 2em 0 2em; }

    .content { background: #CCCCCC; }
    .content .sub{ height: 20em; padding: 1em; }

    a { color: #0066FF; }
    a:hover { color: #00CCFF; }
    a:active { font-weight:bold; }

    #container { height: 20em; overflow: hidden; }

    #sub1{ background: blue; }
    #sub2{ background: green; }
    #sub3{ background: yellow; }
</style>
</head>
<body>
<div id="pagewidth">

<ul id="navigation">
    <li><a href="#c1">Content Block 1</a></li>
    <li><a href="#c2">Content Block 2</a></li>
    <li><a href="#c3">Content Block 3</a></li>
</ul>

<div id="container">

    <div class="content">
        <a name="c1" id="c1"></a>
        <div class="sub" id="sub1">
        <h1>Heading 1</h1>
        <p>Here is some content, I hope that you like it!</p>
        </div>
    </div>

    <div class="content">
        <a name="c2" id="c2"></a>
        <div class="sub" id="sub2">
        <h2>Heading 2</h2>
        <p>Now that you have read content block 1, you can learn more in this block</p>
        </div>
    </div>

    <div class="content">
        <a name="c3" id="c3"></a>
        <div class="sub" id="sub3">
        <h3>Heading 3</h3>
        <p>In conclusion, content blocks are fun</p>
        </div>
    </div>

</div> <!-- end container -->

</div> <!-- end pagewidth -->

</body>
</html>

这是一个JSFiddle,显示它是如何工作的:http://jsfiddle.net/fDk3z/