Is there a way to achieve element scrolling similar to scrollLeft within a scrollable container without using JavaScript

Looking for a CSS Solution:

Is there a way to achieve element.scrollLeft functionality in CSS without using javascript? I want to pre-scroll a scrollable container.

Situation Overview:

I have a scrollable container that houses various items. Some instances of this container may overflow, and I would like the scroll offset to automatically be positioned at the end.

https://i.stack.imgur.com/iTmUu.jpg

The Teaser:

While I currently use javascript to manipulate the scroll position effectively, there is a perceptible jitter or sudden movement within the scrollable content. Can anyone suggest a pure CSS technique to preset the scroll position?

Check out Code Sandbox for sample code: https://stackblitz.com/edit/js-9b9wt5?file=index.html

const cardContainerEls = document.querySelectorAll('.card-container');
  cardContainerEls.forEach((el) => {
  el.scrollLeft = 1000;
});

Answer №1

Check out this interesting fiddle that allows for both horizontal and vertical scrolling using pure CSS without any JavaScript. Feel free to customize it to suit your specific requirements: https://jsfiddle.net/1Lfybv56/2/

HTML

<span id="top"></span>

<div id="anchors">
  <a href="#red">Red</a>
  <a href="#blue">Blue</a>
  <a href="#green">Green</a>
  <a href="#yellow">Yellow</a>
</div>

<div id="red"><a href="#top">Top</a></div>
<div id="blue"><a href="#top">Top</a></div>
<div id="green"><a href="#top">Top</a></div>
<div id="yellow"><a href="#top">Top</a></div>

CSS

html {
    scroll-behavior: smooth;
    white-space: nowrap;
}

#anchors a {
  display: block;
}

a {
    display: inline-block;
    background: #fff;
    padding: 4px;
}

div {
    display: block;
    height: 400px;
    width: 400px;
    padding: 4px;
    border: 2px solid grey;
    position: relative;
}

#red {
    background: red;
    left: calc(412px * 1);
}
#blue {
    background: blue;
    left: calc(412px * 2);
}
#green {
    background: green;
    left: calc(412px * 3);
}
#yellow {
    background: yellow;
    left: calc(412px * 4);
}

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

The use of jQuery.parseJSON is ineffective for a specific string

Why isn't jQuery.parseJSON working on this specific string? ({"stat":"OK","code":400,"data":[{"title":"Development Convention","event_type":false,"dates_and_times":[{"date":"28\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"dat ...

Troubleshooting AJAX, PHP, and mySQL Interactions

I am experiencing issues with my password change script on my website. I am utilizing an AJAX connection to my php file. While my database connection seems stable and variables are being sent correctly (based on firebug), they seem to disappear along the w ...

Mastering Light and Camera Selection in Three.js

Question, In the editor found at this link, you can click on a light or camera to select it. I am familiar with using raycaster.intersectObjects(objects) to select meshes, but how can I achieve the same result for lights and cameras which do not have mesh ...

Is it possible for a Bootstrap modal dialog to overlay another dialog window?

<button class="btn" onClick="$('#firstModal').modal('show');">Click Here</button> <!-- Modal --> <div id="firstModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden=" ...

The text is exceeding the boundaries of its container, displaying in a single line that extends beyond the page

I am currently working on incorporating text into the project-details section by utilizing a rich text uploading field in Django admin. Despite inputting the text as a paragraph in the Django admin project description, it displays as a single line that ove ...

Attempting to retrieve dynamically generated input fields and cross-reference them with structured length .json data

In the process of developing code, I've implemented a for loop to dynamically generate HTML input fields based on the length of some data in a .json file. My goal is to use jQuery to compare the text entered in each input field with the corresponding ...

Basic animation feature malfunctioning

So, I'm really new to this whole HTML5 canvas thing and I'm trying to make a pixel move across the screen with an additive tail behind it. The idea is for the tail to scroll away once it reaches a certain point while the pixel continues moving. I ...

Restrict access to PHP scripts to only be allowed through AJAX requests

Within my content management system (CMS), I have a specific page that fetches various mini-interfaces from PHP files located in an /ajax directory using AJAX. I am wondering if there is a way to restrict access to these files solely through AJAX requests ...

Issue with Angular 6: Animation with LESS is failing to work post deployment

Hello everyone, I'm facing an issue with my LESS animation. When I deploy my app on the server after using ng build --prod, the CSS animations are not visible in browsers. They work perfectly fine on localhost but fail to work after deployment and I c ...

C#: Issues with loading static content in MVC on various environments with IIS

Within my C# MVC 4.5 Website, I recently introduced a new folder to host CMS-type applications separately from the main site. While everything seemed fine during local testing, issues arose after deploying the updates to the DEV environment. A closer look ...

Staggered Drop-Down Menus

I've created a custom CSS drop-down menu that works well, but I've realized that updating this menu across all the HTML pages on my website would be a tedious task. If I ever need to add a new section and tier to the menu, I'd have to update ...

"Customizing FusionCharts: A step-by-step guide to changing the background color

Is there a way to modify the background color of fusionchart from white to black? Additionally, how can I change the font color in the chart? ...

What should the AJAX file in TagManager jQuery look like?

I'm currently utilizing Tagsmanager with JQuery, which can be found at There is a feature that allows tags to be pushed via Ajax: jQuery(".tm-input").tagsManager({ AjaxPush: '/ajax/countries/push', AjaxPushAllTags: true, ...

Ways to engage with a fixed html page generated by an Express router?

Let me start by mentioning that I am relatively new to working with NodeJs. Currently, I am dealing with a situation where I need to render a static HTML web page (which includes JavaScript code) as a response to a post request. Below is the relevant snipp ...

Getting files onto your device - the Phonegap way

I'm having trouble exporting or downloading information to a file. It works fine in my browser, but when I try it in my phonegap app, the file just opens as text without an option to save it or return to the app. Any advice? Keep in mind that I'm ...

I'm interested in developing a React function that generates recipe components based on a set of instructions provided in an array, along with a separate parameter specifying the recipe name

I am currently immersed in the book "Learning React" written by O'Reilly. The book mentions a method of creating components by using a function known as the "component creating function". It advises supplying the necessary parameters as the second par ...

Ajax polling ceases the polling process when an internet connection is lost

My current setup involves a continuous ajax polling cycle where messages are pulled, processed, a wait period occurs, and then the process is repeated. Everything runs smoothly until a user disconnects from the internet, whether it be due to a simple actio ...

Unable to retrieve or remove cookie sent from Express on client side

My Express server is sending a cookie to the client that is not httpOnly. Despite this, the client is unable to access the cookie through document.cookie or see it in the Application tab on chrome dev tools. Interestingly, I am able to view the cookie in C ...

Why isn't Material UI Data Grid onCellEditCommit working? Wondering if anyone has insight

Having trouble retrieving the data after editing using onCellEditCommit, but it's not functioning properly. This callback is triggered when changes to a cell are committed. Signature: function(params: GridCellEditCommitParams, event: MuiEvent, deta ...

WebSocket connection was unsuccessful. Switching to Comet and resending the request

I have been utilizing the Atmosphere framework 2.0.0.RC5 to expand my web application with websocket capabilities and encountered a perplexing error 'Websocket failed. Downgrading to Comet and resending' that I can't seem to resolve. To sta ...