What are the steps to implement a horizontal scroll feature on a website when the scroll width is set to 0?

My goal is to enable users to scroll in the web view similar to how they can drag to scroll on mobile devices

Check out my sandbox here for reference

I have created a sample sandbox with the following code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>

  <style>
    .wrapper {
      display: flex;
      gap: 100px;
      overflow: scroll;
    }

    .wrapper::-webkit-scrollbar {
      width: 0px;
    }
  </style>
  <body>
    <div class="wrapper">
      <div style="width: 50px;">box 1</div>
      <div style="width: 50px;">box 2</div>
      <div style="width: 50px;">box 3</div>
      <div style="width: 50px;">box 4</div>
      <div style="width: 50px;">box 5</div>
      <div style="width: 50px;">box 6</div>
      <div style="width: 50px;">box 7</div>
      <div style="width: 50px;">box 8</div>
      <div style="width: 50px;">box 9</div>
    </div>
  </body>
</html>

Essentially, I utilized overflow: scroll; and customized scrollbar width within wrapper::-webkit-scrollbar


However, I am seeking a method to replicate the mobile scrolling behavior in web without a visible scrollbar

I want my horizontal UI to remain sleek and neat while still allowing for dragging interaction on the web

I noticed that Netflix uses navigation arrows in their web interface. Is this the only way to implement horizontal scrolling on the web?

Answer №1

Desktop users who have a mouse can easily scroll horizontally without scrollbars by using the mouse wheel. Simply hover over the scrollable element, hold down the shift key, and scroll using the mouse wheel.

To simulate touch-screen drag scrolling, JavaScript can be used. Here is an example:

let pointerStart = 0;
let elementStart = 0;

const scrollableElement = document.querySelector('.wrapper');

const onDrag = (event) => {
  if (event.pointerType == 'mouse') {
    scrollableElement.scrollLeft = elementStart - event.clientX + pointerStart;
  }
};

scrollableElement.addEventListener('pointerdown', (event) => {
  if (event.pointerType == 'mouse') {
    isPointerDown = true;
    pointerStart = event.clientX;
    elementStart = scrollableElement.scrollLeft;
    
    document.addEventListener('pointermove', onDrag);
  }
});

document.addEventListener('pointerup', (event) => {
  if (event.pointerType == 'mouse') {
    document.removeEventListener('pointermove', onDrag);
  }
});
    
.wrapper {
  display: flex;
  gap: 100px;
  overflow: scroll;
}

.wrapper::-webkit-scrollbar {
  width: 0px;
}
<div class="wrapper">
  <div style="width: 50px;">box 1</div>
  <div style="width: 50px;">box 2</div>
  <div style="width: 50px;">box 3</div>
  <div style="width: 50px;">box 4</div>
  <div style="width: 50px;">box 5</div>
  <div style="width: 50px;">box 6</div>
  <div style="width: 50px;">box 7</div>
  <div style="width: 50px;">box 8</div>
  <div style="width: 50px;">box 9</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experience the seamless transition of new CSS animations

I designed a basic HTML game where a moving box disappears when clicked on. However, the animation that follows does not originate from the click location but rather from the original position. I believe the remove @keyframes at 0% should be based on the ...

Issues with SVG Image Mask in Firefox and IE: How to Fix Them

Creating a mask with a PNG (black circle, transparent background) and using -webkit-mask-image:url(images/mask.png) has been easy for browsers like Chrome. However, I've been encountering significant difficulties in getting the mask to display properl ...

What causes the CSS width property to vary when using the display:inline property?

There is a piece of code that contains a <ul> element with the default css property display:block. Here is the code snippet: ul,li{ list-style-type:none; padding:0; /*display:inline;*/ } #parent{ width:100%; /*height:50px;*/ background-color ...

Retrieving the selected value from a dropdown menu without having to click on a

I'm facing an issue with 2 dropdown menus outside of an HTML table that is generated by PHP code. There's a submit button within this table, and here's how it's set up: <!doctype html> Select Year: ...

What is the best way to incorporate visual indicators into specific columns containing certain keywords?

Is there a way to customize the script code responsible for displaying table data so that icons automatically appear next to specific keywords in the Gender column? For instance, can we have an icon like glyphicon glyphicon-heart-empty for Male and glyphic ...

My attempt to showcase data from a database in a grid layout using PHP and HTML seems to be resulting in a vertical display instead

My code is supposed to display a grid of database information, but for some reason it keeps stacking everything vertically instead of organizing it into rows and columns like it should. I've been staring at this code all day and I just can't figu ...

Eliminate the hover effect from every element

Is there a method in CSS or Javascript that allows me to eliminate the hover effect on all elements? I am specifically looking for a solution that will disable the hover effect on mobile devices while keeping it intact on desktop. I attempted using pointer ...

When I hover over div 1, I am attempting to conceal it and reveal div 2 in its place

I need help with a CSS issue involving hiding one div on mouse hover and showing another instead. I attempted to achieve this using pure CSS, but both divs end up hidden. Would jQuery be necessary for this task? Below is the CSS/HTML code I wrote: .r ...

The CSS property `touchmove pointer-events: none` appears to have a malfunction on Chrome for Android version 4.4 / ChromeView

For my project, I am utilizing CSS pointer-events to allow touchmove events to pass through a transparent div. This method has been effective on most platforms except for Chrome on Android. I am curious if this is a known issue with Chrome and if there are ...

How to stop font-family and letter spacing from affecting the margin of div and ul elements in CSS

I'm facing a challenge with aligning a rotated div and an unordered list in a container. Adjusting the margin of either element does the trick, but changing font-family or letter-spacing globally affects the spacing between the two. Check out this JS ...

Is there a way to change the color of a specific tab without affecting the content within it

I am attempting to change the color of an individual tab using jQuery. However, when I set the background attribute in CSS, it colors the entire background instead. What specific property should I be setting to only change the color of the tab itself? ...

Are the nav, footer, header tags not functioning correctly?

Hi everyone, I'm a newcomer to StackOverFlow! I have a question about the necessity of tags like Nav, footer, header, etc. Do they serve any purpose other than indicating what each section is about? I've heard they might be good for SEO rankings ...

What is the best way to implement a hover effect on each direct child individually using CSS?

I have been attempting to create a tab bar with hover effects on its direct children, but I am facing some challenges. Below is the code snippet I have so far, which aims to apply an effect to each element inside the list individually. HTML code : < ...

Tips on updating an HTML page following the receipt of a CURL request by a PHP script

I am currently developing a unique login system with customized requirements. The HTML form will submit data to a PHP script using AJAX, which will then forward the information to another PHP script for processing through CURL. After some time has passed ...

Styling text in table cells using only CSS, without the use of scripting, based on the values within those cells

I have scoured the forum and came across several older threads that suggest it is not achievable with just CSS (the system I am utilizing does not support scripts). Is there a possibility that this has changed or if someone else has an alternative idea? W ...

Switching JSON data when clicked in Angular 2

Looking for a way to dynamically switch between different data sets in JSON upon user interaction with UI buttons. I am utilizing Angular 2 framework for this task. The HTML structure is as follows: <button type="button" (click)="changeOdds('odds ...

Tips on utilizing a single modal to showcase various images

Based on the database table rows, my page will be populated with multiple rows (currently 32). Each row includes an image and a button/link at the end. I need a popup (#modal) to display the image fetched from the database when the button is clicked. I wa ...

Troubleshooting border-left and td alignment problems in an HTML email displayed in Outlook 2013

Currently, I am facing a frustrating challenge while working on an HTML email signature. It seems to display perfectly in all email clients except for Outlook 2007, 2010, and 2013. The specific issue I'm encountering is with the alignment of the secon ...

Updating values using jQuery when tags in a single table row are changed

In my current setup, I have a table structured as follows: <table> <tbody> <tr> <td> <input type="text" class="identifier" /> </td> <td class="name"> Some value < ...

Is it better to have JavaScript and HTML in separate files or combined into a single HTML file?

Do you notice any difference when coding in both HTML and JavaScript within a single .html file compared to separating HTML code in a .html file and JavaScript code in a .js file? Does the functionality remain the same in both scenarios? For example, in t ...