Sticky Position Not Functioning Properly in Flexbox Layout

I've attempted to use align-self: flex-start, but it had no effect. The main flex container is on the right side of my page (flex-direction: column) and I want one block of flex columns to stick once I scroll down to them. I enclosed a container around the specific flex columns I want to be sticky, giving that container a position sticky. Below is a snippet of my HTML and CSS for reference:

.rightside_container {
  display: flex;
  flex-direction: column;
  gap: 0px;
  margin-top: 60px;
  right: 17%;
  position: absolute;
  z-index: 0;
  border-radius: 10px;
  background-color: black;
  padding-left: 0px;
  padding-right: 20px;
  padding-top: 8px;
  padding-bottom: 8px;
}

.sticky_container {
  border: 1px solid white;
  position: sticky;
  top: 0px;
}
<div class="rightside_container">
  <!--big main container-->
  <div class="sticky_container">
    <!--since I want a few of my columns to be sticky, I wrapped
            a little div container around them which I gave the position sticky-->

    <div class="follow_flex">
      <!--COLUMN 1-->
      <div class="follow_pic_container">
        <img src="thumbnails/twitterlogo.jpg" class="follow_pic">
      </div>
      <div class="follow_text_container">
        <div class="follow_name">Twitter</div>
        <div class="follow_at">@Twitter</div>
      </div>
      <div class="follow_button_container">
        <button class="follow_button">Follow</button>
      </div>
    </div>

    <div class="follow_flex">
      <!--COLUMN 2-->
      <div class="follow_pic_container">
        <img src="thumbnails/youtubelogo.jpg" class="follow_pic">
      </div>
      <div class="follow_text_container">
        <div class="follow_name">YouTube</div>
        <div class="follow_at">@YouTube</div>
      </div>
      <div class="follow_button_container">
        <button class="follow_button">Follow</button>
      </div>
    </div>
  </div>
</div>

Answer №1

It seems like the code you provided is not sufficient, but by setting a height and removing the margin in your snippet, the sticky effect can be achieved. Keep in mind that if any parent element has overflow: hidden, the sticky positioning will not work.

.rightside_container {
    display: flex;
    flex-direction: column;
    gap: 0px;
    right: 17%;
    position: absolute;
    z-index: 0;
    border-radius: 10px;
    background-color: black;
    padding-left: 0px;
    padding-right: 20px;
    padding-top: 8px;
    padding-bottom: 8px;
    height: 600px;
    
}

.sticky_container {
    border: 1px solid white;
    position: sticky;
    top: 0px;
    
}
            
        <div class="rightside_container"> <!--big main container-->
          <div class="sticky_container"> <!--since I want a few of my columns to be sticky, I wrapped
            a little div container around them which I gave the position sticky-->
          
              <div class="follow_flex"> <!--COLUMN 1-->         
                  <div class="follow_pic_container">
                      <img src="thumbnails/twitterlogo.jpg" class="follow_pic">
                  </div>
                  <div class="follow_text_container">
                      <div class="follow_name">Twitter</div>
                      <div class="follow_at">@Twitter</div>
                  </div>
                  <div class="follow_button_container">
                      <button class="follow_button">Follow</button>
                  </div>               
              </div>

              <div class="follow_flex"> <!--COLUMN 2-->
                  <div class="follow_pic_container">
                      <img src="thumbnails/youtubelogo.jpg" class="follow_pic">
                  </div>
                  <div class="follow_text_container">
                      <div class="follow_name">YouTube</div>
                      <div class="follow_at">@YouTube</div>
                  </div>
                  <div class="follow_button_container">
                      <button class="follow_button">Follow</button>
                  </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

Adjust the arrangement of divs when resizing the window

I have utilized flexbox to rearrange the boxes. Here is a link to the demo code My issue arises when resizing for smaller screens; I need to place B between A and C. https://i.stack.imgur.com/M0dpZ.png Any suggestions on achieving this goal? Thank you i ...

Is there a way to stop all HTML5 videos except for the one that I want to watch?

On my webpage, I have three HTML5 videos all on one page. My goal is to be able to pause or mute all the videos at once, except for the one that I specifically choose. Initially, I tried using an event listener on the parent div which worked well when clic ...

Converting HTML's nested <ul> element into JSON format

I am working on developing a form builder application that allows users to create forms through drag and drop functionality. The main structure of my form is based on the <li> tag, with nested <li> and <ul> tags present underneath it. On ...

Enable sidebar navigation exclusively for mobile and iPad devices

Is there a way to implement a different navigation bar specifically for mobile devices like iPads? I'm considering using this one: Here is the source code: JSFiddle If anyone knows how to achieve this, please share! <link rel="shortcut icon" typ ...

What is the best way to accommodate 4 columns within a 3-column HTML table layout?

I am facing a challenge with my table layout. Normally, it has 3 columns, but I am trying to figure out how to fit 4 cells into one row while maintaining the same width. Any suggestions on how to achieve this? ------------- | 1 | 2 | 3 | ------------- | ...

Incorporating a new row in JQuery Datatable using an mdata array

I am currently using a datatable that retrieves its data through mData. var processURL="path" $.ajax( { type : "GET", url : processURL, cache : false, dataType : "json", success ...

Performing a validation on data fetched using a .load() function

I've been struggling with a persistent issue that I just can't seem to solve. So, here's the situation: I have a blog post on a CMS that I'm developing. The content is saved within a div with its own unique ID. When the user clicks an ...

Why is the outline buttons class only displaying gray colors for me?

I'm having trouble with the outline buttons class (btn btn-outline-primary) in Bootstrap. For some reason, all the buttons appear gray instead of colored as they should. Here's an example of how the buttons should look: https://ibb.co/ChKf83y, bu ...

The combination of HTML, CSS, and vertical text margins is essential for creating visually

Looking for some guidance on how html text and margins work? If there's a good online reference you can point me to, I'd greatly appreciate it! I'm currently struggling with setting precise margins for text. In the example below, there&apos ...

The iPhone 6 Plus's Safari browser, when in landscape mode, displays multiple tabs open at once. Additionally, when a Bootstrap modal

Encountering an issue with the bootstrap modal on iPhone Safari 6+ in landscape mode. This glitch only happens when using multiple tabs on the browser. Follow these steps to replicate the problem: 1) Visit http://getbootstrap.com/javascript/ on your iP ...

Restrict the input to only allow for parentheses, disallowing any letters or numerical characters

Only parentheses are allowed in the input field; letters and numbers will not be accepted. function checkBrackets() { var inputVal = document.getElementById("input").value; var result = document.getElementById("strong").value; console.log(inputVal, ...

Suggestions on how to refactor redundant code in various peer AngularJS controllers for handling $intervals

In my compact single-page application, I have implemented multiple tabs to display operational status information for different applications. Each tab is associated with a controller that creates $interval objects to make AJAX calls to fetch status data fr ...

Tips on adding CSS to a React component

I've successfully converted an HTML file into a component designed to resemble a 7-segment LED display. I have imported the necessary CSS styles from the index.css file, but I am unsure how to properly apply these styles within the component. My atte ...

In Javascript, when trying to call Firebase database child(), it may sometimes result in the return value being "

Here is the current setup: Firebase Database: setores: { -KkBgUmX6BEeVyrudlfK: { id: '-KkBgUmX6BEeVyrudlfK', nome: 'test' } -KkDYxfwka8YM6uFOWpH: { id: '-KkDYxfwka8YM6uFOWpH', nome ...

Positioning the div to align perfectly alongside the list items

My current objective involves dynamically creating div elements and populating them with items. A working demo of my progress can be found by following this link. The issue at hand is that the newly created div does not appear in the desired location, but ...

What is the best way to populate a dropdown menu in PHP?

I am trying to populate my combobox with names from an SQL database. I came across some code on W3schools, but I am having trouble integrating it into my own code. Currently, the combobox is filled with select, but that's not the intended functionalit ...

The ability to scroll horizontally for individual items within a larger container div

JSFIDDLE: http://jsfiddle.net/7zk1bbs2/ If you take a look at the JSFiddle project, you'll notice that the icons are placed inside an orange box. However, there is a vertical scroll needed to browse through them and I am attempting to enable horizont ...

Retrieve the content from an HTML table cell enclosed within a division tag

I am currently facing an issue where I need to extract the value from a specific div in order to proceed with the program. The value that I am trying to retrieve is '12345678'. <td id="TransaccionTesoreriaRetencionItems_NroComprobanteRete ...

Retrieve data quickly and navigate directly to specified div element on page

I am facing an issue with scrolling on my website. While it currently works fine, I would like to make the scrolling instant without any animation. I want the page to refresh and remain in the same position as before, without automatically scrolling to a s ...

Display the element only when the input is in a selected state using CSS

Looking for a way to display an element when an input field is selected without using JavaScript? Preferably, the solution should not involve placing the element within the input field. Unfortunately, targeting the element in CSS with the :active selector ...