Creating a Custom Testimonial Slider using CSS and JavaScript Tutorial

In this tutorial, you can learn to create a Custom Testimonial Slider using HTML, CSS, and JavaScript. The tutorial aims to provide students and beginners with a reference for learning to build useful websites or web application components. Here, I will be providing a sample web page script that demonstrates the creation of a custom testimonial slider component.

What is a Testimonial Slider?

The Testimonial Slider is basically text content of a website containing the testimonies of the company or organization users or clients. This component works like an Image slider or carousel whereas the testimonies items are shown individually and can be navigated to the next or previous item by clicking the navigation button of the slider component.

How to Create a Custom Testimonial Slider?

We can build a Custom Testimonial Slider using CSS and JavaScript. To do that, we need to construct the HTML elements of the component containers and other related elements such as the navigator buttons. Using some of the CSS pseudo-elements and properties, we can design the slider elements on how we want the component to be displayed. Using also the CSS animation properties to create the sliding effect for transitioning from the currently active item to the next or previous one. Then, using the built-in and useful JS methods and APIs, we can make the testimonial slider component functional. Check out the sample web page scripts that I created below to understand it more.

Sample Web Page

The scripts below result in a simple web application page that contains a simple custom Testimonial Slider component that allows the visitor to navigate the testimonial items from the current item to the next and previous one.

Page Interface

The script below is the HTML file script named index.html. The file contains the page layout and the component's elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>HTML CSS and JS - Custom Testimonial Slider</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
  10. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  11. <link rel="stylesheet" href="style.css">
  12. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  13. </head>
  14. <div class="content-md-lg py-3">
  15. <div class="col-lg-8 col-md-10 col-sm-12 col-12 mx-auto">
  16. <div class="page-title">Creating a Custom Testimonial Slider HTML, CSS, and JavaScript</div>
  17. <hr style="margin:auto; width:25px" class="border-light opacity-100">
  18. </div>
  19. <div class="container-lg">
  20. <div class="row py-3 my-5">
  21. <div class="col-lg-5 col-md-5 col-sm-10 col-12 mx-auto">
  22. <!-- Testimonial Wrapper -->
  23. <div id="testimonies">
  24. <div class="testimonies-inner">
  25. <div class="testimony-item active">
  26. <div class="testimony-msg">Nulla tellus ipsum, tincidunt id nulla sit amet, placerat cursus sapien. Pellentesque vel nisi at sapien lobortis imperdiet eu sed augue. Vivamus blandit sollicitudin dolor in vehicula. Mauris vestibulum porttitor est.</div>
  27. <div class="testimony-quote"><span class="material-symbols-outlined">format_quote</span></div>
  28. <div class="testimony-assignatory">Mark Cooper</div>
  29. <div class="testimony-assignatory-sub"> XYZ Corp. CEO</div>
  30. </div>
  31. <div class="testimony-item">
  32. <div class="testimony-msg">Phasellus in nisi cursus, interdum est a, accumsan nisi. Nam efficitur elit a arcu consequat, in sagittis lacus dictum.</div>
  33. <div class="testimony-quote"><span class="material-symbols-outlined">format_quote</span></div>
  34. <div class="testimony-assignatory">Claire Blake</div>
  35. <div class="testimony-assignatory-sub">TestTech Founder</div>
  36. </div>
  37. <div class="testimony-item">
  38. <div class="testimony-msg">Mauris ipsum ligula, venenatis quis mi ac, aliquam aliquam sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras at imperdiet lorem.</div>
  39. <div class="testimony-quote"><span class="material-symbols-outlined">format_quote</span></div>
  40. <div class="testimony-assignatory">Samantha Lou</div>
  41. <div class="testimony-assignatory-sub">FullStack Developer</div>
  42. </div>
  43. </div>
  44. <div class="testimony-item-prev">
  45. <button class="testimony-item-prev-btn"></button>
  46. </div>
  47. <div class="testimony-item-next">
  48. <button class="testimony-item-next-btn"></button>
  49. </div>
  50. </div>
  51. <!-- Testimonial Wrapper -->
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. <script src="script.js"></script>
  57. </body>
  58. </html>

Stylesheet

The script below is the CSS file script known as style.css. The file contains the custom styles or design codes of some of the page layout and component elements.

  1. @import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@200&family=Space+Mono&display=swap" rel="stylesheet');
  2. @import url('https://fonts.googleapis.com/css2?family=Tillana:wght@400;600&display=swap');
  3. :root{
  4. --space-mono-font: 'Space Mono', monospace;
  5. --border-dark-subtle: #373838;
  6. --font-tillana:'Tillana', cursive;
  7. }
  8. *{
  9. box-sizing: border-box;
  10. }
  11. body *{
  12. font-family: var(--space-mono-font);
  13. }
  14. /**
  15. Page Design
  16. */
  17. body,
  18. html{
  19. height: 100%;
  20. width: 100%;
  21. margin: 0;
  22. padding: 0;
  23. }
  24. body{
  25. background-color: #282A3A;
  26. }
  27. .page-title{
  28. font-size: 2.5rem;
  29. font-weight: 500;
  30. color: #fff;
  31. letter-spacing: 3px;
  32. font-family: var(--secular-font);
  33. text-align: center;
  34. text-shadow: 0px 0px 3px #2020208c;
  35. }
  36. .border-dark-subtle{
  37. border-color: var(--border-dark-subtle) !important;
  38. }
  39.  
  40. /* Testimonies */
  41. div#testimonies {
  42. position: relative;
  43. width: 100%;
  44. margin: auto;
  45. }
  46. /* Testimony Items Holder*/
  47. div#testimonies .testimonies-inner{
  48. position: relative;
  49. width: 100%;
  50. overflow: hidden;
  51. display: flex;
  52. box-shadow: 0px 0px 8px #7a747442;
  53. }
  54. /* Testimony Item Container*/
  55. div#testimonies .testimonies-inner .testimony-item{
  56. position: absolute;
  57. width: 100%;
  58. top: 0;
  59. left: 0;
  60. display: none;
  61. background: #3a3a3a;
  62. padding: 2em 1.5em;
  63. border: 2px solid #323232;
  64. }
  65. /* Testimony Item child Elements*/
  66. div#testimonies .testimonies-inner .testimony-item * {
  67. color: #c7c7c7;
  68.  
  69. }
  70. /* Active Testimony Item*/
  71. div#testimonies .testimonies-inner .testimony-item.active{
  72. display: block;
  73. }
  74. /* Active Testimony Text*/
  75. #testimonies .testimony-msg{
  76. font-family: var(--font-tillana);
  77. font-weight: 600;
  78. font-size: 1.2rem;
  79. text-align: center;
  80. }
  81. /* Testimony Item Quote and assignatory*/
  82. #testimonies .testimony-quote,
  83. #testimonies .testimony-assignatory,
  84. #testimonies .testimony-assignatory-sub{
  85. text-align: center;
  86. font-weight: 400;
  87. font-family: var(--font-tillana);
  88. }
  89. #testimonies .testimony-assignatory-sub{
  90. font-size: .8rem;
  91. }
  92. #testimonies .testimony-quote .material-symbols-outlined{
  93. font-size: 2.8rem;
  94. color: #c7c7c7;
  95. }
  96.  
  97. /* Testimonies Slider Navigation*/
  98. #testimonies .testimony-item-prev,
  99. #testimonies .testimony-item-next{
  100. position: absolute;
  101. top: 0;
  102. height: calc(100%);
  103. width: 50px;
  104. z-index: 2;
  105. display: flex;
  106. align-items: center;
  107. justify-content: center;
  108. }
  109. #testimonies .testimony-item-prev{
  110. left: -50px;
  111. }
  112. #testimonies .testimony-item-next{
  113. right: -50px;
  114. }
  115.  
  116. /* Testimonies Slider Navigation Buttons*/
  117. #testimonies .testimony-item-prev .testimony-item-prev-btn,
  118. #testimonies .testimony-item-next .testimony-item-next-btn{
  119. width: 35px;
  120. height: 35px;
  121. padding: unset;
  122. border: 5px solid #9f9f9f;
  123. border-right: unset;
  124. border-bottom: unset;
  125. background-color: transparent;
  126. transition: transform .2s ease-in-out;
  127. }
  128. #testimonies .testimony-item-prev .testimony-item-prev-btn:hover,
  129. #testimonies .testimony-item-next .testimony-item-next-btn:hover{
  130. transform: scale(1.2);
  131. }
  132. #testimonies .testimony-item-prev .testimony-item-prev-btn{
  133. rotate: -45deg;
  134. }
  135. #testimonies .testimony-item-next .testimony-item-next-btn{
  136. rotate: 135deg;
  137. }
  138.  
  139. /* Sliding Animation */
  140. div#testimonies .testimonies-inner .testimony-item.slide-start.slide-next{
  141. display: block;
  142. animation:slideToLeft .3s ease-in-out forwards;
  143. }
  144. @keyframes slideToLeft {
  145. 0%{
  146. transform: translateX(0%)
  147. }
  148. 50%, 100%{
  149. transform: translateX(-100%)
  150. }
  151.  
  152. }
  153. div#testimonies .testimonies-inner .testimony-item.slide-end.slide-next{
  154. display: block;
  155. animation:slideFromRight .3s ease-in-out forwards;
  156. }
  157. @keyframes slideFromRight {
  158. 0%{
  159. transform: translateX(100%)
  160. }
  161. 50%, 100%{
  162. transform: translateX(0%)
  163. }
  164. }
  165. div#testimonies .testimonies-inner .testimony-item.slide-start.slide-prev{
  166. display: block;
  167. animation:slideToRight .3s ease-in-out forwards;
  168. }
  169. @keyframes slideToRight {
  170. 0%{
  171. transform: translateX(0%)
  172. }
  173. 50%, 100%{
  174. transform: translateX(100%)
  175. }
  176.  
  177. }
  178. div#testimonies .testimonies-inner .testimony-item.slide-end.slide-prev{
  179. display: block;
  180. animation:slideFromLeft .3s ease-in-out forwards;
  181. }
  182. @keyframes slideFromLeft {
  183. 0%{
  184. transform: translateX(-100%)
  185. }
  186. 50%, 100%{
  187. transform: translateX(0%)
  188. }
  189. }

JavaScript

Lastly, here is the JavaScript file script known as script.js. It contains the JS scripts such as event listeners and the component's sliding navigation function codes.

  1. /**
  2. * Get testimonial container Height
  3. * */
  4.  
  5. //Default Container Height
  6. var containerHeight = 0;
  7. // Select All Items
  8. var testimonialItems = document.querySelectorAll('#testimonies .testimony-item')
  9. testimonialItems.forEach(el => {
  10. // Check if current Item is longer the the stored containerHeight
  11. console.log(el.clientHeight)
  12. if(el.clientHeight > containerHeight)
  13. containerHeight = el.clientHeight;
  14. })
  15.  
  16. // Update Testimonial Container Height
  17. document.querySelector('#testimonies .testimonies-inner').style.height = `${containerHeight+50}px`
  18. testimonialItems.forEach(el => {
  19. el.style.height = `100%`
  20. })
  21. var _prev = document.querySelector('#testimonies .testimony-item-prev-btn')
  22. var _next = document.querySelector('#testimonies .testimony-item-next-btn')
  23. _next.addEventListener('click', e=>{
  24. e.preventDefault()
  25. var currentActive = document.querySelector('#testimonies .testimony-item.active')
  26. if(currentActive != null){
  27. var nextSibling = currentActive.nextElementSibling || document.querySelector('#testimonies .testimony-item:nth-child(1)')
  28. if(nextSibling != null){
  29. _prev.setAttribute('disabled',true)
  30. _next.setAttribute('disabled',true)
  31. currentActive.classList.remove('active')
  32. nextSibling.classList.add('active')
  33. // if(.)
  34. currentActive.classList.add('slide-next','slide-start')
  35. nextSibling.classList.add('slide-next','slide-end')
  36. setTimeout(()=>{
  37. currentActive.classList.remove('slide-next','slide-start')
  38. nextSibling.classList.remove('slide-next','slide-end')
  39. _prev.removeAttribute('disabled')
  40. _next.removeAttribute('disabled')
  41. },300)
  42. }
  43. }
  44. })
  45. _prev.addEventListener('click', e=>{
  46. e.preventDefault()
  47. var currentActive = document.querySelector('#testimonies .testimony-item.active')
  48. if(currentActive != null){
  49. var prevSibling = currentActive.previousElementSibling || document.querySelector('#testimonies .testimony-item:nth-last-child(1)')
  50. if(prevSibling != null){
  51. _prev.setAttribute('disabled',true)
  52. _next.setAttribute('disabled',true)
  53. currentActive.classList.remove('active')
  54. prevSibling.classList.add('active')
  55. // if(.)
  56. currentActive.classList.add('slide-prev','slide-start')
  57. prevSibling.classList.add('slide-prev','slide-end')
  58. setTimeout(()=>{
  59. currentActive.classList.remove('slide-prev','slide-start')
  60. prevSibling.classList.remove('slide-prev','slide-end')
  61. _prev.removeAttribute('disabled')
  62. _next.removeAttribute('disabled')
  63. },300)
  64. }
  65. }
  66. })

Snapshots

Here are the snapshots of the overall result of the web page scripts that I provided above.

Page Interface

Custom Testimonial Slider using JS

Testimonial Slider Component

Custom Testimonial Slider using JS

Slide to the Next Item

Custom Testimonial Slider using JS

Slide to Previous Item

Custom Testimonial Slider using JS

I have also provided the complete source code zip file of the web page scripts that I provided above on this website. You can download the file by clicking the download button located below this tutorial's content. It is free to download. Feel free to download and modify it the way you wanted.

That's it! I hope this Creating a Custom Testimonial Slider using CSS and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment