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

What is the best method to activate or deactivate a link with jQuery?

Can you provide guidance on how to toggle the activation of an anchor element using jQuery? ...

Struggling to make the toggle button appear on the bootstrap navbar when shrinking the resolution

Everything seems to be working well with the navbar initially, but when resizing the screen to a smaller resolution, all the navigation items disappear and the "hamburger icon" fails to appear. <nav class="navbar navbar-custom navbar-expand-md fi ...

Tips for achieving consistent text and image alignment through CSS styling

My table currently contains a default profile image. Initially, there might not be any text content and I have set up the CSS styling to allow text to wrap around the default image. I am facing an issue when trying to position the image statically in a wa ...

``Can you provide guidance on excluding matching values from a dictionary object in a Angular project?

I've developed a function that takes a dictionary object and matches an array as shown below: const dict = { CheckAStatus: "PASS", CheckAHeading: "", CheckADetail: "", CheckBStatus: "FAIL", CheckBHeading: "Heading1", CheckCStatus: "FAIL", ...

When defining a stripe in TypeScript using process.env.STRIPE_SECRET_KEY, an error of "string | undefined" is encountered

Every time I attempt to create a new stripe object, I encounter the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string& ...

Presentation: Positioning Next Buttons Beside Slide Images While Maintaining Responsiveness

I am facing a challenge with my slideshow project. The slideshow consists of images and text on each slide, along with previous and next buttons that are aligned within the parent container. I am trying to align these buttons at the midpoint of each image ...

What is the best way to create a line break at the maximum width point?

I am currently working on a code that dynamically receives variables and concatenates them with an underscore: var text = product + "_" + someOtherInfo; After combining the variables, I need to display this text inside a div element. div { max-width ...

Tips for adjusting the color of a linear gradient in a shimmer effect

I have a puzzling question that I just can't seem to solve. My goal is to display a shimmer effect on cards as a temporary placeholder until the actual content loads. I found some CSS code in this answer, but when I tried it with a dark background, t ...

Is there a way to arrange my bootstrap navigation tabs vertically on my mobile device?

I have a group of five bootstrap navigation tabs that are evenly spaced and visually appealing on desktop screens. However, when viewed on a mobile device, the text within the tabs becomes cramped together. How can I make the tabs stack vertically on mobil ...

What could be the reason for my Express server returning a 404 error for all files other than index.html?

Currently, I am delving into Node.js with Express in order to set up a small server for educational purposes. Strangely, every request made to files linked within my index.html file, such as the .css, .js, and image files, results in a response code 404. ...

retrieve the parent frame's request URL

I am faced with the task of writing a JSP code that retrieves the getRequestURL() of the parent frame (the URL shown in the address bar) from a child frame. However, when I attempt to do this, I only obtain the URL of the child frame. Is there anyone who ...

Challenges when working with AJAX/jQuery in terms of fetching JSON data and setting dataType

I am currently facing a challenge with my practice of AJAX/jQuery coding. Despite my efforts to learn and improve, I find the concepts of jQuery and AJAX quite perplexing. Specifically, I am struggling to understand dataTypes and how to manage different ty ...

What are the steps to make a dropdown menu using only HTML and CSS?

Can someone please share the most straightforward method for creating a drop-down list using just HTML and CSS? ...

Ways to revert all modifications to styles implemented using JavaScript

Hey there, just checking in to see how you're doing. I was wondering if there's a way to reset all the styles that have been changed using JavaScript? I'm referring to the styles shown in this photo: Thanks in advance. ...

HTML does not support the use of certain symbols

Currently, I am in the process of designing a homepage for my school project. However, I have run into an issue. The content that I want to be displayed on my homepage is as follows: "Daudzspēlētāju tiešsaites lomu spēle (Massively Multiplayer Online ...

When using Ajax in Jquery and expecting data of type JSON, the response always seems

Looking to create a basic jQuery Ajax script that checks a user's discount code when the "Check Discount Code" button is clicked? Check out this prototype: <script> jQuery(function($) { $("#btn-check-discount").click(function() { c ...

Tips for retrieving the distance from the top of a specific div element and transferring it to another div

How can I add margin-top based on the tab that is clicked? Specifically, when TAB 4 is selected, I want the content to remain in the same position from the top. ...

Elements vanish when SHAKE effect is in use

I've been experimenting with this framework and I'm struggling to get the shaking effect to work properly. Whenever I hover over an element, other divs seem to disappear. I tried using different versions of JQuery and JQuery UI on JSFiddle, and i ...

Tips for keeping Sub Menu visible at all times

Is there a way to keep the sub menu always visible on the homepage of my website's navigation? Thank you for your help! Check out my website here ...

I could really use some assistance with this script I'm working on that involves using ($

Using Ajax for Form Submission: $.ajax({ url: 'process.php', type: 'post', data: 'loginName=' + $("#loginName").val() + 'loginPass=' + $("#loginPass").val(), dataType: 'json', success: func ...