Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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%;
}
navli {
  display: inline;
  list-style-type: none;
  padding-right: 20px;
}
navlia {
  color: white;
  text-decoration: none;
}
.text {
  background-color: orange;
  margin-top: 500px;
  min-height: 1000px;
}
<html><body><nav><ul><li><ahref="#">Link 1</a></li><li><ahref="#">Link 1</a></li><li><ahref="#">Link 1</a></li></ul></nav><divclass="text">
    text
  </div></body></html>

Solution 2:

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:

<scripttype="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.

Post a Comment for "Div Image Position Fixed + Scroll"