How to Make a Div Vertically Scrollable
Scrolls are a common part of websites. Learn How to Make a Div Vertically Scrollable with W3docs online tutorial. Try examples. Fast solution!
CSS allows us to make a <div> vertically scrollable. It can be easily done by using the overflow property. The <span class="property">overflow</span> property has different values. For example, <span class="property">overflow:</span> auto; enables scrolling on both axes when content overflows. Alternatively, using overflow-x: hidden; and overflow-y: auto; will hide the horizontal scrollbar and show a vertical scrollbar only when needed.
For a scrollable bar, use the x and y-axis. Set the <span class="property">overflow-x:</span> hidden; and <span class="property">overflow-y:</span> auto; to automatically hide the horizontal scrollbar and show a vertical scrollbar.
Let’s see an example, where the <div> is vertically scrollable.
Create HTML
- Place the
<h2>tag. Write some content in it. - Create a
<div>with the class "scroll".
how to create HTML <h2> and <div> elements inside an HTML <body> tag?
<body>
<h2>W3docs</h2>
<div class="scroll">
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>
</body>Add CSS
- Set the background-color, width, and height properties for the
<span class="property"> XFI4 </span>element. - Use the
<span class="property">overflow-x</span>property to specify whether the content must be hidden, visible or scrolling horizontally when the content overflows the element’s left and right edges. Set the "hidden" value. - Use the
<span class="property">overflow-y</span>property to specify whether the content must be hidden, visible or scrolling vertically when the content overflows the element’s top and bottom edges. Set the "auto" value. - Use the text-align property with its "center" value.
how to style an HTML element using CSS background-color, width, height, overflow-x,overflow-y and text-align properties?
div.scroll {
background-color: #fed9ff;
width: 600px;
height: 150px;
overflow-x: hidden;
overflow-y: auto;
text-align: center;
padding: 20px;
}Let’s bring the parts together and see the whole code!
Example of making a <div> vertically scrollable using the overflow-x and overflow-y properties:
An example of how to make a div vertically scrollable
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div.scroll {
background-color: #fed9ff;
width: 600px;
height: 150px;
overflow-x: hidden;
overflow-y: auto;
text-align: center;
padding: 20px;
}
</style>
</head>
<body>
<h2>W3docs</h2>
<div class="scroll">
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>
</body>
</html>Result
<div class="demo px-2.5 mt-1 mb-5 not-prose"> <div class="scroll">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> ### Example of making a <div> vertically scrollable using the overflow property:
An example of how to make a div vertically scrollable with the CSS overflow property
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div.scroll {
background-color: #fed9ff;
width: 600px;
height: 150px;
overflow: auto;
text-align: justify;
padding: 20px;
}
</style>
</head>
<body>
<h2>W3docs</h2>
<div class="scroll">
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>
</body>
</html>