Hi đź‘‹,

Welcome To Infinitbility! ❤️

Today, we will learn to create scrollable div in react js using CSS with examples of vertically and horizontally scrollable.

So, we are Going to make a scrollable div 👇

Table of content

  1. Scrollable div vertically example
  2. scrollable div horizontally example

Scrollable div vertically example

To make a vertically scrollable div we have to write a fixed height of div and it will make a vertically scrollable div.

In the below example, I have added 👇 following CSS to make beautiful vertically scrollable div.

.scrollable-div {
  background-color: #f1f1f1;
  height: 150px;
  overflow: auto;
  margin: 20px;
  text-align: justify;
  padding: 20px;
}

And in react component

import "./styles.css";

export default function App() {
  return (
    <div className="App">
      <h1>Scrollable div vertically example</h1>
      <div className="scrollable-div">
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text ever
        since the 1500s, when an unknown printer took a galley of type and
        scrambled it to make a type specimen book. It has survived not only five
        centuries, but also the leap into electronic typesetting, remaining
        essentially unchanged. It was popularised in the 1960s with the release
        of Letraset sheets containing Lorem Ipsum passages, and more recently
        with desktop publishing software like Aldus PageMaker including versions
        of Lorem Ipsum. It is a long established fact that a reader will be
        distracted by the readable content of a page when looking at its layout.
        The point of using Lorem Ipsum is that it has a more-or-less normal
        distribution of letters, as opposed to using 'Content here, content
        here', making it look like readable English. Many desktop publishing
        packages and web page editors now use Lorem Ipsum as their default model
        text, and a search for 'lorem ipsum' will uncover many web sites still
        in their infancy.
      </div>
    </div>
  );
}

Output

scrollable div horizontally example

To make a horizontally scrollable div we have to write overflow-x: scroll; with white-space: nowrap; ( in the case of text content ) and it will make a horizontally scrollable div.

In the below example, I have added 👇 following css to make a beautiful vertically scrollable div.

.scrollable-div {
  background-color: #f1f1f1;
  overflow-x: scroll;
  white-space: nowrap;
  margin: 20px;
  text-align: justify;
  padding: 20px;
}

And in react component

import "./styles.css";

export default function App() {
  return (
    <div className="App">
      <h1>Scrollable div horizontally example</h1>
      <div className="scrollable-div">
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text ever
        since the 1500s, when an unknown printer took a galley of type and
        scrambled it to make a type specimen book.
        <br />
        It has survived not only five centuries, but also the leap into
        electronic typesetting, remaining essentially unchanged. It was
        popularised in the 1960s with the release of Letraset sheets containing
        Lorem Ipsum passages, and more recently with desktop publishing software
        like Aldus PageMaker including versions of Lorem Ipsum.
        <br />
        It is a long established fact that a reader will be distracted by the
        readable content of a page when looking at its layout. The point of
        using Lorem Ipsum is that it has a more-or-less normal distribution of
        letters, as opposed to using 'Content here, content here', making it
        look like readable English.
        <br />
        Many desktop publishing packages and web page editors now use Lorem
        Ipsum as their default model text, and a search for 'lorem ipsum' will
        uncover many web sites still in their infancy.
      </div>
    </div>
  );
}

Output

Thanks for reading…