Positioning elements within a Bootstrap column: Fixed versus Absolute positioning

Striving to maintain the absolute positioning of the right column as users scroll through the left column has proven to be a challenging task. Despite exploring various solutions from stackoverflow and google, none seem to effectively address this issue.

Below is the code snippet:

   <div className="wrapper wrapper-content animated">
          <div className="row">
            <div className="col-lg-4 col-xl-4">
              { this.props.showController ? <ModelChartController handleChange={ this.handleChange } handleSavedUntilRetirementChange={ this.handleSavedUntilRetirementChange } handleSavedAfterRetirementChange={ this.handleSavedAfterRetirementChange }
                                              {...this.state} /> : null }
            </div>
            <div className="col-lg-8 col-xl-8">
              <div className="absolute-wrapper">
                <div className="maintainPosition">
                  <ResponsiveContainer width='100%' aspect={ 21.0 / 9.0 }>
                    <LineChart data={ this.state.data } margin={ { top: 5, right: 30, left: 20, bottom: 5 } }>
                      <XAxis dataKey="Age" />
                      <YAxis dataKey="Total" />
                      <CartesianGrid strokeDasharray="3 3" />
                      <Tooltip/>
                      <Legend />
                      <Line type="monotone" dataKey="Non-Retirement" stroke="#8884d8" />
                      <Line type="monotone" dataKey="Retirement" stroke="#82aeca" />
                      <Line type="monotone" dataKey="Total" stroke="#82ca9d" activeDot={ { r: 8 } } />
                    </LineChart>
                  </ResponsiveContainer>
                </div>
              </div>
            </div>
          </div>
        </div>

The CSS used for styling is as follows:

.absolute-wrapper {
   position: relative;
}

@media (min-width: 768px) { 
  .maintainPosition {
      position: absolute;
      top: 0;
  }
}

Answer №1

Initially, the use of .absolute-wrapper and .maintainPosition divs solely for positioning purposes seems unnecessary (unless they serve other style-related functions).

The two columns align as "col-lg-4 col-xl-4" (left column) and "col-lg-8 col-xl-8" (right column), making it ideal to apply CSS directly to these divs.

To achieve this, consider adding an additional class name or assigning an ID to the respective divs:

Appending a Class Name:

<div className="col-lg-4 col-xl-4 leftColumn">
...
</div>

<div className="col-lg-8 col-xl-8 rightColumn">
...
</div>

CSS for Classes:

.leftColumn {}
.rightColumn {
 position: absolute;
 right: 0;
 top: 0;
}

Adding an ID:

<div className="col-lg-4 col-xl-4" id="leftColumn">
...
</div>

<div className="col-lg-8 col-xl-8" id="rightColumn">
...
</div>

CSS for IDs:

#leftColumn {}
#rightColumn {
 position: absolute;
 right: 0;
 top: 0;
}

If the above methods do not yield the desired outcome, you may want to consider using position: fixed; (relative to the viewport) instead of position: absolute; (relative to the parent element). Further information on this topic can be found here: https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/

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 is the best way to alter the background-color of an element that is contained within a GatsbyJS component, depending on the specific page where the component is being utilized?

As a first-time GatsbyJS website builder, I am working on creating reusable components for my site. One of these components is the footer, and I have already structured it. Now, I have a dilemma - I want to change the color of a specific <div> within ...

Tips for properly utilizing "require" in application.css with the Skeleton framework

I am currently working on implementing the skeleton-rails CSS framework into my application. Within the app/views/layouts/application.html.erb file, I have created containers and a list nav that require styling. Following the guidelines provided by the ske ...

What is the process for transmitting information from a page to layout components using the NextJS 13 app router?

Currently, I am facing difficulties in transferring data from a child component to a layout component. My main struggle lies in dynamically rendering a "title" in the layout component. Here is a simplified version of the issue at hand. I am making use of t ...

Retrieving information from the API to populate a child component in Next.js

I have been developing a header component and here's the code snippet: import style from '../../styles/header.css'; import '../../styles/globals.css'; export default function Header({data}){ const [showMe, setShowMe] = useStat ...

Align audio and video elements in HTML5 using JavaScript

I am facing a situation where I have two files - one is a video file without volume and the other is an audio file. I am trying to play both of these files using <audio> and <video> tags. My goal is to make sure that both files are ready to pla ...

Is there a way to emphasize a particular day on the calendar obtained from the URL?

I have implemented FullCalendar functionality to enable users to select a specific date, retrieve data from a database, and then reload the page with that data. The page is updated with the selected date in the URL. However, when the page reloads, althoug ...

What is the best way to add query parameters to router.push without cluttering the URL?

In my current project, I am using NextJS 13 with TypeScript but not utilizing the app router. I am facing an issue while trying to pass data over router.push to a dynamically routed page in Next.js without compromising the clarity of the URL. router.push({ ...

Extension for Chrome - Personalized pop-up notification when page loads

I am looking to create a custom alert box that triggers on page load instead of the default one, which I find unattractive and possibly irritating for users. alert('hello'); My current approach involves the following code: manifesto.js "cont ...

Tips for creating a clickable A href link in the menu bar that triggers an accordion to open in the body when clicked - html

Is there a way to open the first accordion when clicking on the "open 1st accordion" link, and do the same for the second link? The accordions themselves work perfectly fine, I just need a way to trigger them from outside their scope by clicking on links i ...

React components not displaying when the URL changes using React Router

I'm facing an issue where the components are not rendering correctly for the given route in my application. The problem occurs when the application loads at the URL / and I click on the login button, which changes the URL from / to /login. However, th ...

Display when moving up and conceal when moving down

Is there a way to make the div appear when scrolling up and down on the page? I'm new to jquery and could use some assistance, please. ...

Implementing setInterval in ReactJS to create a countdown timer

I have been working on developing a timer application using React. The functionality involves initiating a setInterval timer when a user clicks a specific button. const [timer, setTimer] = useState(1500) // 25 minutes const [start, setStart] = useState( ...

Prevent Second Division from wrapping around floated Division

I am running into a minor issue with the positioning of a div in my current project. +------------------------+ |+-------+ ~~~~ TITLE| <--how can I prevent text from wrapping around || | ~~~~~~~~~~~~~ |\ on the left ...

How can I return from cloud functions to Firebase Hosting?

So, here's my idea: to improve the search engine optimization (SEO) of my Single Page Application (SPA), I plan on detecting web crawlers on the server side using headers. If a crawler is detected, I want to render the page with Puppeteer and return i ...

Ensure that the flex item expands to occupy the vacant space left when other child elements are deleted

I am facing an issue with multiple children inside a parent container that has the CSS properties display: flex and flex-direction: column. There is a toggle button on the webpage which removes one of the children when clicked. The problem I need to solv ...

Logging DOM elements with Electron's webview preload feature

Within my Electron program, I am utilizing a webview in my index.html file. The webview is equipped with a preloader, and my goal is to manipulate the DOM of the webview specifically, not just the index.html file. In my preloader code snippet below, the c ...

Error: The OOP class value for translateX in the Web Animation API is returning as undefined

I'm currently working on a basic animation project using JavaScript. I have utilized the Animation class from the Web Animation API. My goal is to create multiple instances of this class in order to animate different elements with varying values and r ...

Enhanced Interactivity: jQuery UI Dialog Button Transforms Position upon Mouse Hover

I am utilizing the jQuery UI dialog feature and implementing buttons using jQuery in the following manner: $("#151").dialog({ autoOpen: false, autoResize: true, buttons: { "OK": function ...

React and Express: Encounter a proxy error when trying to proxy the request for /total from localhost_errors

This question is a continuation of my previous inquiry on React.js: Axois Post stay on pending(but i am getting data) Below is the content of my package.json file { "name": "my-app", "version": "0.1.0", "private": true, "dependencies": { "ax ...

Arrange a div beneath another div that is positioned absolutely

Within my code, I have a div with the property of absolute positioning referred to as "abs". Right after this div, there is another div called "footer" which does not have any specified position value assigned to it. Even though I've experimented with ...