• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Custom Testimonial Slider using CSS and JavaScript Tutorial

sasa

Meme Lord
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
108
Likes
152
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    HTML CSS and JS - Custom Testimonial Slider</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0"

    /

    >
  11. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    >
  12. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  13. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"

    ></

    script

    >
  14. </

    head

    >
  15. <body

    >
  16. <div

    class

    =

    "content-md-lg py-3"

    >
  17. <div

    class

    =

    "col-lg-8 col-md-10 col-sm-12 col-12 mx-auto"

    >
  18. <div

    class

    =

    "page-title"

    >

    Creating a Custom Testimonial Slider HTML, CSS, and JavaScript</

    div

    >
  19. <hr

    style

    =

    "margin:auto; width:25px"

    class

    =

    "border-light opacity-100"

    >
  20. </

    div

    >
  21. <div

    class

    =

    "container-lg"

    >
  22. <div

    class

    =

    "row py-3 my-5"

    >
  23. <div

    class

    =

    "col-lg-5 col-md-5 col-sm-10 col-12 mx-auto"

    >
  24. <!-- Testimonial Wrapper -->
  25. <div

    id

    =

    "testimonies"

    >
  26. <div

    class

    =

    "testimonies-inner"

    >
  27. <div

    class

    =

    "testimony-item active"

    >
  28. <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

    >
  29. <div

    class

    =

    "testimony-quote"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    format_quote</

    span

    ></

    div

    >
  30. <div

    class

    =

    "testimony-assignatory"

    >

    Mark Cooper</

    div

    >
  31. <div

    class

    =

    "testimony-assignatory-sub"

    >

    XYZ Corp. CEO</

    div

    >
  32. </

    div

    >
  33. <div

    class

    =

    "testimony-item"

    >
  34. <div

    class

    =

    "testimony-msg"

    >

    Phasellus in nisi cursus, interdum est a, accumsan nisi. Nam efficitur elit a arcu consequat, in sagittis lacus dictum.</

    div

    >
  35. <div

    class

    =

    "testimony-quote"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    format_quote</

    span

    ></

    div

    >
  36. <div

    class

    =

    "testimony-assignatory"

    >

    Claire Blake</

    div

    >
  37. <div

    class

    =

    "testimony-assignatory-sub"

    >

    TestTech Founder</

    div

    >
  38. </

    div

    >
  39. <div

    class

    =

    "testimony-item"

    >
  40. <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

    >
  41. <div

    class

    =

    "testimony-quote"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    format_quote</

    span

    ></

    div

    >
  42. <div

    class

    =

    "testimony-assignatory"

    >

    Samantha Lou</

    div

    >
  43. <div

    class

    =

    "testimony-assignatory-sub"

    >

    FullStack Developer</

    div

    >
  44. </

    div

    >
  45. </

    div

    >
  46. <div

    class

    =

    "testimony-item-prev"

    >
  47. <button

    class

    =

    "testimony-item-prev-btn"

    ></

    button

    >
  48. </

    div

    >
  49. <div

    class

    =

    "testimony-item-next"

    >
  50. <button

    class

    =

    "testimony-item-next-btn"

    ></

    button

    >
  51. </

    div

    >
  52. </

    div

    >
  53. <!-- Testimonial Wrapper -->
  54. </

    div

    >
  55. </

    div

    >
  56. </

    div

    >
  57. </

    div

    >
  58. <script

    src

    =

    "script.js"

    ></

    script

    >
  59. </

    body

    >
  60. </

    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. /* Testimonies */
  40. div#testimonies

    {
  41. position

    :

    relative

    ;
  42. width

    :

    100%

    ;
  43. margin

    :

    auto

    ;
  44. }
  45. /* Testimony Items Holder*/
  46. div#testimonies

    .testimonies-inner{
  47. position

    :

    relative

    ;
  48. width

    :

    100%

    ;
  49. overflow

    :

    hidden

    ;
  50. display

    :

    flex;
  51. box-shadow

    :

    0px

    0px

    8px

    #7a747442

    ;
  52. }
  53. /* Testimony Item Container*/
  54. div#testimonies

    .testimonies-inner

    .testimony-item{
  55. position

    :

    absolute

    ;
  56. width

    :

    100%

    ;
  57. top

    :

    0

    ;
  58. left

    :

    0

    ;
  59. display

    :

    none

    ;
  60. background

    :

    #3a3a3a

    ;
  61. padding

    :

    2em

    1.5em

    ;
  62. border

    :

    2px

    solid

    #323232

    ;
  63. }
  64. /* Testimony Item child Elements*/
  65. div#testimonies

    .testimonies-inner

    .testimony-item

    *

    {
  66. color

    :

    #c7c7c7

    ;

  67. }
  68. /* Active Testimony Item*/
  69. div#testimonies

    .testimonies-inner

    .testimony-item

    .active{
  70. display

    :

    block

    ;
  71. }
  72. /* Active Testimony Text*/
  73. #testimonies

    .testimony-msg{
  74. font-family

    :

    var

    (

    --font-tillana

    )

    ;
  75. font-weight

    :

    600

    ;
  76. font-size

    :

    1.2rem

    ;
  77. text-align

    :

    center

    ;
  78. }
  79. /* Testimony Item Quote and assignatory*/
  80. #testimonies

    .testimony-quote,
  81. #testimonies

    .testimony-assignatory,
  82. #testimonies

    .testimony-assignatory-sub{
  83. text-align

    :

    center

    ;
  84. font-weight

    :

    400

    ;
  85. font-family

    :

    var

    (

    --font-tillana

    )

    ;
  86. }
  87. #testimonies

    .testimony-assignatory-sub{
  88. font-size

    :

    .8rem

    ;
  89. }
  90. #testimonies

    .testimony-quote

    .material-symbols-outlined{
  91. font-size

    :

    2.8rem

    ;
  92. color

    :

    #c7c7c7

    ;
  93. }

  94. /* Testimonies Slider Navigation*/
  95. #testimonies

    .testimony-item-prev,
  96. #testimonies

    .testimony-item-next{
  97. position

    :

    absolute

    ;
  98. top

    :

    0

    ;
  99. height

    :

    calc

    (

    100%

    )

    ;
  100. width

    :

    50px

    ;
  101. z-index

    :

    2

    ;
  102. display

    :

    flex;
  103. align-items

    :

    center

    ;
  104. justify-content

    :

    center

    ;
  105. }
  106. #testimonies

    .testimony-item-prev{
  107. left

    :

    -50px

    ;
  108. }
  109. #testimonies

    .testimony-item-next{
  110. right

    :

    -50px

    ;
  111. }

  112. /* Testimonies Slider Navigation Buttons*/
  113. #testimonies

    .testimony-item-prev

    .testimony-item-prev-btn,
  114. #testimonies

    .testimony-item-next

    .testimony-item-next-btn{
  115. width

    :

    35px

    ;
  116. height

    :

    35px

    ;
  117. padding

    :

    unset;
  118. border

    :

    5px

    solid

    #9f9f9f

    ;
  119. border-right

    :

    unset;
  120. border-bottom

    :

    unset;
  121. background-color

    :

    transparent

    ;
  122. transition

    :

    transform .2s

    ease-in-out;
  123. }
  124. #testimonies

    .testimony-item-prev

    .testimony-item-prev-btn

    :

    hover

    ,
  125. #testimonies

    .testimony-item-next

    .testimony-item-next-btn

    :

    hover

    {
  126. transform

    :

    scale

    (

    1.2

    )

    ;
  127. }
  128. #testimonies

    .testimony-item-prev

    .testimony-item-prev-btn{
  129. rotate

    :

    -45deg

    ;
  130. }
  131. #testimonies

    .testimony-item-next

    .testimony-item-next-btn{
  132. rotate

    :

    135deg

    ;
  133. }

  134. /* Sliding Animation */
  135. div#testimonies

    .testimonies-inner

    .testimony-item

    .slide-start

    .slide-next{
  136. display

    :

    block

    ;
  137. animation

    :

    slideToLeft .3s

    ease-in-out forwards

    ;
  138. }
  139. @keyframes

    slideToLeft {
  140. 0%

    {
  141. transform

    :

    translateX(

    0%

    )
  142. }
  143. 50%

    ,

    100%

    {
  144. transform

    :

    translateX(

    -100%

    )
  145. }

  146. }
  147. div#testimonies

    .testimonies-inner

    .testimony-item

    .slide-end

    .slide-next{
  148. display

    :

    block

    ;
  149. animation

    :

    slideFromRight .3s

    ease-in-out forwards

    ;
  150. }
  151. @keyframes

    slideFromRight {
  152. 0%

    {
  153. transform

    :

    translateX(

    100%

    )
  154. }
  155. 50%

    ,

    100%

    {
  156. transform

    :

    translateX(

    0%

    )
  157. }
  158. }
  159. div#testimonies

    .testimonies-inner

    .testimony-item

    .slide-start

    .slide-prev{
  160. display

    :

    block

    ;
  161. animation

    :

    slideToRight .3s

    ease-in-out forwards

    ;
  162. }
  163. @keyframes

    slideToRight {
  164. 0%

    {
  165. transform

    :

    translateX(

    0%

    )
  166. }
  167. 50%

    ,

    100%

    {
  168. transform

    :

    translateX(

    100%

    )
  169. }

  170. }
  171. div#testimonies

    .testimonies-inner

    .testimony-item

    .slide-end

    .slide-prev{
  172. display

    :

    block

    ;
  173. animation

    :

    slideFromLeft .3s

    ease-in-out forwards

    ;
  174. }
  175. @keyframes

    slideFromLeft {
  176. 0%

    {
  177. transform

    :

    translateX(

    -100%

    )
  178. }
  179. 50%

    ,

    100%

    {
  180. transform

    :

    translateX(

    0%

    )
  181. }
  182. }

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. //Default Container Height
  5. var

    containerHeight =

    0

    ;
  6. // Select All Items
  7. var

    testimonialItems =

    document.querySelectorAll

    (

    '#testimonies .testimony-item'

    )
  8. testimonialItems.forEach

    (

    el =>

    {
  9. // Check if current Item is longer the the stored containerHeight
  10. console.log

    (

    el.clientHeight

    )
  11. if

    (

    el.clientHeight

    >

    containerHeight)
  12. containerHeight =

    el.clientHeight

    ;
  13. }

    )

  14. // Update Testimonial Container Height
  15. document.querySelector

    (

    '#testimonies .testimonies-inner'

    )

    .style

    .height

    =

    `${

    containerHeight+

    50

    }

    px`
  16. testimonialItems.forEach

    (

    el =>

    {
  17. el.style

    .height

    =

    `100

    %

    `
  18. }

    )
  19. var

    _prev =

    document.querySelector

    (

    '#testimonies .testimony-item-prev-btn'

    )
  20. var

    _next =

    document.querySelector

    (

    '#testimonies .testimony-item-next-btn'

    )
  21. _next.addEventListener

    (

    'click'

    ,

    e=>

    {
  22. e.preventDefault

    (

    )
  23. var

    currentActive =

    document.querySelector

    (

    '#testimonies .testimony-item.active'

    )
  24. if

    (

    currentActive !=

    null

    )

    {
  25. var

    nextSibling =

    currentActive.nextElementSibling

    ||

    document.querySelector

    (

    '#testimonies .testimony-item:nth-child(1)'

    )
  26. if

    (

    nextSibling !=

    null

    )

    {
  27. _prev.setAttribute

    (

    'disabled'

    ,

    true

    )
  28. _next.setAttribute

    (

    'disabled'

    ,

    true

    )
  29. currentActive.classList

    .remove

    (

    'active'

    )
  30. nextSibling.classList

    .add

    (

    'active'

    )
  31. // if(.)
  32. currentActive.classList

    .add

    (

    'slide-next'

    ,

    'slide-start'

    )
  33. nextSibling.classList

    .add

    (

    'slide-next'

    ,

    'slide-end'

    )
  34. setTimeout(

    (

    )

    =>

    {
  35. currentActive.classList

    .remove

    (

    'slide-next'

    ,

    'slide-start'

    )
  36. nextSibling.classList

    .remove

    (

    'slide-next'

    ,

    'slide-end'

    )
  37. _prev.removeAttribute

    (

    'disabled'

    )
  38. _next.removeAttribute

    (

    'disabled'

    )
  39. }

    ,

    300

    )
  40. }
  41. }
  42. }

    )
  43. _prev.addEventListener

    (

    'click'

    ,

    e=>

    {
  44. e.preventDefault

    (

    )
  45. var

    currentActive =

    document.querySelector

    (

    '#testimonies .testimony-item.active'

    )
  46. if

    (

    currentActive !=

    null

    )

    {
  47. var

    prevSibling =

    currentActive.previousElementSibling

    ||

    document.querySelector

    (

    '#testimonies .testimony-item:nth-last-child(1)'

    )
  48. if

    (

    prevSibling !=

    null

    )

    {
  49. _prev.setAttribute

    (

    'disabled'

    ,

    true

    )
  50. _next.setAttribute

    (

    'disabled'

    ,

    true

    )
  51. currentActive.classList

    .remove

    (

    'active'

    )
  52. prevSibling.classList

    .add

    (

    'active'

    )
  53. // if(.)
  54. currentActive.classList

    .add

    (

    'slide-prev'

    ,

    'slide-start'

    )
  55. prevSibling.classList

    .add

    (

    'slide-prev'

    ,

    'slide-end'

    )
  56. setTimeout(

    (

    )

    =>

    {
  57. currentActive.classList

    .remove

    (

    'slide-prev'

    ,

    'slide-start'

    )
  58. prevSibling.classList

    .remove

    (

    'slide-prev'

    ,

    'slide-end'

    )
  59. _prev.removeAttribute

    (

    'disabled'

    )
  60. _next.removeAttribute

    (

    'disabled'

    )
  61. }

    ,

    300

    )
  62. }
  63. }
  64. }

    )

Snapshots

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

Page Interface

Testimonial Slider Component

Slide to the Next Item

Slide to Previous Item

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 =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top