Can you guide me on creating a horizontal scrolling feature using HTML, CSS, and React?

Struggling to create a horizontal scrolling component similar to Instagram stories? Here's what I've attempted so far:

.scroller{
  height: 125px;
  width: 300px;
  background-color: blue;
  overflow-x: scroll;
  overflow-y: hidden;
}
.itemContainer{
  display: flex;
}
.item{
  display: inline-block;
  width: 100px;
  height: 100px;
  background-color: lime;
  margin: 5px;
}
<div class="scroller">
   <div class="itemContainer">
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
  </div>
</div>

Experimented with flexbox properties and block elements but haven't found a solution yet for the item overflow issue.

Answer №1

Make sure to assign the flex items a flex-shrink: 0 property to prevent their width from collapsing when the container space is limited.

.scroller{
  height: 125px;
  width: 300px;
  background-color: blue;
  overflow-x: scroll;
  overflow-y: hidden;
}
.itemContainer{
  display: flex;
}
.item{
  flex-shrink: 0; /*added*/
  display: inline-block;
  width: 100px;
  height: 100px;
  background-color: lime;
  margin: 5px;
}
<div class="scroller">
   <div class="itemContainer">
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
  </div>
</div>

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

In regards to navigational bars that are oriented horizontally

Trying to create a simple navigation bar with horizontal links but facing issues. Can't seem to make it work despite following examples online: <!-- Navigator --> <div id="nav"> <ul> <li><a href="#"& ...

I seem to be having some trouble with my navigation roll-over menu bar, as

Help! I'm having trouble getting my navigation bar to properly overlap the main content on my website. If you could take a look here: that would be greatly appreciated. Please disregard the header position for now, as I am still deciding on its place ...

React Material UI unselect

I have been exploring a MUI select field implementation using this example as a guide. While I was able to make the code work, there are a few issues that I encountered. One of them is the inability to deselect a value once it has been selected. Additional ...

Switch on the warning signal using bootstrap

How can I make the alert below toggle after 2 seconds? <div class="alert alert-info"> <a href="#" class="close" data-dismiss="alert">&times;</a> Data was saved. </div> ...

Sending a websocket post request to a NODE server and receiving no response

I'm in the process of setting up a node server to act as an intermediary for my website. I am utilizing several libraries for this purpose. Axios - used for making requests to the API and fetching data from the database. Socket.io - employed to track ...

What is the process for creating a downloadable link or button?

Here's the scenario I'm dealing with: I have a dynamic website that includes a form with a "select" dropdown menu, followed by a link or button. When a user clicks on the link: If the selected option is "display," the data is shown using AJAX ...

Strange resizing behavior using automatic background sizing

Currently, I am facing an issue while trying to set a background image on my webpage with the auto background-size property. The problem arises when my image gets automatically resized. Even though my image has a width of 2501px, it is being resized to a ...

Sharing state between components in NextJS involves using techniques like Context API, passing

I am trying to pass state between pages in Next.js. In my App.js, I have wrapped it in a context provider like this: import { useRouter } from 'next/router' import { ClickProvider } from '../context/clickContext' function MyApp({ Compo ...

The function module.exportDefault does not exist

It seems like I am facing a unique issue with Meteor that I haven't come across before. Running on various versions like Meteor 2.0-beta.4, as well as Meteor 1.11 and 1.12, after setting up the app with a fresh checkout and executing "meteor npm inst ...

Generating a partially translucent image overlay

I am currently working on a one-page WordPress theme. Each section of the page includes a header with an image and an overlay. The header consists of a background image, with a wrap-width image inside. Below that, there is a semi-transparent png image tha ...

Sluggish Performance of Material UI Table

Hey there! I've been working with Material-UI for a large data table, but I've noticed it's quite slow. Before reaching out on Github, I wanted to see if other users have any tips or workarounds to help improve performance. Here is the code ...

Updating data without having to refresh the page is a useful feature when it comes to deleting a document on Firebase

Currently, I have a button that triggers this function to remove a value from Firebase. These values are being displayed in a flatlist on the same page. The function itself works perfectly fine, but to see the updated changes, I either need to refresh the ...

Ensure Next.js retains the route when moving from one screen to another

I am currently facing a challenge in Next.js while creating a Dashboard. The root route for this dashboard would be: /dashboard/ Within this route, users can select different stores to access their respective dashboards. When a user clicks on a specific s ...

Why does the Element.style.left property keep rejecting my input value?

I have encountered a problem with the positioning of elements, specifically with the 'left' property. I've designed a rectangular block using CSS and rotated it by 0.17 radians in JavaScript. Now, my aim is to make the block move diagonally ...

Leveraging JSON data to dynamically create HTML elements with multiple class names and unique IDs, all achieved without relying on

Recently, I've been working on creating a virtual Rubik's cube using only JS and CSS on CodePen. Despite my limited experience of coding for less than 3 months, with just under a month focusing on JS, I have managed to develop two versions so far ...

Is the return value of cache.readQuery in Apollo a copy of the data?

While studying the mutation documentation, a question arose regarding whether cache.readQuery returns a copied version of the data? For example: const { todos } = cache.readQuery({ query: GET_TODOS }); // Do I need to create a copy first, or is it alrea ...

Expanding Input Field to Fill Available Space Using Bootstrap

I have developed a sample here: jsFiddle <div class="container"> <div class="row"> <div class="span6"> <ul class="thumbnails"> <li class="span4"> <div style="height:200px; position: relative" ...

What are some tips for incorporating vue.js into a basic web application?

I've been trying to incorporate vue.js into my web application, but for some reason, it's not loading and the HTML is not firing in the browser. Please take a look at the code snippet below: <!DOCTYPE html> <html> < ...

Transfer properties to the children of this component

I'm a beginner with React and facing an issue when trying to pass custom props to this.props.children. I attempted using React.cloneElement, and while I can see the prop in the console.log within the class where I created it, it seems to get lost duri ...

Adjusting the size of content with CSS

I've been attempting to get an image to cover the entire screen on a device using CSS, but so far I haven't had any success. The image is within an :after pseudo element and content tag. Since it needs to be laid over the HTML, I can't use t ...