div容器,背景图像位置固定

时间:2015-01-01 17:36:58

标签: html css

我想创建一个这样的页面:Page

我不想把图像作为背景。我想创建一个div容器,这个div容器有一个背景图像。在这个div下应该是另一个div。如果我使用背景图像滚动div下的div应该用背景图像越过div。我有东西,但它是废话。 在这里我的页面:1

以下是代码:

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
    font-size: 18px;
}
html {
    height: 100%;
}
body{
    height: 100%;
    background-image: url('http://cdn1.editmysite.com/uploads/3/8/9/4/38945355/background-images/748028443.png') !important;
    background-repeat: no-repeat !important;
    background-position: 50% 0 !important;
    background-size: auto !important;
    background-color: transparent !important;
    background: inherit;
}

nav{
   background: url("images/line-header.png") repeat-x scroll center bottom #4A525A;
   padding: 15px;
   position: fixed;
   top: 0px;
   width: 100%;
}

nav > ul{
    margin: 0px;
    padding: 0px;
    text-align: center;
}

nav ul > li{
    margin-left: 25px;
    display: inline-block;
    list-style-type: none;
}

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    color: #697683;
    transition: color 0.5s;
}

nav ul li > a:hover{
    color: #FFF;
}

.content{
    height: 1000px;
    margin-top: 500px;
    background-color: orange;
}
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="index.css" >
        <!-- Open Sans -->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    <body>

            <nav>
                <ul>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                </ul>
            </nav>

            <div class="content">
                <h1></h1>
            </div>

    </body>
</html>

2 个答案:

答案 0 :(得分:1)

只需添加其他div,然后将background-attachment设置为fixed

.background{
  background: url('http://cdn1.editmysite.com/uploads/3/8/9/4/38945355/background-images/748028443.png') no-repeat;
  background-attachment: fixed;
  background-size: cover;
  height: 500px; <------can make 500px instead of using margin-top: 500px on .content
}


.content{
  height: 1000px;
  background-color: orange;
  /*margin-top: 500px;*/ <-------------take out
}

FIDDLE

答案 1 :(得分:1)

您可以尝试这样做,添加700px的图像高度和背景附件:固定;

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
    font-size: 18px;
}
html {
    height: 100%;
}
body{
    height: 700px;
    background-image:url('http://cdn1.editmysite.com/uploads/3/8/9/4/38945355/background-        images/748028443.png') !important;
    background-repeat: no-repeat !important;
    background-position: 50% 0 !important;
    background-size: auto !important;
    background-color: transparent !important;
    background-attachment: fixed;
}

nav{
   background: url("images/line-header.png") repeat-x scroll center bottom #4A525A;
   padding: 15px;
   position: fixed;
   top: 0px;
   width: 100%;
}

nav > ul{
    margin: 0px;
    padding: 0px;
    text-align: center;
}

nav ul > li{
    margin-left: 25px;
    display: inline-block;
    list-style-type: none;
}

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    color: #697683;
    transition: color 0.5s;
}

nav ul li > a:hover{
    color: #FFF;
}

.content{
    height: 1000px;
    margin-top: 500px;
    background-color: orange;
}


<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="index.css" >
        <!-- Open Sans -->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    <body>

            <nav>
                <ul>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                </ul>
            </nav>

            <div class="content">
                <h1></h1>
            </div>

    </body>
</html>