Tips for positioning a group of images in the center of their parent container, with hidden overflow and aligning them both to

I have a wrapper containing 50 small images. Currently, the images are displayed horizontally on the same line with hidden overflow, but they start from the left.

Is there a way to make the overflow extend both LEFT and RIGHT while positioning the starting point in the middle instead of the left?

Below is my code snippet:

.whoARRAY {
  width: 100%;
  height: 90px;
  overflow-x: hidden;
  display: inline-block;
}

.whoARRAY img {}
<div class="whoARRAY"><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_017.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_016.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_015.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_014.png"
  /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_013.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_012.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_011.png" /><img
    src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_010.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_009.png" /><img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_010.png" /></div>

Answer №1

To calculate the scrollLeft value using JavaScript, you can utilize properties like scrollWidth and offsetWidth.

Ensure to take into consideration the CSS property white-space: nowrap as it prevents images from wrapping inside their container.

document.addEventListener("DOMContentLoaded", (event) => {
  const elm = document.querySelector('.whoARRAY');
  elm.scrollLeft = elm.scrollWidth / 2 - elm.offsetWidth / 2
});
.whoARRAY {
  width: 100%;
  height: 90px;
  overflow-x: auto;
  display: inline-block;
  white-space: nowrap;
}

.whoARRAY img {
  vertical-align: middle;
}
<div class="whoARRAY">
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_017.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_016.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_015.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_014.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_013.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_012.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_011.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_010.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_009.png" />
  <img src="https://www.young-marines.com/wp-content/uploads/2017/09/ym_010.png" /></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

The mobile screen menu is causing me some issues

I've written this code snippet (below) that generates a hamburger icon on mobile devices. When the user clicks it, a wave effect appears and covers everything on the screen, but I'm facing an issue where the wave can't cover the Brand sectio ...

What could be causing my body to resist adapting to the information on this website?

Is there a way to adjust content for a page using an overlay grid without exceeding the body boundaries? I'm facing this issue and struggling to find a solution. I've attempted to make a body reference in CSS and adjust the height, but no progres ...

Struggling to interact with a field within an HTML/CSS LESS document

I have encountered an issue with my website where I am unable to click on certain fields or buttons within the Login fieldset. Strangely, I can only interact with the fields and buttons located within the Register fieldset. The CSS for my website is writt ...

Troubleshooting Challenges with JavaScript DOM Manipulation

I am facing an issue with a table where I need the first column to remain fixed when scrolling horizontally. The code snippet below works perfectly for the first column of td's, but fails to work for the tr's. Despite checking the code thoroughly ...

Generate a visually dynamic representation of a live website page

I'm curious if it's possible to create a login page similar to the one shown in this image, using HTML, CSS, and Javascript. Instead of a traditional background image, I want the background to display the actual layout of another website, such a ...

Is it possible to combine Bootstraps responsive utilities into a single Less file?

Looking to implement the show/hide functionality from Bootstrap without needing the entire framework. I attempted including just the responsive-utilities.less file, but encountered errors with lessphp. Does anyone know of a website where I can find this v ...

The responsive design of Bootstrap NavBar seems to be malfunctioning when viewed on an iPhone device

Having an issue in Bootstrap Studio that I can't seem to resolve. I've seen recommendations to add the following meta tag to the <head> section: meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0", bu ...

Is there a way to position my input at the bottom of its parent element?

Is there a way to position this input element at the bottom and center of its parent div? https://i.sstatic.net/gs1yP.jpg ...

Maintain a consistent size for the material UI drawer, preventing it from resizing when the content size fluctuates

Incorporating Material UI into my project, I decided to use a drawer for navigation. However, within the drawer, there are Collapsible lists that expand when clicked. The issue arises when the list text items are lengthy, causing the drawer to unexpectedly ...

How to use CSS to insert a line break after the fourth child in a

At this moment, the example displays an unordered list containing 8 list items. I am curious if there is a way to add a line break after the 4th li item using only CSS (no HTML or JavaScript). Perhaps something like: ul li:nth-child(4n):after { conte ...

Avoid inserting line breaks when utilizing ngFor

I am using ngFor to iterate through a list of images like this: <div class="image-holder" *ngFor="let image of datasource"> <img src="{{image.url}}" height="150" /> </div> Below is the corresponding CSS code: .image-holder { di ...

How can I adjust the number of columns displayed per page on a Bootstrap Studio website?

Currently, I am utilizing Bootstrap studio to design a website featuring multiple cards on each page. Initially, I incorporated cards from the sb-admin-2 template and everything was proceeding smoothly. In my bootstrap studio interface, the three-column ca ...

Adjust the color of additional SVG paths upon hovering

I have the following code snippet. .icon {stroke-width: 0; stroke: currentColor; fill: currentColor;} a {color: red} a:hover {color: pink} a:hover circle {fill: green !important; color: orange} a:hover path {fill: blue !important} <a href=""> ...

Changing the direction of SVG text-path on a semi-circular element

I'm struggling to make a text-path inside an SVG follow the curve of a half circle. Unfortunately, I can't seem to get it to start at the correct point. This is how it's currently implemented in my project: <svg viewBox="0 0 500 ...

Elevate a div over another div

I have a situation where I need to nest two divs inside another div with the ID "video-wrapper". Here is the HTML code: <div class="row"> <div class="video-wrapper col-lg-2"> <div class="video-thumbnail"> <img ...

Is there a way for me to customize the appearance of the drop-down panel in a select box to feature rounded corners specifically for the Chrome browser?

Hey there! Currently working on an Angular 5 application where I need to style a select box with rounded corners for the options panel in Chrome. Would appreciate any tips or suggestions on achieving this using CSS styles. Please refer to the image linked ...

Converting the pictures to black and white by utilizing jQuery and adjusting the opacity

I have been attempting to convert my images to grayscale using CSS, but have encountered an issue with Firefox where it requires the use of SVG. However, utilizing SVG causes problems with other functionalities such as opacity and transitions. Is there a ...

Unexpected behavior encountered when making an AJAX request that involves CSS classes

One of the functionalities on a page involves selecting values for CSS class attributes. Within the rails application show view, there is a style block and a div that utilizes these styles: <style> #bg_<%= @promolayout.id %> { backgroun ...

Child component fails to receive data from Vue parent component

Struggling to grasp the methods of Vue, particularly in regard to my put-request for editing and deleting images. Within the parent component, the get-request loads images which are then pushed to an image gallery (the child-component) through properties. ...

Media queries in CSS appear to be dysfunctional when used on Microsoft Edge

@media (min-width: 992px) and (max-width: 1140px) { .mr-1024-none { margin-right: 0px !important; } .mt-1024 { margin-top: 1rem !important; } .d-1024-none { display: none !important; } } Utilizing the ...