CSS causing unexpected behavior in image slider

I am facing an issue with incorporating a code for an image slideshow into my website. The current CSS is causing the slideshow to malfunction.

Without the slideshow code in the CSS, the image appears correctly on the screen and changes its source every 2 seconds as intended.

However, placing the code inside the CSS results in two different images being displayed inline on the screen. I have spent hours trying to troubleshoot the CSS but haven't been able to solve the problem. Any suggestions or assistance would be greatly appreciated.

var myIndex = 0;
carousel();

function carousel() {
  var i;
  var x = document.getElementsByClassName("mySlides");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  myIndex++;
  if (myIndex > x.length) {
    myIndex = 1
  }
  x[myIndex - 1].style.display = "block";
  setTimeout(carousel, 2000); // Change image every 2 seconds}
#banner .content .image {
  border-radius: 100%;
  display: inline-block;
  height: 18em;
  margin-left: 3em;
  vertical-align: middle;
  width: 18em;
}

#banner .content .image img {
  border-radius: 100%;
  display: block;
  width: 100%;
}
<section id="banner">
  <div class="content">
    <header>
      <h2>header text</h2>
      <p>more text<br />
      </p>
    </header>
    <span class="image"><img class="mySlides" src="images/pic01.jpg" alt="" 
        /></span>
    <span class="image"><img class="mySlides" src="images/pic02.jpg" alt="" 
        /></span>
  </div>

Answer №1

After reviewing your code example, I noticed that there was a missing closing brace in your JavaScript code. Once I fixed that issue, everything started working correctly.

var myIndex = 0;
carousel();

function carousel() {
  var i;
  var x = document.getElementsByClassName("mySlides");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  myIndex++;
  if (myIndex > x.length) {
    myIndex = 1
  }
  x[myIndex - 1].style.display = "block";
  setTimeout(carousel, 2000); // Change image every 2 seconds}
}
#banner .content .image {
  border-radius: 100%;
  display: inline-block;
  height: 18em;
  margin-left: 3em;
  vertical-align: middle;
  width: 18em;
}

#banner .content .image img {
  border-radius: 100%;
  display: block;
  width: 100%;
}
<div class="content">
  <header>
    <h2>header text</h2>
    <p>more text<br />
    </p>
  </header>
  <span class="image"><img class="mySlides" src="http://placekitten.com/200/300" alt="" 
    /></span>
  <span class="image"><img class="mySlides" src="http://placekitten.com/200/200" alt="" 
    /></span>
</div>

Check out the jsFiddle link for more details.

Answer №2

Your JavaScript code contains a typo: The inline comment at the end is causing the function's closing bracket to be disabled.

// Change image every 2 seconds}
                             ^^^^^

Simply deleting the comment will fix the issue in your code.

Answer №3

After some trial and error, I finally cracked the code. The solution was to enclose both images within a single span element.

<span class="image"><img class="mySlides" src="images/pic01.jpg" alt="" />
<img class="mySlides" src="images/pic02.jpg" alt="" /></span>

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

Exploring design techniques in React Native

I am currently part of a team working on a react native project with multiple contributors. My goal is to find an effective way to segregate the styling tasks from coding, allowing UI developers to work independently without relying on code developers. In ...

Targeting images within content in Wordpress

In my custom Wordpress theme, specifically on the single-post page where individual posts are displayed, I have implemented the following code: <article id="post-<?php the_ID(); ?>"<?php post_class('col-md-12'); ?>> <div cla ...

Click event doesn't trigger the 'else if' statement in jQuery

Having trouble with a button click issue. In the following code, when the button is clicked, it checks if the text on the button is "day". If so, it changes the background-color of the body to red and updates the button text to "night". I am struggling wit ...

Parsing HTML code using PHP's DOM parser for iteration

<div class="reviews-summary__stats"> <div class="reviews-summary"> <p class="reviews-title">A</p> <ul class="rating"> <li class="rating__item r ...

How to extract text from a dropdown menu using JQuery in HTML

Having trouble retrieving the value of a selected element and displaying it in the console. Despite all my efforts, I can't seem to make it work - value1 is retrieved fine, but value2 and 3 are not. If you have any suggestions (apart from using JQuer ...

Enhancing User Interfaces with React and CSS Styling

I need to incorporate two unique React components on my webpage: Component1 and Component2. Code for Page1: <Component1/> <Component2/> While the individual components will have their own CSS files for styling, I also have a page1.css file f ...

What is the best method for locating the ID of a specific element tied to a dynamically generated button that presents profile information using javascript?

My goal is to show unique user information when the user hovers over a button. This information is contained in a specific div element with an ID that corresponds to the button being hovered over. The challenge I am facing is extracting the ID of the info ...

Tips on displaying just two buttons in one line

When using *ngFor to display multiple buttons, all buttons appear in one column. I want to have only 2 buttons in a row: the green buttons in one line, and the red buttons in the next line. How can I achieve this? Here is what I have tried: <div class= ...

Looking for a way to trigger PHP mail when a form is submitted rather than on page load without relying on If...POST conditions?

I have a PHP mail code that successfully sends an email message when the page loads, but I need it to send on form submission on the same page. I have searched for solutions, but due to the specific code I am using, I couldn't figure it out. This pag ...

How can an object be utilized within the image tag's src attribute?

Is it possible to use an object in the img src attribute? // HTML <img src= "item.img" https://i.sstatic.net/m8PDy.png ...

Steps for creating a clickable product item card throughout

I am looking to modify my Product Card design. Currently, there is a hyperlink named Buy at the end of the card that users can click on. However, I want to make the entire card clickable with another hyperlink so users do not have to specifically click on ...

Transform input string containing newline characters into separate paragraphs

I utilize Contentful CMS for content management and fetch the content through their API. When the content is fetched, it comes in as a JSON object. One of the keys within this object pertains to the main text block for the entry I am retrieving. This stri ...

Using HTML5 and CSS for 3D transformations along with stacking divs to create a unique accordion

Is there a way to stack divs vertically above each other and use CSS3 3D transforms to create a folding effect between them? I've tried rotating the divs on their X axis, but it leaves gaps between each div because they don't collapse into each o ...

Has jQuery UI Accordion taken over the design of unordered lists?

I'm having trouble with my website's navigation styling getting messed up by the jQuery accordion. The unordered list inside the .accordion div is overflowing and losing its CSS styling. I've tried adding clearStyle true and autoHeight false ...

Adjust the width of a div based on its height dimension

I have a div called #slideshow that contains images with a 2:1 aspect ratio. To set the height of the image using jQuery, I use the following function: Keep in mind that the Slideshow Div is always 100% wide in relation to the browser window. If the use ...

Tips on keeping display flex elements confined within a container

My goal is to add two divs to an element. The first div needs to have a fixed height and position. The second div should fill the remaining space, but not exceed it. I have created an example below to illustrate the desired result. #container { border ...

Setting the default radio button selection in Angular 2

Present within my interface are two radio buttons, not dynamically produced: <input type="radio" name="orderbydescending" [(ngModel)]="orderbydescending" [value]="['+recordStartDate']">Ascending<br> <input type="radio" name="order ...

Adjust the border color of the <input> element when it is clicked on

I'm currently working on a login screen for my next.js application and I've encountered an issue where the border color changes to a mixture of white and blue when I select an input field. https://i.stack.imgur.com/R2yKa.png I attempted to reso ...

Top strategy for managing numerous click occurrences

When a link is clicked in my application, I want to trigger a specific action. For instance, I am currently working on a manual that contains numerous internal links. At the moment, I am handling them individually like this: function waitForClick() { ...

The fundamental element in CSS for structuring and styling web

What distinguishes the selector patterns E * F and E F? Both selectors apply the appropriate style to element F which is a child of element E. Consider the following HTML structure: <div id= "parent"> <div id="subparent"> <div i ...