Holi Sale. Get upto 40% OFF on Job-oriented 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
12.2K Views
1 min read

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 by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this