Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
CSS to show only horizontal and vertical scroll bar in div

CSS to show only horizontal and vertical scroll bar in div

22 Dec 2023
Intermediate
19.1K Views
1 min read
Learn with an interactive course and practical hands-on labs

HTML & CSS Course

Sometimes we need to add scroll bar to a div or span whenever text in the div or span get overflow. scroll bar is supported in all browsers like IE 5+, FF 3.5+, and Safari, Opera etc. To show scroll bar, we need to specify overflow property of css to "visible" or "auto".

Horizontal and vertical scroll bar

 .scrollbar
{
overflow: auto; /*for horizontal and vertical scroll bars */
}
/* or you can set scrolling in x and y direction */
.scrollbarxy
{
overflow-x: auto; /*for horizontal scroll bar */
overflow-y: auto; /*for vertical scroll bar */
} 

Only, Horizontal scroll bar

 /* Show only horizontal scrollbar */
.hscrollbar
{
overflow-x: auto; /*for horizontal scroll bar */
overflow-y: hidden; /*for hiding vertical scroll bar */
} 

Only, Vertical scroll bar

 /* Show only vertical scrollbar */
.vscrollbar
{
overflow-x: hidden; /*for hiding horizontal scroll bar*/
overflow-y: auto; /*for vertical scroll bar*/
} 

Note

  1. To show scroll bar always set overflow to "visible". It will show scroll bar whether text get overflow or not.

  2. To show scroll bar only, when text get overflowed, set overflow property to "auto".

Summary

In this article I try to explain, how you can scroll overflow text in div or span using css. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this