CSS:如何将具有不同z-index的背景拉伸到页面内容的高度

时间:2016-06-12 10:19:29

标签: css responsive-design z-index

所以我正在构建一个网页,我无法找到一种方法来动态地将背景(使用不同的z-index)拉伸到页脚的开头。我搜索了javascript,jquery和css方法,但没有骰子。有人知道怎么做吗?这是我的代码:

<html>
<head>
    <style>
        *
        {
            list-style: none;
            text-decoration:none;
            padding: 0;
            margin: 0;
        }
        html
        {
            margin: 0 auto;
            width: 100%;
            background: #ccc;

        }
        body 
        {
            margin: 0;
            padding: 0;

        }
        #page
        {
            width: 100%;

        }
        #grey_block_left
        {
            width: 30%;
            background-color: #333333;
            height: 100%;
            left: 0px;
            top: 0px;
            position: absolute;
            z-index: 0;
        }
        #purple_block_right
        {
            width: 70%;
            background-color: #9966cc;
            height: 100%;
            right: 0px;
            top: 0px;
            position: absolute;
            z-index: 0;
        }
        #content
        {
            width: 70%;
            margin: 0 auto;
            background-color: #fff;
            height: 1000px;
            position: relative;
            z-index: 5;
            margin-top: 150px;
            margin-bottom: 50px;
            padding: 20px;
        }
        #footer
        {
            position: relative;
            z-index: 5;
            width: 100%;
            height: 300px;
            background-color: #333;
            color: white;
        }
    </style>
</head>
<body>
    <div id="page">
        <div id="grey_block_left"></div>
        <div id="purple_block_right"></div>
        <div id="content">
            <h1>Content</h1>
        </div>
        <div id="footer">
        </div>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

这个怎么样?改变绝对到固定?

<html>
<head>
<style>
    *
    {
        list-style: none;
        text-decoration:none;
        padding: 0;
        margin: 0;
    }
    html
    {
        margin: 0 auto;
        width: 100%;
        background: #ccc;

    }
    body 
    {
        margin: 0;
        padding: 0;

    }
    #page
    {
        width: 100%;

    }
    #grey_block_left
    {
        width: 30%;
        background-color: #333333;
        height: 100%;
        left: 0px;
        top: 0px;
        position: fixed;
        z-index: 0;
    }
    #purple_block_right
    {
        width: 70%;
        background-color: #9966cc;
        height: 100%;
        right: 0px;
        top: 0px;
        position: fixed;
        z-index: 0;
    }
    #content
    {
        width: 70%;
        margin: 0 auto;
        background-color: #fff;
        height: 1000px;
        position: relative;
        z-index: 5;
        margin-top: 150px;
        margin-bottom: 50px;
        padding: 20px;
    }
    #footer
    {
        position: relative;
        z-index: 5;
        width: 100%;
        height: 300px;
        background-color: #333;
        color: white;
    }
   </style>
   </head>
   <body>
    <div id="page">
    <div id="grey_block_left"></div>
    <div id="purple_block_right"></div>
    <div id="content">
        <h1>Content</h1>
    </div>
    <div id="footer">
    </div>
</div>

答案 1 :(得分:1)

您将获得页脚的offsetTop,这将是z索引的高度#purple_block_right和#grey_block_left

footer_height = document.getElementById("footer").offsetTop
document.getElementById("grey_block_left").style.height = footer_height + "px";
document.getElementById("purple_block_right").style.height = footer_height + "px";

希望有所帮助