What is the best way to control the overflow length and display only a specific amount of content at once?

I am currently designing the homepage for a social media platform. I have around 45 dummy posts and I am looking for a way to limit the overflow in CSS so that only 5 posts are displayed at a time. Once the user reaches the bottom of those 5 posts, another 5 will be loaded onto the screen.

Below is the code snippet where I map over each post:

<div id='all-post-container' style={{position: 'absolute', top: '25%', left: '20%', backgroundColor: 'red', height: '72%', width: "54%", borderRadius: '30px', overflow: 'auto'}}>
      <Grid
        container
        direction="column"
        justifyContent="center"
        alignItems="center"
        spacing={45}
      >
      {arrayOfKeysForPost.map(key => {
          return (
            <Grid container item>
              <div id="post-container" style={{margin: '5%', position: 'absolute', width: '90%', height: '40%', backgroundColor: 'white', borderRadius: '30px', overflow: 'auto'}}>
              // Post content goes here
              </div>
            </Grid>
       )})}
      </Grid>
</div>

Here is an illustration showing how it currently looks with 45 posts... (Check the scrollbar on the right)

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

Answer №1

It is recommended to implement this functionality using JavaScript, fetching 5 posts initially and dynamically appending the next 5 as you scroll to the bottom of the page. However, if you prefer using CSS, you can leverage the pseudo selector nth-child.

your-component-className:nth-child(n+5) {
  display: none;
}

You can then update the number 5 with JavaScript once you reach the last post in the list.

The nth-child selector will hide posts starting from the fifth one.

Further details on the nth-child pseudo selector are available here

Answer №2

There are several methods to accomplish this:

  1. To hide all overflow content (assuming you do not want this), simply include overflow: hidden; in your CSS file.
#all-post-container {
    overflow: hidden;
}
  1. Loading the elements using jQuery You can load elements from a separate .html file. You could create one for each avatar and use the following code:
let post-max-count = 20
let posts = /* Use an npm package or module to retrieve all files in your user avatars directory as an array */
for (let _ = 0; post =  posts[_], _++) {
    if (_ < post-max-count) {
        $("#all-post-container").load(post)
    } else {
        break
    }
}

At the moment, I cannot think of any other ways to achieve this. However, I may come up with more solutions later on.

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 assess each item in an array and apply the useState() hook based on a specific condition

I am currently working on a simulation for a blackjack hand and have run into an issue with my code. The game follows these steps: users receive two random cards and a total point value, then click 'hit' to draw another random card from the deck. ...

Tips for building a dynamic view using Angular

I am working on a project where I need to dynamically generate multiple horizontal lines using data from a JSON file, similar to the example in the image below. Here is my attempt: Component.html <div style="text-align: center"> <div ...

Drift alongside a Division in HTML5

Struggling to make an Aside tag float alongside a Section tag using CSS. Here is the HTML: <div id="WholePage"> <section> <asp:ContentPlaceHolder ID="MainContentWindow" runat="server"> </asp:ContentPlaceHolder> ...

Another project cannot import the library that was constructed

I am in the process of creating a library that acts as a wrapper for a soap API. The layout of the library is structured like this: |-node_modules | |-src | |-soapWrapperLibrary.ts | |-soapLibraryClient.ts | |-types | |-soapResponseType.d.ts The libra ...

Material UI Grid's layout does not support placing a select element within a row

As a newcomer in the world of full stack development, I am delving into coding projects to enhance my understanding of frontend technologies like React JS and Material UI. My latest endeavor involves creating a page that displays posts from the backend in ...

Encountering unhandled promise rejection error with email field in NextJS when using React Hook Form

I am encountering a bizarre issue where, on my form with two fields (name and email), whenever the email field is focused and onSubmit is called, I receive the following error message: content.js:1 Uncaught (in promise) Error: Something went wrong. Please ...

Angular-Chart.js - Issue with Tooltip not displaying for data points with a value of 0

Within my Angular Chart JS Application, I have encountered an issue where having "0" values in my data array seems to affect the functionality of the tooltip for the corresponding bar. Interestingly, changing the "0" values to "0.1" resolves the problem... ...

Creating an empty input field with a predetermined default output in ReactJS

My current challenge involves navigating form data between various components in a React application. I am looking to pass data from child components to parent components and vice versa, creating a seamless flow of information. The key requirement is to ha ...

Display the menu item's name upon selection using react hooks

I have some data on my parent component structured like this: const filters =[ name1, name2, name3, etc ] I am trying to create a Select button with a dropdown list (using mui/material) based on the data received from props in my component. I have manage ...

Incorporate Stripe Elements into your Nuxt Js application

I recently managed to integrate Stripe into my React + Spring Boot application by following the guidelines provided in this documentation: https://stripe.com/docs/stripe-js/react. I used it in my React class component. Now, I am transitioning to Nuxt from ...

Receiving a 200 response code, however the controller's store() method is not being accessed

Recently, I made the decision to switch from using a form in a blade file to a Vue-based form that utilizes Axios for posting data. After making these changes, I encountered an issue where I receive a 200 response, but my controller's store() method i ...

What is the best way to effectively utilize Material UI breakpoints in combination with styled components?

Here is my code: const FooterBox = styled(Box)` background: #4e738a; left: 0; right: 0; bottom: 0; width: 100%; color: #ffffff; ${p => p?.theme?.breakpoints.up('xs')} { margin: auto; display: flex; flex-direction ...

Distinguishing div identifiers for jQuery displaying and concealing

Although I am not a coder, I am still learning and trying to improve my skills. I have multiple div elements listed by a MySQL query. I require a JavaScript code that can display the class="qform" div based on which button is clicked. If button-1 is clic ...

Toggle the current list item in Jquery to expand to 100% width, while simultaneously shifting any adjacent items on the left and right downwards if present

I am working with a list (ul) that contains multiple items displayed in a tiled format. Each item has a brief introduction and can expand to show more information when clicked on. What I would like to achieve is for the expanded item to take up the entire ...

Utilize fixed values in type declaration

Strange Behavior of Typescript: export type MyType = 0 | 1 | 2; The above code snippet functions correctly. However, the following code snippet encounters an issue: export const ONE = 1; export const TWO = 2; export const THREE = 3; export type MyType = O ...

What is the best way to determine the width of a CSS-styled div element?

Is there a way to retrieve the width of a div element that is specified by the developer? For example, using $('body').width() in jQuery will provide the width in pixels, even if it was not explicitly set. I specifically need to access the width ...

Server unable to redirect client to specified URL

Currently, I am in the process of developing a React application that necessitates Google login functionality. In order to also support Progressive Web App (PWA) capabilities down the line, I have decided to implement JWT for authentication. The issue I a ...

Utilizing Angular 2+ with the [innerHTML] property to incorporate HTML with added style attributes

I am currently utilizing Angular 2+ [innerHTML] input for inserting HTML formatting that includes style tags. Within my template, the code looks like this: <span [innerHTML]="someVar"></span> In the component file, I have defined: someVar = ...

Error message indicating that the function is not defined within a custom class method

I successfully transformed an array of type A into an object with instances of the Person class. However, I'm facing an issue where I can't invoke methods of the Person class using the transformed array. Despite all console.log checks showing tha ...

What is the best way to utilize XMLHttpRequest for sending POST requests to multiple pages simultaneously?

I have a unique challenge where I need to send data to multiple PHP pages on different servers simultaneously. My logic for sending the post is ready, but now it needs to be executed across various server destinations. var bInfo = JSON.stringify(busines ...