Tips for aligning the first element in an inline-block list item horizontally

I'm working on a list with horizontal scroll functionality to allow users to view content by scrolling. I have successfully implemented this, but I'm facing a couple of challenges that I need help with:

  1. I want the first item in the list to always start centered horizontally, similar to how a text block would appear. I've tried different CSS techniques like flex-box, but they end up centering the entire scrollable content instead of just the first item. Even using Bootstrap's offset doesn't work well across different screen sizes.

Current Appearance: https://i.sstatic.net/7dj8F.jpg

Desired Appearance: https://i.sstatic.net/3pTTS.jpg

  1. Another issue I'm facing is that my horizontal scrollbar looks like Image 1. Is there a way to make the scrollbar appear in the center without taking up the entire width while still allowing users to scroll through the content?

https://i.sstatic.net/SpEU2.jpg

Any suggestions or guidance would be greatly appreciated! It seems like solving these issues might require JavaScript, which I'm not very skilled at. I'm even willing to pay for a solution at this point due to my urgency.

Thank you in advance!

You can view the codepen demo here: https://codepen.io/anon/pen/EqwZLJ

.scroll-horizontal ul {
    position: relative;
    margin: 0;
    padding: 0;
    list-style: none;
    white-space: nowrap;
}

.scroll-horizontal ul li {
    display: inline-block;
    max-width: 735px;
    margin-right: 15px;
    overflow: hidden;
}

.scroll-horizontal {
    padding-bottom: 50px;
    margin-bottom: 150px;
}

.overflow-x {
    position: relative;
    overflow-x: scroll;
}

.overflow-x::-webkit-scrollbar {
    -webkit-appearance: none;
}

.overflow-x::-webkit-scrollbar:vertical {
    width: 11px;
}

.overflow-x::-webkit-scrollbar:horizontal {
    height: 11px;
}

.overflow-x::-webkit-scrollbar-thumb {
    border-radius: 8px;
    border: 2px solid white; /* should match background, can't be transparent */
    background-color: rgba(0, 0, 0, .5);
}

Answer №1

Do you currently utilize jQuery on your website? If so, consider implementing this solution.

jQuery

let mainElement = $(".scroll-horizontal"); // the id of the scrollable div
let firstImageElement = $("#image-1");
mainElement.scrollLeft(firstImageElement[0].offsetLeft);

Javascript

let mainElement = document.getElementsByClassName("scroll-horizontal")[0]; // the id of the scrollable div
let firstImageElement = document.getElementById("image-1"); // the id of the first image div
mainElement.scrollLeft(firstImageElement.offsetLeft);

Add blank space

<ul class="">
    <li id="image-blank" style="width: 735px;height: 459px;"></li>
    <li id="image-1"><img src="https://ultraimg.com/images/2016/08/11/1V6u.jpg" class="img-fluid" alt=""></li>
    <li><img src="https://ultraimg.com/images/2016/08/11/1V6u.jpg" class="img-fluid" alt=""></li>
    <li><img src="https://ultraimg.com/images/2016/08/11/1V6u.jpg" class="img-fluid" alt=""></li>
    <li><img src="https://ultraimg.com/images/2016/08/11/1V6u.jpg" class="img-fluid" alt=""></li>
 </ul>

Please take a look at your code pen. I have made the necessary adjustments for you.

Answer №2

Check out this modified version of your design. I made use of a container-grid with 3 columns for centering purposes, which seemed like the simplest solution:

Update after receiving feedback: I decided to utilize percentages instead of pixels for sizing.

.grid-container {
  display: grid;
  overflow: hidden;
  width: 100%;
  grid-template-rows: 1fr 2fr;
  grid-template-columns: repeat(3, 33%);
}

To adjust the image sizes, I followed the same approach:

.scroll-horizontal li {
  display: inline-block;
  overflow: hidden;
  max-width: 33%;
}

.scroll-horizontal .img-fluid {
  max-width: 100%;
  height: auto;
  margin-right: 15px;
}

In order to center the first image precisely in the middle, I applied a margin equivalent to the size of the first column:

.scroll-horizontal li:first-of-type {
  margin-left: 33%;
}

Regarding the scrollbar issue, I attempted incorporating specific styles, but they didn't yield the desired outcome at the moment:

.space-default::-webkit-scrollbar-track-piece:end {
  margin-right: 33%;
}

.space-default::-webkit-scrollbar-track-piece:start {
  margin-left: 33%;
}

Thus, I decided to hide the scrollbar entirely (please note that this technique may not function in Firefox):

.space-default::-webkit-scrollbar {
  display: none;
}

If you're interested in customizing the scrollbar appearance, you can refer to this discussion thread or this article, although these methods are primarily tailored for webkit browsers.

I trust that these adjustments prove beneficial to you.

Answer №3

Is the float:right; property being used to right align the divs?

Answer №4

Combine all the individual image divs into a new container and experiment with centering them or utilizing a grid layout for organization.

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 navigation bar is obscuring the header on my website

I am facing an issue with creating a navigation bar that covers up the header text. I tried adjusting the margins to fix this problem but ended up moving the header along with the navigation bar. How can I ensure that the navigation bar remains fixed witho ...

AngularJS form submission with Ajax requiring a double click to successfully submit the form

I need to perform the following activities on my HTML page: User inputs email and password for registration Form is sent to Controller when user clicks Submit Controller uses AJAX to create JSON request to RESTful Server and receives response from server ...

Optimizing CSS for printing by eliminating unnecessary white space with media queries

Appreciate your assistance! I'm currently working on a solution to print a specific div using CSS Media queries. When the user clicks on print, I want to hide all other elements except for the body div they chose. However, I'm facing an issue whe ...

I attempted to implement a CSS and Typescript animation for a sliding effect, but unfortunately, it isn't functioning

So I'm encountering an issue with this unique ts code: {/* Mobile Menu */} <div className="lg:hidden"> <button className="flex items-center w-8" onClick={toggleMenu}> {isMobileMenuOpen ? ( ...

Tips for integrating Jquery into your Laravel project

There is an issue that I am facing on this particular line of code <script type="text/javascript" src="js/jquery.js"></script> I suspect the error is due to jQuery not being installed in my Laravel project. I typically use npm for installin ...

Error 504 occurs on Express.js due to a timeout issue while a timer is active

Encountering a "504 Gateway Timeout" error on my Express.js application when a JavaScript timer is set to run every 20 seconds. Here is the code for my Express.js listener and the timer: const express = require('express') const app = express() ...

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

I am having trouble toggling radio buttons in React

class VoiceSelector extends Component { constructor(props){ this.handleCheck = this.handleCheck.bind(this); } state={ voices : [ {"Voice":"Indian English Female Voice 1"}, {"Voice":&qu ...

Comparison: NumberFormatter versus NumberFormat in PHP and JavaScript

My attempts to format currency seem to yield inconsistent results. Using PHP with the NumberFormatter class, here's a snippet of my code: $number = 5125.99; echo getInternationallyFormattedCurrency($number, 'tl-PH', 'PHP'); echo & ...

Creating an Angular table row that can expand and collapse using ng-bootstrap components is a convenient and

I need assistance with an application I am developing, where I want to expand a table row to display details when it is clicked. The issue I am facing is that currently, all rows expand and show the data of the clicked row as seen in the image result below ...

AngularJS filter that retrieves only values that have an exact match

In my AngularJS application, I have a checkbox that acts as a filter: Here is the data being used: app.controller('listdata', function($scope, $http) { $scope.users = [{ "name": "pravin", "queue": [{ "number": "456", "st ...

Adaptive text sizing within Microsoft Outlook

I am facing a challenge in making the font size of my email signature responsive. Although VW seems to be the solution, it doesn't seem to work in Outlook. I have attempted using CSS, but it appears that inline CSS might be necessary. However, I am un ...

NPM encountered an issue that prevented it from scanning directories due to a permission denial with the error code E

Whenever I type "npm run build:prod" build:prod npm run build -- --configuration production --aot --optimization=false I encounter the following error: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e4e3ece1fbafece3 ...

Creating links in JSTree that cannot be used in JSP pages with Struts2

I created a tree using JSTree in my JSP page for a Struts2 web application. Here is the code snippet: <div class="panel"> <div id="demo1" class="demo"> <ul> <li id="node"><a href="#"><s:property value="product"/></ ...

Flipping and rotating images using the power of HTML5 Canvas

Hello, I need assistance with my Electron app that arranges images for batch printing on an industrial printer. The issue I am facing is with images flipping or being mirrored unpredictably. Below is the code snippet responsible for determining whether an ...

Is it possible to call a ref from a different component in React?

I'm currently working on a React chat application and I want the input field where messages are entered to be focused every time you click on the chat box. However, the challenge I'm facing is that the chat box in the main component is separate ...

Angular Satellizer causing issues with Facebook login on mobile devices

Issue Currently, I'm developing an application using Angular and have successfully integrated Facebook login through Satellizer. The functionality works flawlessly on desktop browsers; however, when accessed from mobile devices, it tends to be unreli ...

Issues with the Bootstrap date time picker functionality

Even though I have added all the necessary libraries for the date time picker, it still isn't working properly. You can check out this fiddle to see the code. <div class='input-group date' id='from_time'> <input type ...

Angular date selection with a range of plus two days, factoring in the exclusion of weekends

I am currently using a mat date picker range with specific logic. The minimum date that a user can select on the calendar is set to + 2 days. For example, if today's date is July 20, 2022, the minimum selectable date would be July 22, 2022. However, ...

Efficiently refresh several DIV elements with a single JSON request

After extensive searching, I've come to the conclusion that due to my limited proficiency in javascript, I need some help. The issue at hand is with a json format output produced by an ASP page on a separate webserver, which looks something like this: ...