Skeptar
Skeptar

Reputation: 189

div image position fixed + scroll

I want to create something like that: Page

I mean the first image. If i scroll the div under the img get over the image. How can i create that? If i give my img the position: fixed, everything is destroyed^^ And the position absoulte is maybe usefull but the div is next to the nav then. Here is my try: enter link description here

And here is the code:

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
    font-size: 18px;
}
html {
    height: 100%;
}
body{
    /*background: url("images/bg.png") repeat-x scroll left top #9EB5D6;
    background: url("images/bg2.png"); */
    height: 100%;
}

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

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;
}

.pic1 {
    background: url(images/banner.jpg);
    height: 100%;
    width: 100%;
}

.text{
    height: 800px;
    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="pic1">
            </div>
            <div class="text">
            </div>

    </body>
</html>

Upvotes: 3

Views: 7533

Answers (2)

Friso van Dijk
Friso van Dijk

Reputation: 669

It's about choosing the right approach. This type of scrolling can easily be done by a fixed menu (which you have at the top), a pushed-down div and a background image. I added a nice reusable cat image to show the effect.

You were almost there, just needed to give the nav a fixed position.

* {
  padding: 0;
  margin: 0;
}
body {
  background: url(https://c1.staticflickr.com/9/8226/8557105873_a82c51f03f_z.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
nav {
  background-color: #555555;
  text-align: center;
  height: 20px;
  position: fixed;
  top: 0px;
  width: 100%;
}
nav li {
  display: inline;
  list-style-type: none;
  padding-right: 20px;
}
nav li a {
  color: white;
  text-decoration: none;
}
.text {
  background-color: orange;
  margin-top: 500px;
  min-height: 1000px;
}
<html>

<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="text">
    text
  </div>

</body>

</html>

Upvotes: 2

Jean P. Johnson
Jean P. Johnson

Reputation: 31

You have to set the CSS rules as follows:

.pic1 {
    background-attachment: fixed;
    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: -1;
}

.text{
    margin-top: 792px;
}

This will make the div '.pic1' to be fixed and div '.text' to be able to scroll over div '.pic1'. That's the solution.

Something Extra:

Here I have given 'margin-top:792px' to div '.text'. But instead you can use jQuery to detect view-port height and assign that value as 'margin-top' for div '.text' which will make this always display properly below the fold across all screens.

here is how to do it via jQuery:

$(document).ready(function(){

var wH = $(window).height();
$('.text').css('margin-top',wH);

})

In this case you have to remove the following CSS rule:

 .text{
        margin-top: 792px;
    }

Please note, you have to initially link jQuery library in your web page. That is the whole jQuery code should be like shown below:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
 $(document).ready(function(){

    var wH = $(window).height();
    $('.text').css('margin-top',wH);

    })

</script>

should be added inside the markup, just before closing your BODY tag.

Upvotes: 2

Related Questions