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

Creating a Responsive Custom ToolTips using HTML, CSS, and JavaScript Tutorial

iseeulikebleach

Forum Architect
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
50
Likes
119
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you can learn how to create a Responsive Custom ToolTip using HTML, CSS, and JavaScript. The tutorial aims to provide students and beginners with a reference for learning to create or looking for a script of the Responsive ToolTip Component. Here, I will be providing reusable scripts of this component and a sample web page that demonstrate the Custom Tooltip Component.

What is ToolTip?

A ToolTip is sort of a popup message or text content describing the hovered element or the cursor is positioned over the element. For example, a program page has multiple buttons that show only icons or images and developers often implement a tooltip component to the buttons to describe the purpose/name/action of the button.

How to Create a Responsive Custom ToolTip?

In HTML, there is a built-in tag or element attribute called title which is used for describing the link, icon, etc. But some developers often use a custom tooltip so they can design the tooltip element that suits the design of their program or so they can use HTML string. We can simply create a custom ToolTip by creating an HTML element for the wrapper or container of the tooltip content, design the tooltip element with CSS, and make it function using Pure JavaScript. Check out the following reusable scripts that I created to understand it more.

Reusable Scripts

The scripts below are the reusable scripts that I created the result into a custom ToolTip for websites or web applications. It outputs a responsive tooltip that automatically repositions for any device screen (desktop, laptop, and mobile)

Stylesheet Script

Here's the CSS file script that designs the Tooltip Wrapper/Container and content elements.

  1. /* Tooltip Wrapper */
  2. .tooltip-wrapper

    {
  3. position

    :

    absolute

    ;
  4. background

    :

    #ffffff

    ;
  5. padding

    :

    0.35em

    0.75em

    ;
  6. border

    :

    1px

    solid

    #e3e2e2

    ;
  7. box-shadow

    :

    0px

    0px

    3px

    #7e7e7e26

    ;
  8. max-width

    :

    300px

    ;
  9. min-width

    :

    50px

    ;
  10. border-radius

    :

    5px

    ;
  11. z-index

    :

    99

    ;
  12. }

  13. /* Tooltip pointer */
  14. .tooltip-wrapper

    :

    before

    {
  15. content

    :

    ""

    ;
  16. position

    :

    absolute

    ;
  17. width

    :

    1.3rem

    ;
  18. height

    :

    1.3rem

    ;
  19. z-index

    :

    99

    ;
  20. background

    :

    #fff

    ;
  21. rotate

    :

    45deg

    ;
  22. }
  23. /* Tooltip pointer for Right Position */
  24. .tooltip-wrapper[

    data-position=

    "right"

    ]

    :

    before

    {
  25. left

    :

    -0.65rem

    ;
  26. top

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  27. }
  28. /* Tooltip pointer for Left Position */
  29. .tooltip-wrapper[

    data-position=

    "left"

    ]

    :

    before

    {
  30. right

    :

    -0.65rem

    ;
  31. top

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  32. }
  33. /* Tooltip pointer for Top Position */
  34. .tooltip-wrapper[

    data-position=

    "top"

    ]

    :

    before

    {
  35. bottom

    :

    -0.65rem

    ;
  36. right

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  37. }
  38. /* Tooltip pointer for Bottom Position */
  39. .tooltip-wrapper[

    data-position=

    "bottom"

    ]

    :

    before

    {
  40. top

    :

    -0.65rem

    ;
  41. right

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  42. }
  43. /* Tooltip Content Block */
  44. .tooltip-wrapper

    .tooltip-content

    {
  45. position

    :

    relative

    ;
  46. backface-visibility

    :

    hidden

    ;
  47. z-index

    :

    100

    ;
  48. }

JavaScript

Here's the JavaScript file script that makes the tooltip functional and creates the elements tooltips.

  1. /**
  2. * Creating Tooltip Wrapper Element
  3. */
  4. const

    tooltipWrapper =

    document.createElement

    (

    'div'

    )
  5. tooltipWrapper.classList

    .add

    (

    'tooltip-wrapper'

    )
  6. tooltipWrapper.innerHTML

    =

    `<

    div class

    =

    "tooltip-content"

    ></

    div>

    `

  7. /**
  8. * Selecting all elements that has tooltip Attribute
  9. */
  10. const

    tooltipEls =

    document.querySelectorAll

    (

    '[data-tooltip]'

    )

  11. /**
  12. * Show Tootlip of the Element
  13. * @param {dom} el - selected comment
  14. * @param {dom} toolTipEl - cloned ToolTip Element
  15. */
  16. const

    showToolTip =

    (

    el,

    toolTipEl)

    =>

    {
  17. // Getting the Element Tooltip Element
  18. var

    _content =

    el.dataset

    .tooltip

    ||

    ""

    ;
  19. // Getting the Element Tooltip Element Position
  20. var

    _pos =

    el.dataset

    .tooltipPosition

    ||

    "right"

    ;
  21. // Getting the ViewPort Width
  22. var

    _vpWidth =

    window.outerWidth
  23. // Getting the ViewPort Height
  24. var

    _vpHeight =

    window.outerHeight
  25. // Setting up the Tooltip Content
  26. toolTipEl.querySelector

    (

    '.tooltip-content'

    )

    .innerText

    =

    _content
  27. // Setting up the Tooltip Position Attribute
  28. toolTipEl.dataset

    .position

    =

    _pos

  29. // Getting the Element's Bounding Rectangle
  30. var

    rect =

    el.getBoundingClientRect

    (

    )

  31. // Temporarily hide the tooltip
  32. toolTipEl.style

    .visibility

    =

    `hidden`
  33. // Append the Tooltip Element to the document body
  34. document.body

    .appendChild

    (

    toolTipEl)
  35. // Tooltip Height
  36. var

    tipHeight =

    toolTipEl.clientHeight
  37. // Tooltip Width
  38. var

    tipWidth =

    toolTipEl.clientWidth

  39. if

    (

    _pos ==

    'right'

    )

    {
  40. // Setting Up the Tooltip Postion at the right side of the element
  41. toolTipEl.style

    .left

    =

    `calc(

    ${

    rect.right

    }

    px +

    1.3rem)

    `
  42. toolTipEl.style

    .top

    =

    `${

    (

    rect.top

    +

    (

    rect.height

    /

    2

    )

    )

    -

    (

    tipHeight /

    2

    )

    }

    px`
  43. }
  44. if

    (

    _pos ==

    'left'

    )

    {
  45. // Setting Up the Tooltip Postion at the left side of the element
  46. toolTipEl.style

    .left

    =

    `calc(

    ${

    rect.left

    -

    tipWidth}

    px -

    1.3rem)

    `
  47. toolTipEl.style

    .top

    =

    `${

    (

    rect.top

    +

    (

    rect.height

    /

    2

    )

    )

    -

    (

    tipHeight /

    2

    )

    }

    px`
  48. }
  49. if

    (

    _pos ==

    'top'

    )

    {
  50. // Setting Up the Tooltip Postion at the top side of the element
  51. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  52. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.top

    -

    tipHeight}

    px -

    1.3rem)

    `
  53. }
  54. if

    (

    _pos ==

    'bottom'

    )

    {
  55. // Setting Up the Tooltip Postion at the bottom side of the element
  56. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  57. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.bottom

    }

    px +

    1.3rem)

    `
  58. }
  59. // Getting the Tooltip Element Bounding Rectangle
  60. var

    ttRect =

    toolTipEl.getBoundingClientRect

    (

    )

  61. /**
  62. * Making the Tootltip Responsive
  63. */
  64. if

    (

    (

    ttRect.left

    +

    ttRect.width

    )

    >

    _vpWidth ||

    ttRect.left

    <

    0

    ||

    (

    (

    (

    ttRect.top

    +

    ttRect.height

    )

    >

    _vpHeight ||

    ttRect.top

    <

    0

    )

    &&

    (

    _pos ==

    "top"

    ||

    _pos ==

    "bottom"

    )

    )

    )

    {
  65. if

    (

    (

    rect.top

    -

    ttRect.height

    )

    >

    0

    )

    {
  66. // Repositioning the Tooltip to the top of element becuase it overflows from the page viewport
  67. toolTipEl.dataset

    .position

    =

    `top`
  68. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  69. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.top

    -

    tipHeight}

    px -

    1.3rem)

    `
  70. }

    else

    {
  71. // Repositioning the Tooltip to the bottom of element becuase it overflows from the page viewport
  72. toolTipEl.dataset

    .position

    =

    `bottom`
  73. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  74. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.bottom

    }

    px +

    1.3rem)

    `
  75. }
  76. }
  77. toolTipEl.style

    .visibility

    =

    `visible`

  78. }

  79. /**
  80. * Closing the Tooltip
  81. * @param {dom} toolTipEl - tooltip element
  82. */
  83. const

    closeToolTip =

    (

    toolTipEl)

    =>

    {
  84. toolTipEl.remove

    (

    )
  85. }
  86. tooltipEls.forEach

    (

    el =>

    {
  87. el.addEventListener

    (

    'mouseenter'

    ,

    e=>

    {
  88. // Trigger tooltip element to show when the element is being hovered
  89. var

    toolTipEl =

    tooltipWrapper.cloneNode

    (

    true

    )
  90. showToolTip(

    el,

    toolTipEl)
  91. el.addEventListener

    (

    'mouseout'

    ,

    e=>

    {
  92. closeToolTip(

    toolTipEl)
  93. }

    )
  94. // Closing the tootlip if client press the 'Esc' (Escape) key
  95. document.body

    .addEventListener

    (

    'keyup'

    ,

    e =>

    {
  96. if

    (

    e.key

    ==

    'Escape'

    ||

    e.code

    ==

    'Escape'

    ||

    e.keyCode

    ==

    27

    ||

    e.which

    ==

    27

    )
  97. closeToolTip(

    toolTipEl)
  98. }

    )
  99. // Closing the tootlip if the page is scrolled
  100. window.addEventListener

    (

    'scroll'

    ,

    function

    (

    e)

    {
  101. closeToolTip(

    toolTipEl)
  102. }

    ,

    true

    )

    ;
  103. // Closing the tootlip if the page is resized
  104. window.addEventListener

    (

    'resize'

    ,

    function

    (

    e)

    {
  105. closeToolTip(

    toolTipEl)
  106. }

    ,

    true

    )

    ;
  107. }

    )
  108. }

    )

Syntax

Here is the sample HTML syntax to implement the custom ToolTip to a certain element.

  1. <button

    type

    =

    "button"

    data-tooltip=

    "Nullam nec dapibus orci, eget volutpat lorem."

    data-tooltip-position=

    "top"

    >

    Hover Me</

    button

    >

Sample Web Page

Here are the scripts that result in a simple web page that demonstrate the custom tooltip scripts that I provided above.

HTML

The following HTML script is known as index.html.

  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

    >

    Custom ToolTips - HTML, CSS and JS</

    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"

    >

    Custom ToolTips using HTML, CSS, and JavaScript</

    div

    >
  19. <hr

    style

    =

    "margin:auto; width:25px"

    class

    =

    "border-light opacity-100"

    >
  20. </

    div

    >
  21. <!-- Sample Elements with Tooltips Wrapper -->
  22. <div

    class

    =

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

    >
  23. <div

    class

    =

    "row"

    >
  24. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  25. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

    data-tooltip-position=

    "right"

    >

    Tooltip #1</

    div

    >
  26. </

    div

    >
  27. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  28. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Cras non pulvinar nibh. Phasellus volutpat sem eu magna volutpat, et eleifend ante consectetur."

    data-tooltip-position=

    "bottom"

    >

    Tooltip #2</

    div

    >
  29. </

    div

    >
  30. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  31. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Suspendisse potenti. Phasellus sollicitudin pulvinar sem eget egestas."

    data-tooltip-position=

    "left"

    >

    Tooltip #3</

    div

    >
  32. </

    div

    >
  33. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  34. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."

    data-tooltip-position=

    "right"

    >

    Tooltip #4</

    div

    >
  35. </

    div

    >
  36. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  37. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Cras in dui sit amet nisi vehicula ultrices. Vivamus ac augue eleifend, suscipit velit vel, semper justo. Duis sit amet ligula ac nibh hendrerit pellentesque."

    data-tooltip-position=

    "top"

    >

    Tooltip #5</

    div

    >
  38. </

    div

    >
  39. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  40. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Fusce ipsum magna, hendrerit eu turpis vitae, vulputate varius orci."

    data-tooltip-position=

    "left"

    >

    Tooltip #6</

    div

    >
  41. </

    div

    >
  42. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  43. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Nullam nec dapibus orci, eget volutpat lorem."

    >

    Tooltip #7</

    div

    >
  44. </

    div

    >
  45. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  46. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Quisque elementum ipsum nec rutrum pulvinar."

    data-tooltip-position=

    "bottom"

    >

    Tooltip #8</

    div

    >
  47. </

    div

    >
  48. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-12"

    >
  49. <div

    class

    =

    "tootlip-sample-item"

    data-tooltip=

    "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."

    data-tooltip-position=

    "left"

    >

    Tooltip #9</

    div

    >
  50. </

    div

    >
  51. </

    div

    >
  52. </

    div

    >
  53. <!-- Sample Elements with Tooltips Wrapper -->
  54. </

    div

    >
  55. <script

    src

    =

    "script.js"

    ></

    script

    >
  56. </

    body

    >
  57. </

    html

    >

CSS

The following CSS script is known as style.css.

  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?f...00;1,500;1,600;1,700;1,800;1,900&display=swap'

    )

    ;

  2. *

    {
  3. box-sizing

    :

    border-box

    ;
  4. font-family

    :

    'Exo 2'

    ,

    sans-serif

    ;
  5. }
  6. /**
  7. Page Design
  8. */
  9. body,
  10. html{
  11. height

    :

    100%

    ;
  12. width

    :

    100%

    ;
  13. margin

    :

    0

    ;
  14. padding

    :

    0

    ;
  15. overflow-x

    :

    hidden

    ;
  16. }
  17. body{
  18. background-color

    :

    #282A3A

    ;
  19. }
  20. .page-title{
  21. font-size

    :

    2.5rem

    ;
  22. font-weight

    :

    500

    ;
  23. color

    :

    #fff

    ;
  24. letter-spacing

    :

    3px

    ;
  25. font-family

    :

    var

    (

    --secular-font

    )

    ;
  26. text-align

    :

    center

    ;
  27. text-shadow

    :

    0px

    0px

    3px

    #2020208c

    ;
  28. }
  29. .border-dark-subtle{
  30. border-color

    :

    var

    (

    --border-dark-subtle

    )

    !important;
  31. }

  32. /* Sample Elements Design */

  33. .tootlip-sample-item

    {
  34. margin

    :

    0.5em

    auto

    ;
  35. width

    :

    80%

    ;
  36. padding

    :

    0.5em

    0.75em

    ;
  37. background

    :

    #4a9de3

    ;
  38. color

    :

    #fff

    ;
  39. border-radius

    :

    10px

    ;
  40. box-shadow

    :

    2px

    2px

    6px

    #9d9b9b

    ;
  41. font-weight

    :

    600

    ;
  42. text-align

    :

    center

    ;
  43. cursor

    :

    pointer

    ;
  44. }

  45. /* Tooltip Wrapper */
  46. .tooltip-wrapper

    {
  47. position

    :

    absolute

    ;
  48. background

    :

    #ffffff

    ;
  49. padding

    :

    0.35em

    0.75em

    ;
  50. border

    :

    1px

    solid

    #e3e2e2

    ;
  51. box-shadow

    :

    0px

    0px

    3px

    #7e7e7e26

    ;
  52. max-width

    :

    300px

    ;
  53. min-width

    :

    50px

    ;
  54. border-radius

    :

    5px

    ;
  55. z-index

    :

    99

    ;
  56. }

  57. /* Tooltip pointer */
  58. .tooltip-wrapper

    :

    before

    {
  59. content

    :

    ""

    ;
  60. position

    :

    absolute

    ;
  61. width

    :

    1.3rem

    ;
  62. height

    :

    1.3rem

    ;
  63. z-index

    :

    99

    ;
  64. background

    :

    #fff

    ;
  65. rotate

    :

    45deg

    ;
  66. }
  67. /* Tooltip pointer for Right Position */
  68. .tooltip-wrapper[

    data-position=

    "right"

    ]

    :

    before

    {
  69. left

    :

    -0.65rem

    ;
  70. top

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  71. }
  72. /* Tooltip pointer for Left Position */
  73. .tooltip-wrapper[

    data-position=

    "left"

    ]

    :

    before

    {
  74. right

    :

    -0.65rem

    ;
  75. top

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  76. }
  77. /* Tooltip pointer for Top Position */
  78. .tooltip-wrapper[

    data-position=

    "top"

    ]

    :

    before

    {
  79. bottom

    :

    -0.65rem

    ;
  80. right

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  81. }
  82. /* Tooltip pointer for Bottom Position */
  83. .tooltip-wrapper[

    data-position=

    "bottom"

    ]

    :

    before

    {
  84. top

    :

    -0.65rem

    ;
  85. right

    :

    calc

    (

    50%

    - 1.3rem

    +

    0.7em

    )

    ;
  86. }
  87. /* Tooltip Content Block */
  88. .tooltip-wrapper

    .tooltip-content

    {
  89. position

    :

    relative

    ;
  90. backface-visibility

    :

    hidden

    ;
  91. z-index

    :

    100

    ;
  92. }

JavaScript

The following JS script is known as script.js.

  1. /**
  2. * Creating Tooltip Wrapper Element
  3. */
  4. const

    tooltipWrapper =

    document.createElement

    (

    'div'

    )
  5. tooltipWrapper.classList

    .add

    (

    'tooltip-wrapper'

    )
  6. tooltipWrapper.innerHTML

    =

    `<

    div class

    =

    "tooltip-content"

    ></

    div>

    `

  7. /**
  8. * Selecting all elements that has tooltip Attribute
  9. */
  10. const

    tooltipEls =

    document.querySelectorAll

    (

    '[data-tooltip]'

    )

  11. /**
  12. * Show Tootlip of the Element
  13. * @param {dom} el - selected comment
  14. * @param {dom} toolTipEl - cloned ToolTip Element
  15. */
  16. const

    showToolTip =

    (

    el,

    toolTipEl)

    =>

    {
  17. // Getting the Element Tooltip Element
  18. var

    _content =

    el.dataset

    .tooltip

    ||

    ""

    ;
  19. // Getting the Element Tooltip Element Position
  20. var

    _pos =

    el.dataset

    .tooltipPosition

    ||

    "right"

    ;
  21. // Getting the ViewPort Width
  22. var

    _vpWidth =

    window.outerWidth
  23. // Getting the ViewPort Height
  24. var

    _vpHeight =

    window.outerHeight
  25. // Setting up the Tooltip Content
  26. toolTipEl.querySelector

    (

    '.tooltip-content'

    )

    .innerText

    =

    _content
  27. // Setting up the Tooltip Position Attribute
  28. toolTipEl.dataset

    .position

    =

    _pos

  29. // Getting the Element's Bounding Rectangle
  30. var

    rect =

    el.getBoundingClientRect

    (

    )

  31. // Temporarily hide the tooltip
  32. toolTipEl.style

    .visibility

    =

    `hidden`
  33. // Append the Tooltip Element to the document body
  34. document.body

    .appendChild

    (

    toolTipEl)
  35. // Tooltip Height
  36. var

    tipHeight =

    toolTipEl.clientHeight
  37. // Tooltip Width
  38. var

    tipWidth =

    toolTipEl.clientWidth

  39. if

    (

    _pos ==

    'right'

    )

    {
  40. // Setting Up the Tooltip Postion at the right side of the element
  41. toolTipEl.style

    .left

    =

    `calc(

    ${

    rect.right

    }

    px +

    1.3rem)

    `
  42. toolTipEl.style

    .top

    =

    `${

    (

    rect.top

    +

    (

    rect.height

    /

    2

    )

    )

    -

    (

    tipHeight /

    2

    )

    }

    px`
  43. }
  44. if

    (

    _pos ==

    'left'

    )

    {
  45. // Setting Up the Tooltip Postion at the left side of the element
  46. toolTipEl.style

    .left

    =

    `calc(

    ${

    rect.left

    -

    tipWidth}

    px -

    1.3rem)

    `
  47. toolTipEl.style

    .top

    =

    `${

    (

    rect.top

    +

    (

    rect.height

    /

    2

    )

    )

    -

    (

    tipHeight /

    2

    )

    }

    px`
  48. }
  49. if

    (

    _pos ==

    'top'

    )

    {
  50. // Setting Up the Tooltip Postion at the top side of the element
  51. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  52. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.top

    -

    tipHeight}

    px -

    1.3rem)

    `
  53. }
  54. if

    (

    _pos ==

    'bottom'

    )

    {
  55. // Setting Up the Tooltip Postion at the bottom side of the element
  56. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  57. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.bottom

    }

    px +

    1.3rem)

    `
  58. }
  59. // Getting the Tooltip Element Bounding Rectangle
  60. var

    ttRect =

    toolTipEl.getBoundingClientRect

    (

    )

  61. /**
  62. * Making the Tootltip Responsive
  63. */
  64. if

    (

    (

    ttRect.left

    +

    ttRect.width

    )

    >

    _vpWidth ||

    ttRect.left

    <

    0

    ||

    (

    (

    (

    ttRect.top

    +

    ttRect.height

    )

    >

    _vpHeight ||

    ttRect.top

    <

    0

    )

    &&

    (

    _pos ==

    "top"

    ||

    _pos ==

    "bottom"

    )

    )

    )

    {
  65. if

    (

    (

    rect.top

    -

    ttRect.height

    )

    >

    0

    )

    {
  66. // Repositioning the Tooltip to the top of element becuase it overflows from the page viewport
  67. toolTipEl.dataset

    .position

    =

    `top`
  68. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  69. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.top

    -

    tipHeight}

    px -

    1.3rem)

    `
  70. }

    else

    {
  71. // Repositioning the Tooltip to the bottom of element becuase it overflows from the page viewport
  72. toolTipEl.dataset

    .position

    =

    `bottom`
  73. toolTipEl.style

    .left

    =

    `${

    (

    rect.left

    +

    (

    rect.width

    /

    2

    )

    )

    -

    (

    tipWidth /

    2

    )

    }

    px`
  74. toolTipEl.style

    .top

    =

    `calc(

    ${

    rect.bottom

    }

    px +

    1.3rem)

    `
  75. }
  76. }
  77. toolTipEl.style

    .visibility

    =

    `visible`

  78. }

  79. /**
  80. * Closing the Tooltip
  81. * @param {dom} toolTipEl - tooltip element
  82. */
  83. const

    closeToolTip =

    (

    toolTipEl)

    =>

    {
  84. toolTipEl.remove

    (

    )
  85. }
  86. tooltipEls.forEach

    (

    el =>

    {
  87. el.addEventListener

    (

    'mouseenter'

    ,

    e=>

    {
  88. // Trigger tooltip element to show when the element is being hovered
  89. var

    toolTipEl =

    tooltipWrapper.cloneNode

    (

    true

    )
  90. showToolTip(

    el,

    toolTipEl)
  91. el.addEventListener

    (

    'mouseout'

    ,

    e=>

    {
  92. closeToolTip(

    toolTipEl)
  93. }

    )
  94. // Closing the tootlip if client press the 'Esc' (Escape) key
  95. document.body

    .addEventListener

    (

    'keyup'

    ,

    e =>

    {
  96. if

    (

    e.key

    ==

    'Escape'

    ||

    e.code

    ==

    'Escape'

    ||

    e.keyCode

    ==

    27

    ||

    e.which

    ==

    27

    )
  97. closeToolTip(

    toolTipEl)
  98. }

    )
  99. // Closing the tootlip if the page is scrolled
  100. window.addEventListener

    (

    'scroll'

    ,

    function

    (

    e)

    {
  101. closeToolTip(

    toolTipEl)
  102. }

    ,

    true

    )

    ;
  103. // Closing the tootlip if the page is resized
  104. window.addEventListener

    (

    'resize'

    ,

    function

    (

    e)

    {
  105. closeToolTip(

    toolTipEl)
  106. }

    ,

    true

    )

    ;
  107. }

    )
  108. }

    )


Snapshots

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

Top Position

Bottom Position

Left Position

Right Position

There you go! I have provided also the complete source code zip file of the sample web page script I provided above on this website. Feel free to download and modify it to do some experiments. To download it, kindly click the download button located below this tutorial's content.

That's it! I hope this Creating a Responsive Custom ToolTips using HTML, 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 the hidden content.
 

452,496

335,994

336,002

Top