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

Retrieve input value in Angular 8 using only the element's ID

Can the value of an input be obtained in Angular 8 with TypeScript if only the element's id is known? ...

Tips on effectively rendering child components conditionally in React

My components currently consist of an AddBookPanel containing the AddBookForm. I am looking to implement a feature where the form is displayed upon clicking the 'AddBookButton', and hidden when the 'x' button (image within AddBookForm c ...

Tips for updating React context provider state when a button is clicked

WebContext.js import React, { createContext, Component } from 'react'; export const WebContext = createContext(); class WebContextProvider extends Component { state = { inputAmount: 1, }; render() { return <WebC ...

Unexpected resizing of a div due to Vue animation

I'm currently working on a quiz and I'm trying to incorporate some animation effects when users navigate between questions. I've been experimenting with a fade in and out effect, but there seems to be a glitch. Whenever I click the button, t ...

designing alternating rows within a table

Can I apply styles to alternating rows in an HTML table using only element hierarchy selectors and avoiding style names? I am tasked with styling the HTML output generated by a server component, which does not include styles for alternate rows. While I co ...

Issue with Passing 'key' Prop to React Component in a Mapped Iteration

https://i.sstatic.net/qeFKT.pngI'm struggling with adding a key prop to a React component in a mapped array within a Next.js project. The issue lies in a Slider component and an array of Image components being mapped. Even though I have provided a uni ...

The transparent DIV/waiting message may not be displayed in IE10

Here is a DIV container that serves as a transparent background with a loading message. This is the HTML code for the DIV container: <div id="generatingexcel" style="display:none; position: fixed; width: 100%;height: 100%; z-index: 999; top: 0; left: ...

React component making repeated calls to my backend server

As a React newbie, I am currently in the process of learning and exploring the framework. I recently attempted to fetch a new Post using axios. However, upon hitting the '/getPost' URL, I noticed that the GetPost component continuously calls the ...

``Considering the compatibility issues with position: absolute in Internet Explorer 8

Here's an example snippet of code in HTML: <form> <input type="file" id="iefile"> </form> <form> <input type="file" id="iefile"> </form> This is how it looks with some CSS styling: form{ position:rela ...

Issues with webpage responsiveness, width not adjusting correctly

Just completed coding my website and now I'm facing a new challenge. I am focusing on making it responsive for tablet screens (768px) starting with the header section, but for some reason, the modifications I make in the responsive.css file are not ta ...

Having trouble getting tailwind dark mode to work on next.js?

I have set up a custom boilerplate using next.js(10.0.5) with preact(10.5.12), typescript(4.1.3), and tailwind(2.0.2). I am attempting to incorporate a dark mode feature from Tailwind. I followed the instructions from next-themes in order to add the dark ...

Updating the redux store in React without concern for the cache

In my Node.js-React application with server-side rendering, I have successfully created the initial store in Redux and saved it as cache. Now, I need to update a specific value when the user loads my page. How can I achieve this? Currently, the process w ...

Issue with Aligning Text Inputs and Images in Firefox

I am really struggling with this issue and it's driving me crazy. The problem lies with an image positioned at the end of a text input, styled using CSS. Here is the code snippet: .text_input{ background:url(../images/forms/text_input_back.png) t ...

Ways to resolve the issue of the experimental syntax 'jsx' not being enabled

I encountered an issue while trying to implement react-fancybox and received an error. https://i.sstatic.net/vgFha.png To resolve the error, I executed the following commands: npm install --save-dev @babel/preset-react, npm install --save-dev @babel/plugi ...

Display a dynamic variable within React's HTML code

const fetchTime = () => { const currentDate = new Date(); const currentTime = currentDate + ' ' + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds(); return {currentTime}; } export default fun ...

What is the technique for using JavaScript to implement styling on a <td> within an HTML table?

Can someone help me with applying JavaScript to an entire column in an HTML table? Here is the script I am currently using: I want to insert a line break after each comma in the Message column of the table. Can anyone point out what I'm doing wrong? ...

How to properly align TableHeader and TableBody contents in a Material-UI table

I am experiencing an issue with a file that is supposed to display a table of data pulled from a database. Although the table does appear, all the data seems to be displayed under the ISSUE NUMBER column, instead of being aligned with their respective col ...

If the height of the window changes, then update the CSS height accordingly

Here is the code that I am using: $(document).ready(function() { $('body').css('overflow', 'scroll'); var heightWithScrollBars = $(window).height(); $('body').css('overflow', 'auto') ...

"Creating dynamic webpage designs with CSS for responsive layouts

Currently, I am in the process of building a website utilizing a CSS fluid layout approach which is rooted in adaptive and responsive web design principles. For this project, I am avoiding using fixed values such as pixels (px) and instead opting for perc ...

Encountering a TypeScript issue when integrating @material-tailwind/react with Next.js 14

Attempting to incorporate "@material-tailwind/react": "^2.1.9" with "next": "14.1.4" "use client"; import { Button } from "@material-tailwind/react"; export default function Home() { return <Button>Test MUI</Button>; } However, the button i ...