What is the best way to ensure that pagination remains in a static position?

My task tracker allows me to show 4 tasks per page, with additional tasks moving to the next page using React pagination when there are more than 4.

I need the page numbers to remain fixed in position and not move. For example, if I have 2 pages of tasks - the first with all 4 tasks and the second with 2 tasks - I want the pagination to stay right under the last task without shifting.

Task component

const Task = ({ tasks, setTasks, completeTask, removeTask }) => {
  const [pageNumber, setPageNumber] = useState(0);

  const tasksPerPage = 4;
  const pagesVisited = pageNumber * tasksPerPage;

  const displayTasks = tasks
    .slice(pagesVisited, pagesVisited + tasksPerPage)
    .map((task, index) => {
      return (
        <div
          className={task.isComplete ? 'todo-row complete' : 'todo-row'}
          key={index}>
          <div key={task.task_id} onClick={() => completeTask(task.task_id)}>
            {task.description}
          </div>
          <div className='icons'>
            <RiCloseCircleLine
              onClick={() => removeTask(task.task_id)}
              className='delete-icon'
            />
          </div>
        </div>
      );
    });

  const pageCount = Math.ceil(tasks.length / tasksPerPage);

  const changePage = ({ selected }) => {
    setPageNumber(selected);
  };

  return (
    <div>
      {displayTasks}
      <ReactPaginate
        previousLabel={'Back'}
        nextLabel={'Next'}
        pageCount={pageCount}
        onPageChange={changePage}
        containerClassName={'paginationBttns'}
        previousLinkClassName={'previousBttn'}
        nextLinkClassName={'nextBttn'}
        activeClassName={'paginationActive'}
      />
    </div>
  );
};

CSS styling

.paginationBttns {
  list-style: none;
  display: flex;
  justify-content: center;
  margin-top: 18px;
  padding-left: 0;
  color: white;
  bottom: 12px;
}

.previousBttn, .nextBttn {
  text-decoration: none;
}

.paginationBttns a {
  padding: 10px;
  margin: 8px;
  border-radius: 5px;
  border: 1px solid white;
  color: white;
  cursor: pointer;
}

.paginationBttns a:hover {
  background: #00adb5;
}

.paginationActive a {
  background: #00adb5;
}

To address this issue, I have created a code sandbox to demonstrate what is currently happening. How can I ensure that my pagination remains fixed in its position?

View Task Tracker Sandbox

Answer №1

One way to solve this issue is by initially assigning a min-height value to the wrapper's style, then using position:relative, and incorporating position:absolute to the pagination block. The code snippet below demonstrates this approach:

.wrapper {
  background: pink;
  padding-bottom: 5px;
  min-height: 300px;
  width: 100%;
  position: relative;
}

.paginationBttns {
  position: absolute;
  bottom: 0px;
  list-style: none;
  left: 50%;
  display: flex;
  justify-content: center;

  color: black;
}

EXAMPLE

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

Is it possible to achieve a 300 dpi resolution when printing an HTML page?

I've encountered an issue with my web page. It is encoded in HTML and CSS and designed to be the size of an A4 page (300dpi) at 2480 x 3508 pixels. However, when I try to print the page, it is printing in a resolution of 72/96 dpi and splitting into ...

How can I showcase the data obtained from an API call in React?

Having successfully created API's for a standard transaction application (using mongoDB for DB), I have API's set up for users, products, and orders. While I've been able to effectively use and display the information stored about users and ...

Is there a way to transform these into five columns within a single row using the Material-UI Grid system?

I'm trying to align 5 columns in one row, but I'm struggling to achieve the desired layout. Here is what I currently have: https://i.stack.imgur.com/d3z3n.png Any tips on how to make all columns appear in a single row? You can also view my att ...

"Exploring Bootstrap 3: How to Work with Grids and Organ

Check out my custom HTML code: <div class="container"> <div class="row"> <div class="col-sm-3"> <img src="http://placehold.it/274x175"> </div> <div class="col-sm-3"> <img src="http://placeh ...

Bootstrap - extra spacing

Utilizing bootstrap, I have implemented two div elements in my HTML code: <div class="container"> <div class="col-12 circle lg-circle"> <div class="col-12 circle sm-circle"> </div> </div> </div> The corresp ...

The content is failing to push the footer downwards on the webpage

My footer refuses to stay at the bottom of the content no matter what I've tried, from z-index to sticky footers. Check out this link for more information This is my CSS: #footer { position: absolute; clear: both; bottom:0; width:10 ...

Displaying threaded discussions in React

Below is an array that contains comments, and I am attempting to display them in a threaded manner by utilizing the parentId property. comments: [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, { id: 3 ...

Encountering problems while executing the command "npm run build."

When I run npm run build, I am encountering the following error: Creating an optimized production build... Failed to compile. HookWebpackError: Cannot read properties of undefined (reading 'e') -- inner error -- TypeError: Cannot read properties ...

What is the best way to invoke a child component function for selective parent components in Next.js?

Recently, a situation arose with a component named child.jsx. This component contains a button that triggers a function called handleClick when clicked. The challenge is that after importing this component into two others - parent1.jsx and parent2.jsx - I ...

Title appears to be left aligned instead of centered

My H1 is having trouble centering as I increase the font size. It positions correctly with a smaller size (30px), but when I increase the font, it becomes too low from the center even though text-align:center; is not helping to solve the issue. What adjust ...

Showing a collection of objects in a React component

**Recently started learning React and Node, and decided to fetch data into a functional component by following various tutorials. I successfully set up the server, connected it to the database, and fetched the data in React as per the tutorial instruction ...

A step-by-step guide to customizing the Material UI Chips delete SVG icon to appear in white color through

Using a Material UI component, I added a custom class to my chip. Attached is a screenshot showing what I mean. Currently, I am attempting to change the color of the cross button to white. After inspecting the element, I discovered that it is an SVG ico ...

Grid system that is responsive, while also maintaining a fixed column width

My problem seems simple, but I can't find an easy solution. What I need is to create a grid with fixed widths and a fixed distance between each column. Each column should be 400px wide, and as the browser window is resized, the grid should adjust by ...

Having trouble resolving React within the Formik/dist package due to a custom webpack configuration

Struggling to set up projects from scratch, encountering an issue with webpack not being able to resolve formik's modules while other third-party modules like styled-components work fine. I've tried searching online for a solution but couldn&apos ...

"ReactJS throws an error of 'Uncaught TypeError: undefined' for one scenario but works fine

I can't seem to understand why my console is throwing an "Uncaught TypeError" even though everything else in the object is functioning properly. Let me illustrate with this example: This is the data I am receiving: { "coord":{"lon":-117.8231,"lat ...

Ways to modify the gap between buttons using CSS

I am faced with a design challenge on my home page. I want to add some spacing between the buttons so they are not too close to each other, but for some reason margin-top and margin-bottom properties are not working as expected. Can you help me figure out ...

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

What is the best way to expand the navigation bar: should I add a side div or incorporate a background image?

I am encountering two issues that I need help solving. My first problem involves stretching out the navigation bar to fill the height and length of the adjacent div (div#textarea) while also keeping the text of the menu centered within that div. The seco ...

What is the best way to use identical styles for 2 different classes at separate breakpoints?

Is it possible to apply a chunk of (S)CSS to two different elements in two different breakpoints to avoid redundancy? I am using Bootstrap 4.6. <div class="one"> <div class="content">Something</div> </div> & ...

Troubleshooting issue with React mapping an array of items in a modal window

My state implementation is working perfectly fine, except for a minor issue with the modal window. Within the state, I have utilized objects that are normally displayed (you can refer to the screenshot here). Please pay attention to the "Open modal" butt ...