We often encounter this situation in front-end development, where we need to support scrolling while hiding the scroll bar. The easiest way to think of it is to add an iscroll plug-in, but in fact, CSS can also achieve this function now. I have used it in many places. Let's take a look at these three methods. Method 1: Calculate the scroll bar width and hide it In the sidebar of this site, you can see that there is no scroll bar for the front-end daily report, but you can scroll the content by moving the mouse over it. What is this technology? In fact, I just hid the scroll bar by positioning it. Demo Here is a simplified version of the code. <div class="outer-container"> <div class="inner-container"> ...... </div> </div> .outer-container{ width: 360px; height: 200px; position: relative; overflow: hidden; } .inner-container{ position: absolute; left: 0; top: 0; right: -17px; bottom: 0; overflow-x:hidden; overflow-y: scroll; } This code cleverly moves the scroll bar 17 pixels to the right, which is exactly the width of the scroll bar. I got this value by manual debugging. No problem found in Chrome and IE. Method 2: Use three containers to surround it, no need to calculate the width of the scroll bar I first saw this code on a Microsoft blog. It is similar to my idea above, except that they added an extra box inside to confine the content inside the box. This way you can scroll without seeing the scroll bar. The code is as follows: <div class="outer-container"> <div class="inner-container"> <div class="content"> ...... </div> </div> </div> //code from http://caibaojian.com/hide-scrollbar.html .element, .outer-container { width: 200px; height: 200px; } .outer-container { border: 5px solid purple; position: relative; overflow: hidden; } .inner-container { position: absolute; left: 0; overflow-x:hidden; overflow-y: scroll; } .inner-container::-webkit-scrollbar { display: none; } Method 3: Hide the scroll bar with CSS At the same time, the article also shares a method to hide the scroll bar through CSS, but this method is not compatible with IE and can only be used for mobile terminals. Chrome and Safari .element::-webkit-scrollbar { width: 0 !important } IE 10+ .element { -ms-overflow-style: none; } Firefox .element { overflow: -moz-scrollbars-none; } Summarize This concludes this article about how to use CSS to hide the scroll bar and scroll the content (three ways to achieve it). For more information about how to use CSS to hide the scroll bar and scroll the content, please search 123WORDPRESS.COM’s previous articles or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: Basic concepts and usage examples of HTML inline elements and block-level elements
>>: Vue global filter concepts, precautions and basic usage methods
Table of contents text 1. Prepare the machine 2. ...
JPQL stands for Java Persistence Query Language. ...
This article was originally written by blogger We...
Table of contents Conditional compilation Page La...
When you are working on a shared system, you prob...
This article is mainly for those who do not under...
Detailed explanation of tinyMCE usage initializat...
Table of contents (I) Using Workbench to operate ...
Recently, when I was learning Django, I needed to...
Big data continues to heat up, and if you are not...
Today we discussed the issue of what the margin v...
Experimental environment: Physical machine Window...
Table of contents 1. What is a prototype? 1.1 Fun...
Vim is a powerful full-screen text editor and the...
1|0MySQL (MariaDB) 1|11. Description MariaDB data...