No matter what I attempt, my presentation refuses to align in the center

My slideshow, which is powered by jQuery/JS and involves absolute positioning for each image, is causing me trouble when trying to horizontally center it on the page. No matter what I do, I can't seem to get it right. The challenge is not only getting it centered at full-screen width but also ensuring it stays centered when the browser window is resized.

According to my understanding, elements positioned absolutely should be relative to their next positioned ancestor. In my case, this ancestor seems to be the ".fadein" div (or maybe the "#slideshow" div?), which is relatively positioned. So, if I move the ".fadein" div, the slideshow should theoretically follow suit.

And it does work to some extent. I can shift the ".fadein" div relatively from one side, making the slideshow move accordingly.

However, I'm struggling to find a reliable method to center the slideshow horizontally without either:

1) Creating extra white space on one side of the image, leading to an unnecessary scroll bar at the bottom of the page.

2) Preventing the image from resizing as the browser window is resized.

Edit 1: I managed to get the slideshow centered but still need a solution for resizing...

Hopefully, someone can assist me with this issue. Thanks!

$(function() {

    $('.fadein img:gt(0)').hide();
    
    setInterval(function() {
    
    $('.fadein :first-child').fadeOut(2000)
      
        .next('img').fadeIn(2000)
         
        .end().appendTo('.fadein');
         
    }, 5000);
});
.fadein {
position:relative;
}

.fadein img{
position:absolute;
left:0;
}
<div id="slideshow">

  <div class="fadein">

  <img src="http://placehold.it/350x150" />
   <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />

  </div>

</div>

Edit 2: Although I achieved perfect centring in Safari, Firefox on Mac displays it off-center. Here's the updated CSS:

.fadein {
position:relative;
top:70px;
height:100%;
}

.fadein img {
position:absolute;
left:50%;
top:50%;
-webkit-transform: translate(-50%, -0%);
}

#slideshow {
max-width:100%;
height:100%;
}
<div id="slideshow">

  <div class="fadein">

  <img src="http://placehold.it/350x150" />
   <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />

  </div>

</div>

Answer №1

Utilizing the calc function in CSS, you can center align elements by calculating their position:

.centered-element {
  position: absolute;
  left: calc(50% - sizeOfElement);
}

This is just one method among many for achieving central alignment of boxes.

Answer №2

$(function() {

    $('.fadein img:gt(0)').hide();
    
    setInterval(function() {
    
    $('.fadein :first-child').fadeOut(2000)
      
        .next('img').fadeIn(2000)
         
        .end().appendTo('.fadein');
         
    }, 5000);
});
body,html {
position:relative;
  height: 100%;
}

.fadein {
position:relative;
  height: 100%;
}

.fadein img{
position:absolute;
left:50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
}

#slideshow {
position:absolute;
  width: 100%;
  height: 100%;
}
<div id="slideshow">

  <div class="fadein">

  <img src="http://placehold.it/350x150" />
   <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />
  <img src="http://placehold.it/350x150" />

  </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

Why does modifying a variable within the fetch URL result in undefined

I currently have a server running on the URL http://localhost:3000/. Within my JavaScript code, I have defined a variable called productCode with the value 'example-code55'. let productCode = 'example-code55'; const fetchData = async ...

Determining the number of columns and rows within an HTML table

Is there a way to determine the number of columns and rows in an HTML file? How can we accurately count the total number of td tags present within the HTML file? ...

Utilize jQuery to toggle classes on and off for anchor elements within list items in an unordered

<ul class="tab_sublinks"> <li><a href="#" class="active">Product Master</a></li> <li style="margin: 0;">|</li> <li><a href="#">Charges Computation Master</a></li> </ul> Ho ...

Listening to events on the iterative variable of NgFor directive in Angular 2

Angular2 has been my latest exploration in solving a unique data binding challenge. In my UI, I've presented a javascript array of objects like a database recordset in an HTML table. Each row contains menus and inputs allowing users to modify the rec ...

How to retrieve the index upon clicking in Javascript

In my 3d art gallery project, I am utilizing plain JavaScript. The task at hand involves populating certain columns with images by pulling from an array of image sources. On click, I need to retrieve the index of the clicked image so that I can extract add ...

Using Ajax with Angular services

Recently, I decided to explore Angular JS and Async calls as an alternative to $JQuery. However, I encountered a peculiar issue while making ajax calls. To adhere to 'best practices', I shifted my ajax requests to a service and then initiated th ...

Why is IE waiting to load XML content until I access Developer tools?

I'm encountering an issue with my webpage where the product information loaded from an XML file using a jQuery AJAX get request works fine in Firefox and Chrome, but for some reason it doesn't load in Internet Explorer. Oddly enough, it does load ...

Testing the revised react component through unit testing with jest and enzyme

I am currently working on writing the test file for this code snippet. Here is my approach: import React from 'react'; import renderer from 'react-test-renderer'; // import { mount } from 'enzyme'; import LazyToastMessage from ...

Columns in Bootstrap4 housing elements positioned absolutely

Recently, I've been developing a game that involves using absolute positioning for certain elements. This is necessary for creating moving animations where elements need to slide around and overlap to achieve a specific effect. As I focus on optimizi ...

Is it possible to replace the catch function in JavaScript with a custom function?

Below is the code snippet: function xyz() { try { var a = someexecutions(); handlesuccess(a) } catch (err) { handleerror(err) } } This type of function is repeated numerous times in my codebase and I am looking for a way to improve it. f ...

Article discussing the Facebook like button

I've recently incorporated Facebook like buttons into the articles on my online store. It's great because when someone shares a post about a product or article, it shows up on their wall. Users receive notifications on Facebook when someone else ...

Creating a dynamic map in AngularJS by parsing and utilizing data from a JSON file

Looking to create a map from JSON data returned by a RESTFUL WEB Service using AngularJS. Here is an example of the JSON format: myJSON = [{ id: 8, color: "red", value: "#f00" }, { id: 37, color: "green", value: "#0f0" }, { id ...

The button on my page refuses to align in the center, and I'm at a loss as

Struggling to center a button within my div, despite having all other content centered. Here is my code: .middle { width: 100%; height: auto; text-align: center; position: relative; ...

Best practices for displaying code blocks within an AngularJS application

Currently, I am in the process of building a website focusing on AngularJS within AngularJS ;-) To accomplish this, I have included ng-app within the body element. As a result, all my code snippets (enclosed in <pre><code> ... </code>&l ...

Extracting data from a web service and populating an OWC11 spreadsheet with 10,000 rows

Having an issue with the web service call taking too long to return. The ASP.NET page is taking over a minute to start loading. Currently, I am using C# Response.Write() to output the data to Javascript for insertion into OWC11 spreadsheet. I'm lookin ...

Performing an AJAX GET request to the API after a set time interval

The API is constantly updating with live values, so I am attempting to fetch the data every second and display it on the webpage. Although I used a GET request call every N seconds using set_interval(), the values only load once and do not update with eac ...

Searching for values in nested arrays using lodash.find

Presented below is an array where I aim to employ lodash for item search: [ { "itemA": "apple", "itemB": [ { "itemC": "1", "itemD": "red apple" }, { "itemC": "2", "itemD": "green apple" }, ...

When resizing the window, divs shift positions and prevent the use of the horizontal scrollbar

I'm encountering an issue with my website layout. Specifically, I have a full-width navbar with some divs nested within it. However, when the page is resized, the position of these divs in the navbar shifts and a scrollbar appears at the bottom of the ...

Prevented a frame from "https://googleads.g.doubleclick.net" from accessing another frame

After placing ads on my website, they are displaying properly. However, I am receiving an error repeatedly in the console when the page loads: A frame from origin "" is being blocked from accessing a frame with origin "". The requesting frame has an "ht ...

AngularJS - Viewless and Issue-Free

I'm currently working on a project that involves using AngularJS and PHP. I made some changes, but now it's not functioning properly... The issue is that there are no errors in the console, Angular works (I can retrieve its version), but my vi ...