1

I have a grid with two columns. On the right column I'm trying to implement the parallax background effect when scrolling, but for some reason, the background looks like it is being centered from the middle of the screen. How do I position the background so that it is within only the right column's grid?

header {
  display: flex;
  justify-content: center;
  align-items: center;
  background: gray;
}

body {
  height: 10000px
}

.navLink {
  padding: 0 25px;
}

#logo {
  width: 5%;
  height: 5%;
}


/*.container
{
 display: grid;
 grid-template-columns: [content] minmax(0, 1fr) [images] minmax(0, 1fr);
}*/

.container {
  display: grid;
  grid-template-columns: [content] 1fr [images] 1fr
}

.content {
  grid-column: content;
  background: blue;
}

#l-splash {}

.images {
  grid-column: images;
  background: yellow;
}

.spacing {
  height: 100px;
}

.image-block {
  width: 400px;
  height: 200px;
  margin: 0 auto;
}

#r-splash {}
    <div class="container">

      <div class="content">
        <div id="l-splash">
        </div>
      </div>

      <div class="images">
        <div class="spacing"></div>
        <div id="r-splash">
          <div class="image-block" style="background:url(http://placekitten.com/1543/1024) no-repeat center center fixed;"></div>

          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
          survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
          publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
      </div>
    </div>

http://jsfiddle.net/c8jw45ux/1/

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Trav
  • 135
  • 1
  • 2
  • 11

1 Answers1

1

When using fixed

The background is fixed relative to the viewport. Even if an element has a scrolling mechanism, the background doesn't move with the element.ref

In this case you need to adjust the position/size to make it on the right. Basically the image need to be in the center of the second half of the screen which is 75% but we have to place the center of the image on 75% thus we add 25% of its width (400px defined in the background-size equal to the conainer width). Then we place it at 100px from the top which will give us calc(75% + 100px) 100px/400px auto.

header {
  display: flex;
  justify-content: center;
  align-items: center;
  background: gray;
}

body {
  height: 200vh;
}

.navLink {
  padding: 0 25px;
}

#logo {
  width: 5%;
  height: 5%;
}

.container {
  display: grid;
  grid-template-columns: [content] 1fr [images] 1fr
}

.content {
  grid-column: content;
  background: blue;
}

.images {
  grid-column: images;
  background: yellow;
}

.spacing {
  height: 100px;
}

.image-block {
  width: 400px;
  height: 200px;
  margin: 100px auto 0;
  background:url(http://placekitten.com/1543/1024)  calc(75% + 100px) 100px /400px auto fixed no-repeat;
}
<div class="container">

  <div class="content">
    <div id="l-splash">
    </div>
  </div>

  <div class="images">
    <div id="r-splash">
      <div class="image-block"></div>

      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
  </div>
</div>

The above will work perfectly on big screen when we will have two equal sections. On small screen you may consider media query to adjust the position.

You can increase the size of the image more follwing the logic above:

header {
  display: flex;
  justify-content: center;
  align-items: center;
  background: gray;
}

body {
  height: 200vh;
}

.navLink {
  padding: 0 25px;
}

#logo {
  width: 5%;
  height: 5%;
}

.container {
  display: grid;
  grid-template-columns: [content] 1fr [images] 1fr
}

.content {
  grid-column: content;
  background: blue;
}

.images {
  grid-column: images;
  background: yellow;
}

.spacing {
  height: 100px;
}

.image-block {
  width: 400px;
  height: 200px;
  margin: 100px auto 0;
  background:url(http://placekitten.com/1543/1024)  calc(75% + 200px) 50px /800px auto fixed no-repeat;
}
<div class="container">

  <div class="content">
    <div id="l-splash">
    </div>
  </div>

  <div class="images">
    <div id="r-splash">
      <div class="image-block"></div>

      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
  </div>
</div>

You can get more details about the calculation here: https://stackoverflow.com/a/51734530/8620333

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Hi. Thanks for taking the time to help. I implemented your changes and noticed that the image box where you can see the background actually changes size when scrolling? Is there a way to keep it from expanding when scrolling? http://jsfiddle.net/xgndjq0s/ – Trav Feb 24 '19 at 02:14
  • if you look at this example, the window you can see the the image background remains the same size http://jsfiddle.net/WZm9Z/. do you know why the window to see the image background gets modified in my right grid column? – Trav Feb 24 '19 at 15:50
  • @Trav increase the background size, try this `url(http://placekitten.com/1543/1024) calc(75% + 200px) 100px /800px auto fixed no-repeat` – Temani Afif Feb 24 '19 at 15:52
  • unfortunately modifying that didn't work. the .image-block is still changing sizes when scrolling. the goal is to have the .image-block remain constant with the background remaining fixed – Trav Feb 24 '19 at 19:30
  • @Trav the image block is constant, add border to it to notice .. the issue is the moving image... since it's getting fixed and you want to center it there so it will be cut when you scroll since it's fixed – Temani Afif Feb 24 '19 at 19:31
  • I have linked an album of screenshots to show what I'm referring to. https://imgur.com/a/pAYuV0h. the first image shows the current implementation where the block is collapsed the further you scroll down. in the next two pictures you can see that the image block remains the same rectangular shape no matter how far you scroll down the page – Trav Feb 25 '19 at 01:54
  • @Trav check this: https://jsfiddle.net/xabo10r7/ .. I have increased the image size and I added border. You can see that block is always the same and it's all about the image that is fixed, compare with the code here. When the image is set to a small size you will have the unwanted effect, you simply need to make it bigger enough so it always cover the area on scroll – Temani Afif Feb 25 '19 at 07:55
  • when you do calc(75% + 200px) 0px/800px. Correct me if I'm wrong but the 75% moves the center of the background to the center of the right grid. what's the 200px do exactly? the 0px signifies the position from the height of the container and the 800px dictates the size the background makes up the container? Thank you for your commitment to helping me understand this. – Trav Feb 26 '19 at 04:30
  • @Trav the `800px auto` is the size which means `width height` then `calc(75% + 200px) 0` is the position which means 0 from the top of the viewport and the other one means put the center of the image at 75% ... I used 200px to have a perfect center because percentage value behave differently as pixel value, check the link at the end of my answer to better understand why I used calc(75% + 200px) and how it works – Temani Afif Feb 26 '19 at 07:51
  • @Trav Also note that when using fixed, all the values are relative to viewport not the container that's why we need some *complex* calculation to have the element in the needed place where the container is – Temani Afif Feb 26 '19 at 07:55
  • Do you know why this demo site https://www.w3schools.com/howto/tryhow_css_parallax_demo.htm is able to have these fixed backgrounds relative to the container then? – Trav Feb 26 '19 at 20:35
  • @Trav because as you can see the container is having full width and full height of the screen so you think it's the container but it's not. You container is simply having the same dimension as the viewport and is already centred so it's easier in this case than your case. – Temani Afif Feb 26 '19 at 20:47
  • do you think from a design perspective, a table format or flex box format would be better suited? to give you a gist the site would be divided in half down the center with content on the left and parallax scrolling images on the right. – Trav Feb 26 '19 at 20:51
  • @Trav I wouldn't consider table but either flexbox or grid. I cannot tell you if one is better than the other, all depend on what you know better and how you will use it. Technically we can achieve the same with both of them – Temani Afif Feb 27 '19 at 00:34