skip to Main Content

I am working on a vertical timeline where i need to show the yearly events and i year can have multiple event.

I am trying to make year sticky so that when user scroll down a long list of event for any year, then year should remain sticky for that year till that container.

i did add below css however it down work

 h2 
  {
  display:inline-block;
  position:relative;
  background:red;
  margin-left:-150px;
  position: -webkit-sticky;
  position: sticky;
  }
* {
  box-sizing: border-box;
}
body {
  background-color: #474e5d;
  font-family: Helvetica, sans-serif;
}

/* The actual timeline (the vertical ruler) */
.timeline {
  position: relative;
  max-width: 1200px;
  margin: 0 auto;
}

/* The actual timeline (the vertical ruler) */
.timeline::after {
  content: '';
  position: absolute;
  width: 6px;
  background-color: white;
  top: 0;
  bottom: 0;
  left: 50%;
  left: 200px;
  margin-left: -3px;
}

/* Container around content */
.container {
  padding: 10px 40px;
  position: relative;
  background-color: inherit;
  width: 50%;
}

/* The circles on the timeline */
.container::after {
  content: '';
  position: absolute;
  width: 25px;
  height: 25px;
  right: -17px;
  background-color: white;
  border: 4px solid #FF9F55;
  top: 15px;
  border-radius: 50%;
  z-index: 1;
}

/* Place the container to the left */
.left {
  left: 0;
}

/* Place the container to the right */
.right {
  left: 50%;
left: 200px;
}

/* Add arrows to the left container (pointing right) */
.left::before {
  content: " ";
  height: 0;
  position: absolute;
  top: 22px;
  width: 0;
  z-index: 1;
  right: 30px;
  border: medium solid white;
  border-width: 10px 0 10px 10px;
  border-color: transparent transparent transparent white;
}

/* Add arrows to the right container (pointing left) */
.right::before {
  content: " ";
  height: 0;
  position: absolute;
  top: 22px;
  width: 0;
  z-index: 1;
  left: 30px;
  border: medium solid white;
  border-width: 10px 10px 10px 0;
  border-color: transparent white transparent transparent;
}

/* Fix the circle for containers on the right side */
.right::after {
  left: -16px;
}

/* The actual content */
.content {
  padding: 20px 30px;
  background-color: white;
  position: relative;
  border-radius: 6px;
}

/* Media queries - Responsive timeline on screens less than 600px wide */
@media screen and (max-width: 600px) {
  /* Place the timelime to the left */
  .timeline::after {
  left: 31px;
  }
  
  /* Full-width containers */
  .container {
  width: 100%;
  padding-left: 70px;
  padding-right: 25px;
  }
  
  /* Make sure that all arrows are pointing leftwards */
  .container::before {
  left: 60px;
  border: medium solid white;
  border-width: 10px 10px 10px 0;
  border-color: transparent white transparent transparent;
  }

  /* Make sure all circles are at the same spot */
  .left::after, .right::after {
  left: 15px;
  }
  
  /* Make all right containers behave like the left ones */
  .right {
  left: 0%;
  }
 
}
.content {
  position: relative;
}
 h2 
  {
  display:inline-block;
  position:relative;
  background:red;
  margin-left:-150px;
  position: -webkit-sticky;
  position: sticky;
  }
<div class="timeline">
  <div class="container right">
    <div class="content">
      <h2>2017</h2>
      <p>1. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
    </div>
  </div>
  <div class="container right">
    <div class="content">
      <h2>2016</h2>
      <p>1. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
            <p>2. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum.</p>
      <p>3. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum. orem ipsum dolor sit amet, quo orem ipsum dolor sit amet, quo </p>
      <p>4. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum.orem ipsum dolor sit amet, quo </p>
      <p>4. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum.</p>
    </div>
  </div>
  <div class="container right">
    <div class="content">
      <h2>2015</h2>
      <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
    </div>
  </div>
  <div class="container right">
    <div class="content">
      <h2>2012</h2>
      <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
      <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
    </div>
  </div>
  <div class="container right">
    <div class="content">
      <h2>2011</h2>
      <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
    </div>
  </div>
  <div class="container right">
    <div class="content">
      <h2>2007</h2>
      <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
    </div>
  </div>
</div>

2

Answers


  1. The main problem was the relation with your h2 and the sticky class is that it was missing a top:0px css attribute.

    I’ve moved your h2 a level up in the html (only for the first so you can see) then I changed your css a bit. (This is optional but I think it works better that way.)

    Demo

    (Run the code in full page to see it properly, since i haven’t altered code for mobile.)

    * {
      box-sizing: border-box;
    }
    body {
      background-color: #474e5d;
      font-family: Helvetica, sans-serif;
    }
    
    /* The actual timeline (the vertical ruler) */
    .timeline {
      position: relative;
      max-width: 1200px;
      margin: 0 auto;
    }
    
    /* The actual timeline (the vertical ruler) */
    .timeline::after {
      content: '';
      position: absolute;
      width: 6px;
      background-color: white;
      top: 0;
      bottom: 0;
      left: 50%;
      left: 200px;
      margin-left: -3px;
    }
    
    /* Container around content */
    .container {
      padding: 10px 40px;
      position: relative;
      background-color: inherit;
      width: 50%;
    }
    
    /* The circles on the timeline */
    .container::after {
      content: '';
      position: absolute;
      width: 25px;
      height: 25px;
      right: -17px;
      background-color: white;
      border: 4px solid #FF9F55;
      top: 15px;
      border-radius: 50%;
      z-index: 1;
    }
    
    /* Place the container to the left */
    .left {
      left: 0;
    }
    
    /* Place the container to the right */
    .right {
      left: 50%;
    left: 200px;
    }
    
    /* Add arrows to the left container (pointing right) */
    .left::before {
      content: " ";
      height: 0;
      position: absolute;
      top: 22px;
      width: 0;
      z-index: 1;
      right: 30px;
      border: medium solid white;
      border-width: 10px 0 10px 10px;
      border-color: transparent transparent transparent white;
    }
    
    /* Add arrows to the right container (pointing left) */
    .right::before {
      content: " ";
      height: 0;
      position: absolute;
      top: 22px;
      width: 0;
      z-index: 1;
      left: 30px;
      border: medium solid white;
      border-width: 10px 10px 10px 0;
      border-color: transparent white transparent transparent;
    }
    
    /* Fix the circle for containers on the right side */
    .right::after {
      left: -16px;
    }
    
    /* The actual content */
    .content {
      padding: 20px 30px;
      background-color: white;
      position: relative;
      border-radius: 6px;
    }
    
    /* Media queries - Responsive timeline on screens less than 600px wide */
    @media screen and (max-width: 600px) {
      /* Place the timelime to the left */
      .timeline::after {
      left: 31px;
      }
      
      /* Full-width containers */
      .container {
      width: 100%;
      padding-right: 25px;
      }
      
      /* Make sure that all arrows are pointing leftwards */
      .container::before {
      left: 60px;
      border: medium solid white;
      border-width: 10px 10px 10px 0;
      border-color: transparent white transparent transparent;
      }
    
      /* Make sure all circles are at the same spot */
      .left::after, .right::after {
      left: 15px;
      }
      
      /* Make all right containers behave like the left ones */
      .right {
      left: 0%;
      }
     
    }
    .content {
      position: relative;
      margin-left: 0px;
    }
     h2 
      {
      position:relative;
      background:red;
      margin-left:-150px;
      position: -webkit-sticky;
      position: sticky;
      top:0px;
        float: left;
        z-index: 2000;
        left: 0px;
        display: inline-block;
        margin-left: -119px;
      }
    <div class="timeline">
      <div class="container right">
          <h2>2017</h2>
        <div class="content">
          <p>1. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
        </div>
      </div>
      <div class="container right">
        <div class="content">
          <h2>2016</h2>
          <p>1. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
                <p>2. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum.</p>
          <p>3. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum. orem ipsum dolor sit amet, quo orem ipsum dolor sit amet, quo </p>
          <p>4. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum.orem ipsum dolor sit amet, quo </p>
          <p>4. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum.</p>
        </div>
      </div>
      <div class="container right">
        <div class="content">
          <h2>2015</h2>
          <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
        </div>
      </div>
      <div class="container right">
        <div class="content">
          <h2>2012</h2>
          <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
          <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
        </div>
      </div>
      <div class="container right">
        <div class="content">
          <h2>2011</h2>
          <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
        </div>
      </div>
      <div class="container right">
        <div class="content">
          <h2>2007</h2>
          <p>Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit voluptua dignissim per, habeo iusto primis ea eam.</p>
        </div>
      </div>
    </div>
    Login or Signup to reply.
  2. Or you can wrap it with an absolute div, then use sticky on child.

    * {
        box-sizing: border-box;
    }
    body {
        background-color: #474e5d;
        font-family: Helvetica, sans-serif;
    }
    
    /* The actual timeline (the vertical ruler) */
    .timeline {
        position: relative;
        max-width: 1200px;
        margin: 0 auto;
    }
    
    /* The actual timeline (the vertical ruler) */
    .timeline::after {
        content: '';
        position: absolute;
        width: 6px;
        background-color: white;
        top: 0;
        bottom: 0;
        left: 50%;
        left: 200px;
        margin-left: -3px;
    }
    
    /* Container around content */
    .container {
        padding: 10px 40px;
        position: relative;
        background-color: inherit;
        width: 50%;
        background-color: #0b7d6c;
    }
    
    .year-wrapper {
        position: absolute;
        height: 100%;
        z-index: 1;
        top: 0;
        padding-top: 15px;
        right: calc(100% - 12px);
    }
    
    .year-wrapper .year-item {
        position: sticky;
        top: 5px;
        z-index: 99;
        gap: 20px;
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
    }
    
    .year-wrapper h2 {
        background: red;
        margin: 0;
        padding: 0;
        position: sticky;
        top: 5px;
        z-index: 99;
    }
    
    .year-wrapper span {
        display: block;
        width: 25px;
        height: 25px;
        background-color: white;
        border: 4px solid #ff9f55;
        border-radius: 50%;
    }
    
    /* Place the container to the left */
    .left {
        left: 0;
    }
    
    /* Place the container to the right */
    .right {
        left: 50%;
        left: 200px;
    }
    
    /* Add arrows to the left container (pointing right) */
    .left::before {
        content: ' ';
        height: 0;
        position: absolute;
        top: 22px;
        width: 0;
        z-index: 1;
        right: 30px;
        border: medium solid white;
        border-width: 10px 0 10px 10px;
        border-color: transparent transparent transparent white;
    }
    
    /* Add arrows to the right container (pointing left) */
    .right::before {
        content: ' ';
        height: 0;
        position: absolute;
        top: 22px;
        width: 0;
        z-index: 1;
        left: 30px;
        border: medium solid white;
        border-width: 10px 10px 10px 0;
        border-color: transparent white transparent transparent;
    }
    
    /* Fix the circle for containers on the right side */
    .right::after {
        left: -16px;
    }
    
    /* The actual content */
    .content {
        padding: 20px 30px;
        background-color: white;
        position: relative;
        border-radius: 6px;
    }
    
    /* Media queries - Responsive timeline on screens less than 600px wide */
    @media screen and (max-width: 600px) {
        /* Place the timelime to the left */
        .timeline::after {
            left: 31px;
        }
    
        /* Full-width containers */
        .container {
            width: 100%;
            padding-left: 70px;
            padding-right: 25px;
        }
    
        /* Make sure that all arrows are pointing leftwards */
        .container::before {
            left: 60px;
            border: medium solid white;
            border-width: 10px 10px 10px 0;
            border-color: transparent white transparent transparent;
        }
    
        /* Make sure all circles are at the same spot */
        .left::after,
        .right::after {
            left: 15px;
        }
    
        /* Make all right containers behave like the left ones */
        .right {
            left: 0%;
        }
    }
    .content {
        position: relative;
    }
    <div class="timeline">
      <div class="container right">
        <div class="year-wrapper">
          <div class="year-item">
            <h2>2017</h2>
            <span></span>
          </div>
        </div>
    
        <div class="content">
          <p>
            1. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit
            voluptua dignissim per, habeo iusto primis ea eam.
          </p>
        </div>
      </div>
    
      <div class="container right">
        <div class="year-wrapper">
          <div class="year-item">
            <h2>2016</h2>
            <span></span>
          </div>
        </div>
    
        <div class="content">
          <p>
            1. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit
            voluptua dignissim per, habeo iusto primis ea eam.
          </p>
          <p>
            2. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum.
          </p>
          <p>
            3. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum. orem ipsum dolor sit amet, quo orem ipsum dolor sit amet, quo
          </p>
          <p>
            4. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum.orem ipsum dolor sit amet, quo
          </p>
          <p>
            4. Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum.
          </p>
        </div>
      </div>
    
      <div class="container right">
        <div class="year-wrapper">
          <div class="year-item">
            <h2>2015</h2>
            <span></span>
          </div>
        </div>
    
        <div class="content">
          <p>
            Lorem ipsum dolor sit amet, quo ei simul congue exerci, ad nec admodum perfecto
            mnesarchum, vim ea mazim fierent detracto. Ea quis iuvaret expetendis his, te elit
            voluptua dignissim per, habeo iusto primis ea eam.
          </p>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search