Search code examples
cssparallax

Unsmooth parallax effect during scrolling - with border-radius method


I’ve just started to learn HTML/CSS. My goal is to prepare a parallax effect on my test website. I constructed a code with parallax effect in CSS, but the problem is that the images located under the container is unsmooth during scrolling the page (the image extends and rips).

Please consider that I used border-radius method which rounds corners of the containers under which an images are located. I noted that when I cut border-radius method then the unsmoothing effect doesn’t occur. But my goal is to leave this border-radius method unchanged

I know that I can construct similar parallax effect in JS, but my goal is to understand reason why parallax effect doesn’t work correctly in CSS together with border-radius method.

I focused that the unwanted effect occurs only in the case when the browser page is narrowed. Please see the differences between the effect in Codepen one with code (part of the browser page in which finishing page is showed is narrowed):

https://codepen.io/marartgithub/pen/vYpPEjQ

and second one in full page (the problem doesn’t occur):

https://codepen.io/marartgithub/full/vYpPEjQ

I'm sorry if the problem is not the biggest one and for some of you could be insignificant, but my goal is to understand why not all which I wanted works fine to be better programmer.


Solution

  • I would use a :before pseudo tag to achieve this effect. Here are the changes I made:

    1. I remove the about bg div and set each box to flexbox as that will be a cleaner way to acheive this layout.
    2. Then, I removed the border-radius from .about-us-box and added it to .about-us-box:before. In the :before styling, I set it the size of the parent container (.about-us-box) and then set it to have a border radius. You will see box-shadow attribute as border-radius doesn't curve the inside corner. Box-shadow takes care of that for us.

    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }
    
    body {
        font-family: 'Raleway', sans-serif;
    }
    
    /* n a v */
    .nav {
        height: 50px;
        background-color: #333;
        text-align: center;
        line-height: 50px;
        font-size: 0;   
    }
    
    .nav-item {
        display: inline-block;
    }
    
    .nav-item a {
        padding: 0 50px;
        color: whitesmoke;
        text-decoration: none;
        text-transform: uppercase;
        font-weight: bold;
        letter-spacing: 2px;
        transition: color 0.3s;
        font-size: 16px;    
    }
    
    .nav-item a:hover {
        color: royalblue;
    }
    
    /* h e a d e r */
    .header-jpg {
        position: relative;
        height: 300px;
        background-image: url('https://cdn.pixabay.com/photo/2016/09/29/13/08/planet-1702788_1280.jpg');
        background-size: cover;
        background-position: 0 50%;
    }
    
    .header-text {
        position: absolute;
        color: whitesmoke;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);   
    }
    
    .header-bg {
        position: absolute;
        height: 100%;
        width: 100%;    
    }
    
    .header-text h1 {
        direction: rtl;
        margin-bottom: 10px;
        text-transform: lowercase;
        letter-spacing: 2px;
        text-shadow: 2px 2px 6px gold;
    }
    
    /* m a i n */
    main {
        margin: 50px auto;
        width: 1200px;
    }
    
    main h2 {
        margin-bottom: 20px;
        text-transform: uppercase;
        text-align: center;
        font-weight: 100;
        font-size: 16px;
    }
    
    .about-us-box {
      position: relative;
        height: 300px;
        margin: 40px 0;
        background-size: cover;
        background-attachment: fixed;
        background-position: center center;
      background-repeat: no-repeat;
      display: flex;
      align-items: flex-end;
      z-index: 0;
    }
    
    .about-us-box:before {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: 20px 0 20px 0;
      z-inex: 1;
      background-color: transparent;
      border-radius: 20px 0 20px 0;
      box-shadow: 0 0 0 13px #fff;
    }
    
    .top {
        background-image: url('https://cdn.pixabay.com/photo/2017/08/06/07/10/coffee-2589761_1280.jpg');
    }
    
    .middle {
        background-image: url('https://cdn.pixabay.com/photo/2017/06/10/16/19/iphone-2390121_1280.jpg');
    }
    
    .bottom {
        background-image: url('https://cdn.pixabay.com/photo/2015/01/09/11/08/startup-594090_1280.jpg');
    }
    
    .about-us-text {
        text-align: center;
        color: whitesmoke;
      padding: 2rem 1rem;
      background-color: black;
    }
    
    .about-us-text h3 {
        margin-bottom: 10px;
        text-transform: uppercase;
    }
    
    /* f o o t e r */
    footer {
        height: 80px;
        line-height: 80px;
        background-color: #333;
        color: #ddd;
        text-align: center;
        font-size: 20px;
    }
    
    .icon-box {
        margin-left: 20px;
    }
    
    .icon-box a {
        margin: 0 5px;
        color: #ddd;
        text-decoration: none;
        font-size: 20px;
        transition: color 0.3s;
    }
    .icon-box a:hover {
        color: royalblue;
    }
    
    .ti {
        padding-right: 10px;
        font-size: 26px;
        margin-right: 10px;
    }
    
    .elem-main {
        width: 300px;
        margin: 0 auto;
    }
    
    .prices-table {
        margin: 0 auto;
    }
    
    .prices-table td {
        padding: 10px 30px;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>TASK - WE LOVE COFFEE</title>
      <link rel="preconnect" href="https://fonts.googleapis.com" />
      <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
      <link href="https://fonts.googleapis.com/css2?family=Raleway&display=swap" rel="stylesheet" />
      <link rel="stylesheet" href="https://unpkg.com/@tabler/icons@latest/iconfont/tabler-icons.min.css" />
      <link rel="stylesheet" href="./css/style_en.css" />
    </head>
    
    <body>
      <header>
        <div class="header-jpg">
          <div class="header-bg"></div>
          <div class="header-text">
            <h1>Creative design</h1>
            <p>With our support you will create a dreamlike website</p>
          </div>
        </div>
      </header>
      <nav class="nav">
        <ul>
          <li class="nav-item"><a href="index.html">home</a></li>
          <li class="nav-item"><a href="services.html">services</a></li>
          <li class="nav-item"><a href="pricing.html">pricing</a></li>
          <li class="nav-item"><a href="contact.html">contact</a></li>
        </ul>
      </nav>
      <main>
        <h2>About us</h2>
    
        <div class="about-us-box top">
            <div class="about-us-text">
              <h3>We love coffee</h3>
              <p>
                We interested in coffe in our team on years. We love his smell and
                taste. We love the process on which coffee beans goes through
                starting from day of cutting during harvest then heat treatment to
                grinding process in our coffee grinder and passing it through a
                espresso machine.
              </p>
          </div>
        </div>
        <div class="about-us-box middle">
          <div class="about-us-text">
            <h3>We all are creative</h3>
            <p>
              Characteristic of our work requires from us to be continously a
              creative persons, because of competentive market and our clients
              demands which expects from us to provide unconventional solutions
              supported theri business.
            </p>
          </div>
        </div>
        <div class="about-us-box bottom">
          <div class="about-us-text">
            <h3>We like our job</h3>
            <p>
              We are young team of simmilar thingking and creative and full
              positive energy persons. We meets as well outside of our job to
              receive a good balance between proffesionall acvivity and private
              life.
            </p>
          </div>
        </div>
      </main>
      <footer>
        <p>
          &copy; 2022 Creative design
          <span class="icon-box">
            <a href="#"><i class="ti ti-brand-facebook"></i></a>
            <a href="#"><i class="ti ti-brand-twitter"></i></a>
          </span>
        </p>
      </footer>
    </body>
    
    </html>