What is the best method for overlaying text on individual images in a slideshow?

Currently, I am delving into the world of jQuery, experimenting with various slideshows and attempting to incorporate text and effects.

In my endeavors, I stumbled upon this codepen that served as an excellent template. However, when I tried adding text over the images, it disrupted the fluidity of the slideshow, creating clutter and ultimately breaking the functionality. My goal is to have distinct quotes overlaying each of the three images.

Check out the codepen here

 ``<div id="slideshow">
  <div id="frame">
    <img class="slide" src="http://placehold.it/350x150/ffffff/f0265b&text=1">
    <img class="slide" src="http://placehold.it/350x150/ffffff/47d1c0&text=2">
    <img class="slide" src="http://placehold.it/350x150/ffffff/006400&text=3">
  </div>
</div>

If anyone has a solution to this issue, I would greatly appreciate your input!

Answer №1

To achieve the desired effect, I recommend creating a container for the images and positioning it using position: relative. Then, you can add an element that is positioned absolute within the container to overlay your text.

var $frame = $('#frame');
   
setInterval( function() {
  var slides = $('.slide');
  slides.first().appendTo($frame);
}, 3000);
body { background-color: #ddd;}
#slideshow {
  position: relative;
  background-color: #ccc;
  border: 1px solid #555;
  border-top: none;
  box-shadow: 0 1px 9px #222;
  width: 60%;
  margin: 50px auto;
}
#frame {
  position: relative;
  background-color: transparent;
  width: 100%;
margin: 0 auto;
}
.slide:first-child {
  display: block;
  width: 100%;  
}
.slide img {
  width: 100%;
  display: block;
  max-width: 960px;
  height: auto;
  margin: 0 auto;
}
.slide:not(:first-child) {
  display: none;
}
.text {
  position: absolute;
  top: 0; left: 0; right: 0;
  background: rgba(0,0,0,0.5);
  color: white;
  margin: 0;
  padding: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slideshow">
  <div id="frame">
    <div class="slide"><img src="http://placehold.it/350x150/ffffff/f0265b&text=1">
      <p class="text">foo</p>
    </div>
    <div class="slide"><img src="http://placehold.it/350x150/ffffff/f0265b&text=2">
      <p class="text">foo</p>
    </div>
    <div class="slide"><img src="http://placehold.it/350x150/ffffff/f0265b&text=3">
      <p class="text">foo</p>
    </div>
  </div>
</div>

Answer №2

One interesting technique is to enclose images within a div element that includes the image as a background, along with captions. This way, you can smoothly slide the entire div instead of just the image itself.

Answer №3

Take a look at this:

var $frame = $('#frame');
   
setInterval( function() {
  var slides = $('.slide');
  slides.first().appendTo($frame);
}, 3000);
body { background-color: #ddd;}
#slideshow {
  position: relative;
  background-color: #ccc;
  border: 1px solid #555;
  border-top: none;
  box-shadow: 0 1px 9px #222;
  width: 60%;
  margin: 50px auto;
}
#frame {
  position: relative;
  background-color: transparent;
  width: 100%;
margin: 0 auto;
}
.slide {
  position:relative;
}
.slide:first-child ,.slide > img{
  display: block;
  width: 100%;
  max-width: 960px;
  height: auto;
  margin: 0 auto;
}
.slide:not(:first-child) {
  display: none;
}

.slide > div {
  position:absolute;
  padding:10px;
  background:rgba(0,0,0,.2);
  font-family:helvetica,tahome,serif;
  font-size:18px;
  bottom:0;
  left:0;
  right:0
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slideshow">
  <div id="frame">
    <div class="slide" >
      <div>
        Example Text for Slide No.1
      </div>
     <img  src="http://placehold.it/350x150/ffffff/f0265b&text=1">
    </div>
    <div class="slide" >
      <div>
        Example Text for Slide No.2
      </div>
     <img src="http://placehold.it/350x150/ffffff/47d1c0&text=2">
    </div>
    <div class="slide">
      <div>
        Example Text for Slide No.3
      </div>
      <img src="http://placehold.it/350x150/ffffff/006400&text=3">
    </div>
  </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

The jQuery show/hide function fails to function properly when a div is relocated within another div

I've been racking my brain over this issue and can't seem to understand why jQuery is not showing or hiding a div when I place it inside a repeater. Below is the div that I'm trying to work with: <div id="EmailForMoreInfo">Hey there!& ...

What is the best way to incorporate data from a foreach method into a function call within an HTML string?

Having trouble calling a function with data from a foreach loop while generating HTML cards and buttons from an array. The issue seems to be in the renderProducts() method. /// <reference path="coin.ts" /> /// <reference path="prod ...

IE Form SubmissionThe process of submitting a form in

My attempt to prevent form submission in IE 8 using jQuery 1.8.2 by using e.preventDefault(); is not working as expected. $(document).ready(function () { $("form").submit(function (e) { e.preventDefault(); $.ajax({ .... ...

Adding a click function to a div individually when they share the same class in HTML

My goal is to include 4 unique divs inside a timeline container: This is how I envision the structure: <div id="timeline" > <div class="timeline-bar t-1"></div> <div class="timeline-bar t-2"> ...

When making changes to my database using PHP and MYSQL, only the first column gets updated while the rest remain unchanged

I have a mysql database with the following columns: firstname, lastname, age, email, phone When I attempt to update the data, it sometimes turns out like this: John Doe 23 <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2e2e2 ...

When trying to implement a dark/light theme, CSS variables may not function properly on the body tag

Currently, I am in the process of developing two themes (light and dark) for my React website. I have defined color variables for each theme in the main CSS file as shown below: #light{ --color-bg: #4e4f50; --color-bg-variant: #746c70; --color-primary: #e2 ...

Ways to execute several actions within an HTML form

If I wanted to implement a double action in an HTML form, how would I go about it? The first action should direct to examle1.php And the second action should go to example2.php Currently, my code looks like this: <form name="input" action="" method= ...

I am unable to achieve negative X degree rotation of the image while using mousemove to rotate it

I need assistance with moving a picture in 3D. I want the offsetX of the mouse to be positive when it's past half of the picture, and negative otherwise. How can I achieve this effect for rotation degrees? This is what I have tried: $('#img ...

divs aligned at the same vertical position

Struggling for days to align buttons vertically, I have tried various approaches without success. I attempted using position: absolute; bottom: 0; on the parent with position: relative; set. @import url('https://fonts.googleapis.com/css?family=Mon ...

Initializing an HTML5 video player directly from an Array

I am trying to populate a video player on my webpage with an array of videos. Here is the code I have so far: var current = 0; var videos = ["01", "02", "03", "04"]; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomInde ...

jQuery - Exploring the World of Cricket Stats

Looking for assistance with jQuery - can someone help me with the following task? I am trying to automatically calculate a player's total runs, strike rate etc. based on information from Match 1 and Match 2. HTML <table width="100%" border="0" c ...

Cannot see the box-shadow with :before and z-index in CSS

My main objective is to prevent the box-shadow from overlapping with adjacent elements by utilizing the :before pseudo-element and adjusting the z-index. However, I am facing an issue where the shadow appears beneath the container of the list item that is ...

Ways to switch out the background image using jQuery

I am currently using jQuery to dynamically change the background image of a web page. Right now, I have implemented two separate buttons that toggle between Image A and Image B. By default, Image A is displayed on the page. My goal is to enhance this func ...

What is the best way to use jQuery to access dynamic HTML elements?

I have created a dynamic table that allows users to add and delete rows. However, I am facing an issue with accessing the information inputted by the users for calculations because it seems to be inaccessible once dynamically added to the page. Below are ...

Showcased a few items from the model.forms dataset

My table has three fields: id, name, status. When I use {{ form|bootstrap }}, all fields are displayed. The 'name' field is a drop-down in HTML. I only want to show entries where the status is equal to 1. How can I achieve this? ...

Mysterious extra space appearing on the right side of the left scroll div in Chrome

I am currently working with a table that dynamically increases in content when a button is clicked. I have successfully implemented a scroll on the right side to handle overflow. However, I encountered an issue when trying to change the scroll to the left ...

Detecting Collisions in a Canvas-Based Game

As part of my educational project, I am developing a basic shooter game using HTML5 canvas. The objective of the game is to move left and right while shooting with the spacebar key. When the bullets are fired, they travel upwards, and the enemy moves downw ...

Tips for choosing the right element in CSS

Can someone help me with this issue? https://i.stack.imgur.com/pBu1w.png I've tried setting a CSS selector, but it doesn't seem to be working as expected. What could be causing the selector to not work properly? Any suggestions on how I can f ...

Issue with FullPageJS: scrollOverflow feature not functioning upon click event

I am currently working on a Fullpage.js instance that needs to be initialized with a click event and then destroyed when switching to another page, and vice versa. The scrollOverflow feature works perfectly as long as the #fullpage element is not hidden up ...

Using Phonegap with Dreamweaver CS5.5

Can anyone tell me what version of Phonegap is used in Dreamweaver CS5.5? I attempted to update the default phonegap.js file with the newest one, but it resulted in errors. Would it be wise to replace the current phonegap.js file with the latest version? ...