How to use CSS to make the second item in a flex container sticky

I'm working with a flex container that has two columns, where each column is also a flex container.

My goal is to make the second item in the side column sticky, so it remains visible even when scrolling down the page.

In my attempt, I used position: sticky;, but it doesn't stick all the way to the end of the page. What am I missing?

I've looked at solutions like the one on Stack Overflow, but I haven't been able to get it to work.

Check out the demo here: Demo

.page-container {
  display: flex;
  justify-content: space-evenly;
}

.column-container {
  max-width: 50%;
}

.item-wrapper  {
  display: flex;
  flex-direction: column;
}

.item {
  min-width: 150px;
  margin: 15px;
  padding: 30px;
  background: red;
  text-align: center;
}

.sticky {
  background: green;
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  align-self: flex-start;
}
<div class="page-container">
    <div class="column-container">
      <div class="item-wrapper">
        <div class="item">
          Item 1
        </div>
        <div class="item">
          Item 2
        </div>
        <div class="item">
          Item 3
        </div>
        <div class="item">
          Item 4
        </div>
        <div class="item">
          Item 5
        </div>
         ...
      </div>
    </div>
    <div class="column-container">
      <div class="item-wrapper">
        <div class="item">
          Side item 1
        </div>
        <div class="item sticky">
          Side item 2
        </div>
      </div>
    </div>
  </div>

Answer №1

The reason why the second column's inner wrapper does not extend to its full height is because the `sticky` property prevents the element from exceeding the boundaries of its container.

To address this issue, you can set the height to 100% by adding the following CSS code (I applied it using a class):

.page-container {
  display: flex;
  justify-content: space-evenly;
}

.column-container {
  max-width: 50%;
}

.item-wrapper  {
  display: flex;
  flex-direction: column;
}

.item {
  min-width: 150px;
  margin: 15px;
  padding: 30px;
  background: red;
  text-align: center;
}

.sticky {
  background: green;
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  align-self: flex-start;
}

.full-height {
  height: 100%;
}
<div class="page-container">
    <div class="column-container">
      <div class="item-wrapper">
        <div class="item">
          Item 1
        </div>
        <div class="item">
          Item 2
        </div>
        <div class="item">
          Item 3
        </div>
        <div class="item">
          Item 4
        </div>
        <div class="item">
          Item 5
        </div>
         ...
      </div>
    </div>
    <div class="column-container">
      <div class="item-wrapper full-height">
        <div class="item">
          Side item 1
        </div>
        <div class="item sticky">
          Side item 2
        </div>
      </div>
    </div>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When it comes to HTML5 drag and drop functionality, only the dragstart event is triggered

I have successfully implemented HTML5 drag&drop using jQuery UI, but now I want to switch to using pure HTML5 API. Currently, only the dragstart event is firing for me. I am not able to catch the rest of the events. However, everything seems to be fun ...

Innovative solutions for seamless transformation of HTML designs into Silverlight 3.0

One of my challenges with clients is getting them on board with web applications. However, I am eager to try something new and explore the potential of Silverlight 3.0. It would be great to see if I can transform some of these applications into fully funct ...

Eliminating unnecessary whitespace between the bottom of the document and an element

I have a HTML page that when scrolled should limit scroll to the document height. It must not exceed the bottom of an element. Here is a screenshot for better understanding: https://i.stack.imgur.com/RaVg8.jpg The scrolling should be limited after the En ...

What methods can we employ to engage with embedded HTML pages utilizing Selenium WebDriver?

Struggling to access elements within an embedded html page on a parent html file Hello, The website I am trying to automate includes an embedded html page within the main page. I have been facing difficulty interacting with the elements within the embed ...

Using Selenium to scrape eBay's website

Hello, I am completely new to coding and currently working on a project that involves scraping data from eBay using selenium. Throughout this process, I have encountered two main challenges. The issue arises with the XPath for the listing name, which ...

The onreadystatechange function is not triggering

For some reason, the onreadystatechange callback function is not triggering in asynchronous mode. After testing the post request in synchronous mode, it seems that the post itself is functioning correctly (the testing code used for synchronous mode has b ...

What is the best way to choose the next adjacent element using a CSS selector with Python Selenium?

The structure of the DOM is as shown below: <ul> <li> <a href="#" role="button" class="js-pagination link" data-page="1">1</a> </li> <li> <a href="#" role="button" class="js-pagination link active" data ...

What are some recommended techniques for designing a voting system that accommodates both registered and unregistered users?

In my latest project, I am in need of a voting script that will allow both registered and unregistered users to vote on content featured on my website. The goal is to ensure that each user, whether registered or not, can only vote once for the same piece o ...

What could be causing the show button to change its style every time I click it?

I'm a beginner in the world of JavaScript. I have been working on customizing the "show more" button and encountered an issue. I managed to add text and a down arrow below the button, but when I click it, another button labeled "I don't know how ...

I'm seeking assistance in identifying the issue with my form validation code. Could anyone lend a hand?

<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="./css/createanaccount.css"> <script src="https://kit.fontawesome.com/c90e5c3147.js" crossorigin=&quo ...

Changing font-weight to bold

Currently, I have been utilizing {font-weight:bold} to style an Arial font, however, it appears to be excessively thick. Is there a way to make it slightly lighter? I've attempted using {font-weight:400} in increments of 100, but the result remains un ...

Why are the HTML links generated by JS not opening in Chrome?

<a href='http://www.xyz.hu/xyz' alt='Kosár' title='Kosár'>Megtekintés</a> Additionally: - A setInterval function refreshes the sibling's content every second, although it should not affect this specific el ...

Having issues with extending multiple classes in a scss file?

I have integrated AdminLTE into my project, and within an html page I have the following code: <aside class="main-sidebar sidebar-dark-primary elevation-4"> <aside> Everything seems to be working fine. To customize the styling for m ...

How to use Angular to access the properties of an HTML tag from within the

Can someone help me with this html code: <label [class]="text === 'test1' ? 'class1' : 'class2'" text="test1"></label> <label [class]="text === 'test1' ? 'class1' : 'class2'" tex ...

Set the CSS attribute value to be equal to twice the value of another CSS attribute value

Currently, I am experimenting with jQuery to create a specific effect that has been presenting some challenges: My goal is to set a css attribute value to be equal to another css attribute value multiplied by 2. To elaborate further, I want the margin-le ...

What is the most efficient way to remove all typed characters from fields when clicking on a different radio button? The majority of my fields share the same ngModel on a separate page

Is there a way to automatically clear all typed characters in form fields when switching between radio buttons with the same ngModel on different pages? I noticed that the characters I type in one field are retained when I switch to another radio button. ...

Utilizing CSS to Display a Variety of Colors within a Text String

Is it possible to style different words in a sentence with various colors using only CSS, without using the span element? For instance, how can I make the word "Red" appear in red, "Blue" in blue, and "Green" in green within an h1 tag? ...

Divided Progress Indicators in Bootstrap 3

Looking to enhance user experience on my webpage by creating a progress bar for data entry. The goal is for users to enter 10 items before proceeding. The challenge I'm facing is developing a progress bar that accurately reflects the number of items ...

The issue of body height not functioning properly when using a skeleton

Greetings! This is my debut question on Stack Overflow, and I am hopeful that someone can provide me with some guidance. I have encountered a hiccup while working on my webpage using the Skeleton framework. The issue at hand is setting the "height" to 100 ...

What are the steps for applying a Bootstrap class to an element?

I keep encountering this error in the console: Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('si col-md-4') contains HTML space characters, which are not valid in tokens. Below is a ...