Home > Web Front-end > CSS Tutorial > How to Force a Vertical Scrollbar to Always Appear in CSS?

How to Force a Vertical Scrollbar to Always Appear in CSS?

Mary-Kate Olsen
Release: 2024-12-06 09:28:11
Original
222 people have browsed it

How to Force a Vertical Scrollbar to Always Appear in CSS?

How to Always Display Vertical Scroll Bar in CSS

In certain scenarios, you may want to ensure that a vertical scroll bar is always visible, even when a div's content doesn't extend beyond the visible area. By default, browsers hide scrollbars until the mouse hovers over an element.

To force a scrollbar to always display, add the following CSS:

:::-webkit-scrollbar {
  -webkit-appearance: none;
  width: 7px;
}

::-webkit-scrollbar-thumb {
  border-radius: 4px;
  background-color: rgba(0, 0, 0, .5);
  box-shadow: 0 0 1px rgba(255, 255, 255, .5);
}
Copy after login

The first line removes the default browser styling and the second line defines custom styling for the scrollbar thumb.

For example:

#div {
  position: relative;
  height: 510px;
  overflow-y: scroll;
  
  ::-webkit-scrollbar {
    -webkit-appearance: none;
    width: 7px;
  }
  
  ::-webkit-scrollbar-thumb {
    border-radius: 4px;
    background-color: rgba(0, 0, 0, .5);
    box-shadow: 0 0 1px rgba(255, 255, 255, .5);
  }
}
Copy after login

The above is the detailed content of How to Force a Vertical Scrollbar to Always Appear in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template