Sticky positioning is not maintaining its position at the top

I've noticed a strange behavior where, when I scroll down, the yellow box goes on top of the blue box. I have set both boxes to have a position sticky so that they should stay in place and not overlap. However, I want only the orange box to be scrollable when I scroll down. Can anyone help with this issue?

https://i.sstatic.net/EsZuS.png

code to try:

https://codesandbox.io/s/example-react-hooks-usestate-forked-69suro?file=/src/index.js

code:

import React from "react";
import ReactDOM from "react-dom";
import "./app.css";

function App() {
  return (
    <div className="box-container">
      <div className="box"></div>
      <div className="both">
        <div className="left__side"></div>
        <div className="right__side">
          <div className="oikea__box"></div>
          <div className="oikea__box"></div>
          <!-- more oikea__box divs -->
        </div>
      </div>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
.box {
  height: 100px;
  border: 1px solid black;
  width: 100%;
  background-color: blue;
  position: sticky;
  top: 0;
}

.both {
  display: flex;
}

.left__side {
  height: 200px;
  border: 1px solid black;
  width: 100px;
  background-color: yellow;
  position: sticky;
  top: 0;
}

.right__side {
  border: 1px solid black;
  width: 100%;
  background-color: white;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}

.oikea__box {
  height: 100px;
  width: 100px;
  border: 1px solid red;
  background-color: orange;
}

Answer №1

When dealing with the yellow box, it's important to note that using top:0 will not position it correctly relative to the blue box. In order to place it beneath the blue box, you should use top: (height of blue box in pixels), which is typically 100px.

.left__side: {
top: 100px;
}

Answer №2

Follow these steps:

top: 100px;

This code should be included in the CSS file for .left__side.

Answer №3

Your "both" class is conflicting with the blue box. No need to add "sticky" to the yellow box as flex automatically positions it after the blue box.

Consider using the following CSS:

.box {
  height: 100px;
  border: 1px solid black;
  width: 100%;
  background-color: blue;
}

.both {
  display: flex;
  flex-direction: row;
  overflow: scroll;
}

.left__side {
  height: 200px;
  border: 1px solid black;
  width: 100px;
  background-color: yellow;
}

.right__side {
  border: 1px solid black;
  width: 100%;
  background-color: white;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  height: 200px;
  overflow-y: scroll;
}

.oikea__box {
  height: 100px;
  width: 100px;
  border: 1px solid red;
  background-color: orange;
}

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

What are the best ways to enhance performance for ajax requests using Jquery?

Currently, I am in the process of developing a mobile application that utilizes jquery mobile, jquery, and a PHP backend. My issue arises when dealing with certain pages as there are numerous ajax requests being sent and received simultaneously, resulting ...

JavaScript library that provides SQL-like functionality for easily manipulating arrays

After successfully parsing a csv file into an array of objects, I find myself in need of making multiple alterations to that array. Currently, I am utilizing lodash methods for this task, but I am finding it to be quite verbose. I am considering working wi ...

AngularJS ng-repeat filtering by date is there any solution?

I have a ng-repeat loop with dates in the format below: <small>Added: <strong>{{format(contents.completeddate)}}</strong></small> I am using a datepicker plugin that provides me with 2 objects - a start date and an end date. For ...

How can I check if response.Text matches a specific String in an ajax call?

I am facing an issue where I am receiving data from a servlet in an AJAX function. Within this function, I am attempting to compare the response.Text with a certain String value, for example 'x'. However, the comparison is not working as expected ...

PHP may not be the only language that deals with website security concerns. This question on website security extends beyond just PHP and

Let's imagine we have files named "index.htm" and "routines.php". In the scenario, "index.htm" will eventually reach out to "routines.php" using JavaScript (AJAX). Now, here is the query: how can "routines.php" confirm that the request originated fr ...

What is the best way to include multiple entries in a select2 form using ajaxform?

select2/3.5.2/ I had to repost this because my initial post was not formatting correctly. The tools in use are: A select2 form field that allows searching through multiple records A bootstrap popup modal containing a form for entering a new record if i ...

Pressing the button results in no action

I am currently developing a program that randomly selects 5 words from a database and inserts them into an array. Although the page loads correctly initially, nothing happens when the button is clicked. None of the alerts are triggered, suggesting that the ...

How can I prevent mouse movement hover effects from activating in a media query?

I have implemented a custom image hover effect for links in my text using mousemove. However, I want to disable this feature when a specific media query is reached and have the images simply serve as clickable links without the hover effect. I attempted ...

The background image and svgs are not displayed on laravel localhost/public/index.php, but they appear when using "php artisan serve"

I've created a beautiful Laravel project on my PC. The welcome.blade.php file in the project has a background image located in the: public/images/ directory. To display the images, I've used the following CSS: html, body { color: #f4f6f7; ...

Creating a toggle link with 5 different states in coding

I have an HTML table where certain TDs contain inline styles, title tags, and links. I am looking to implement a feature where users can hide these elements by clicking the same icon multiple times. For example, on the first click, only styles are removed, ...

Find the current value of Animated.Value in React Native

I'm attempting to create an animation using interpolate on a View element. My goal is to access the current value of my Animated.Value, but I'm unsure how to do so. The information provided in the React Native documentation was not clear to me. ...

Updating previous values in reactjs to a new state

I have a div set up to render multiple times based on data retrieved from the database. The background color of the div corresponds to the ID received from the backend. My goal is to change the background color of the currently selected div and remove the ...

Using Angular material to display a list of items inside a <mat-cell> element for searching

Can I use *ngFor inside a <mat-cell> in Angular? I want to add a new column in my Material table and keep it hidden, using it only for filtering purposes... My current table setup looks like this: <ng-container matColumnDef="email"> < ...

JavaScript Date behaving oddly

While working with Javascript, I encountered a puzzling issue involving the Date object. date1 = new Date(1970, 1, 1); date2 = new Date("1970-01-01T13:00:00.000Z"); console.log(date1.getYear()); //70 console.log(date1.getMonth()); //1 console.log(date1.g ...

Uncertain about navigating the Ionic update

I have been actively working with Ionic 3 on multiple projects, but now I find myself in a position where upgrading to Ionic 5 is necessary to stay current. The process of upgrading to Ionic 5 seems daunting as it requires significant rework of my existin ...

The AJAX success function is operational, yet the file does not execute

As a newcomer to StackOverflow and web development, I am pressed for time with this assignment, so please bear with me if I struggle with understanding web development terminologies. My current challenge involves calling another php file through ajax in my ...

Obtaining JSON Data Using WinJS.xhr():

I'm encountering difficulties with retrieving chat messages using winjs.xhr: function getMessage() { var time = MESSAGE_RETURNED.unixtime; if (time == 0) { time= window.parent.SESSION.unixtime; } WinJS.x ...

What could be causing the delay in the execution of Redux dispatch?

When I open a file and parse it, the dispatch into redux state takes longer than anticipated. It seems like the dispatch itself is taking several hundred milliseconds more than the reducer. After opening and parsing the file, the following dispatch is mad ...

Numerous intersecting lines on display within Google Maps

Currently, I am working on displaying multiple flight routes on Google Maps. I have implemented polylines with geodesic to achieve this functionality successfully. However, a challenge arises when more than two flights intersect the same route, causing o ...

How can you make a Tempory Drawer wider in Material Components Web?

Increasing the Width of a Temporary Drawer in Material-components-web Greetings! I am currently facing an issue with Material-components-web In the provided demo here, I would like to extend the width of the Temporary drawer to accommodate additional co ...