CSS: Adjusting the position of tooltips to align with the triggering element (rest of code is fully functional)

Looking to create a custom tooltip without relying on plugins. The goal is to align the tooltip vertically to the right of the triggering element, in this case, an input field.

The current setup (see Fiddle demo) achieves the desired layout and content for the tooltip, but it lacks the ability to position it correctly on the right side of the input field (currently appearing in the center of the screen).

$(document).ready(function() {
  $('.tooltipClose').on('click', function() {
    $(this).closest('div.tooltip').removeClass('tooltipShow');
  });

  $('.triggerTooltip').on('click', function() {
    $(this).next('div.tooltip').addClass('tooltipShow');
  });
});
.tooltip {
  -moz-transition: opacity 400ms ease-in;
  -webkit-transition: opacity 400ms ease-in;
  bottom: 0;
  font-family: Helvetica, Arial, Verdana, sans-serif;
  left: 0;
  right: 0;
  filter: alpha(opacity=0);
  opacity: 0;
  pointer-events: none;
  position: fixed;
  top: 0;
  transition: opacity 400ms ease-in;
  z-index: 99999;
}
.tooltipClose {
  background: #028dca;
  border-radius: 12px;
  box-shadow: 2px 2px 3px #666;
  color: #fff;
  font-weight: bold;
  line-height: 25px;
  position: absolute;
  right: -12px;
  text-align: center;
  text-decoration: none;
  top: -12px;
  vertical-align: middle;
  width: 24px;
}
.tooltipClose:hover {
  background: #00507e;
}
.tooltipFooter,
.tooltipHeader {
  border-bottom: 2px solid #ccc;
  font-size: 20px;
  font-weight: bold;
  margin: 0;
  padding: 8px 0;
  vertical-align: middle;
}
.tooltipShow {
  filter: alpha(opacity=100);
  opacity: 1;
  pointer-events: auto;
}
.tooltip > div {
  background: #fff;
  border: 3px solid #028dca;
  border-radius: 10px;
  margin: 10% auto;
  padding: 8px 16px;
  position: relative;
  width: 200px;
}
.tooltip > div:after {
  border-bottom: 12px solid transparent;
  border-right: 12px solid #028dca;
  border-top: 12px solid transparent;
  content: '';
  height: 0;
  left: 0;
  margin: -12px;
  position: absolute;
  right: 50%;
  top: 50%;
  width: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- to see tooltip in the demo -->
<div style="height: 200px;"></div>
<input type="text" class="triggerTooltip" />
<div class="tooltip">
  <div>
    <a href='javascript:void(0);' class='tooltipClose'>X</a>
    <p class='tooltipHeader'>Tooltip header</p>
    <p class='tooltipBody'>Tooltip content</p>
  </div>
</div>

Check out the demonstration here:
Fiddle

Answer №1

You're almost finished with the task. Check out the updated version of your Fiddle

Here are some minor adjustments:

$('.triggerTooltip').on('click', function(){

    // Adjust the vertical position by setting it to half of the tooltip height
    $('div.tooltip').css('top', $('div.tooltip div').height()/2);

    // For the horizontal position, subtract the width of the field
    $('div.tooltip').css('left', $(this).offset().left-$(this).width());


    $('div.tooltip').addClass('tooltipShow');

});

I hope this helps you out

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

Bring to the front the div altered by a CSS3 animation

Recently, I encountered an issue with a div card that triggers an animation when clicked. The animation involves the card scaling up larger, but the problem arises as its edges get hidden under other cards. To visualize this, take a look at the screenshot ...

Having trouble getting the CSS Hover state to function properly?

I am currently working with a code snippet that looks like this: <div style="position: absolute; margin-left: -22px; margin-top: -22px; left: 502px; top: 379px; z-index: 380; display: block;" class="maptimize_marker_0 f st">1<span class="pinlabel ...

Fixing content at the bottom inside a container with Bootstrap 4

My app is contained within a <div class="container">. Inside this container, I have an editor and some buttons that I always want to be displayed at the bottom of the screen. Here's how it looks: <div class="container> // content here.. ...

Is there a way to display icons alongside each option in the dropdown menu?

I have a dropdown icon (pumpkin 1) div that is causing spacing issues with the other icons on the page. The problem is that it has a dropdown functionality which needs to overlay the text at the bottom. I want to display all icons next to each other (1,2, ...

Bootstrap3 and jQuery are attempting to achieve vertical alignment

I am trying to vertically align Bootstrap col-md* columns. I have two blocks, one with text and one with an image, and I want them to be equal in height with the text block vertically centered. My current solution is not working correctly. Can someone plea ...

Trouble encountered with card flip style login form in Vue.js, as the card is not maintaining its position properly during transition animations

Need help styling a login/signup component in Vue.js where flipping between the two cards isn't working smoothly. The transition is causing one card to move down and right while the other comes in from the bottom right. It's hard to explain the i ...

Is there a way to arrange an HTML list in this specific manner using CSS or JavaScript?

I need to arrange a list of items in columns with 5 rows each, as shown in the attached image. This list is generated dynamically using an SQL query with a loop on the li tag. I am looking for a solution to order the list in this way using javascript or ...

Issues with Jquery datatable causing column header to overlap the table header

I am facing an issue with my datatable as the table is not displaying correctly. Could someone advise me on how to troubleshoot and fix this problem? Below is the code snippet in question: $(tbName).dataTable({ "sScrollX": "100%", "sScro ...

I have written code in JavaScript, but now I am interested in converting it to jQuery

What is the best way to convert this to jQuery? showDish("balkandish1") function showDish(dishName) { var i; $(".city").css('display', 'none'); $("#" + dishName).css('display', 'block'); } ...

I have noticed that there are 3 images following the same logical sequence, however only the first 2 images seem to be functioning correctly. Can you help

Update: I have found a solution that works. You can check it out here: https://codepen.io/kristianBan/pen/RwNdRMO I have a scenario with 3 images where clicking on one should give it a red outline while removing any outline from the other two. The first t ...

What's causing these divs to be misaligned in various directions?

I am currently working with a front-end framework based on flexbox called Material-UI, and I have noticed that the layout of certain components, particularly the quantity control elements, appears to be different even though they all share the same styling ...

What is the method for using jQuery to retrieve hidden fields with the visible attribute set to "false"?

How can I access a control with "visible = false" attribute using jQuery? Some code examples would be appreciated. <tr>   <td class="TDCaption" style="text-align: left">     <asp:Label ID="lblMsg" runat="server" EnableViewState="False" F ...

Conceal the <div> element if the <span>

Is there a way to hide the content within this div if the prop-value is empty? I specifically want to hide the text "First class" when Prop-value does not have any value. Any solutions? <div> <span class='prop-name'>F ...

What is the best approach to animating a specified quantity of divs with CSS and javascript?

How neat is this code snippet: <div class="container"> <div class="box fade-in one"> look at me fade in </div> <div class="box fade-in two"> Oh hi! i can fade too! </div> <div class="box fade-in three"& ...

Ways to enlarge the font size of a hyperlink

I am just starting out with coding and I need to figure out how to increase the font size of my link. <!DOCTYPE html> <html> <head> <style> /* unvisited link */ a:link { color: #FFFFFF; } /* visited link */ a:visited { color: #FF ...

What is the method for adjusting the text color in an HTML document?

Is there a way to modify the text color of this statement: You Have Already Earned Credit For This Link? <div id=\"myform\" style=\"visibility: hidden;\"> <font size=2><b>You Have Already Earned ...

When utilizing jQuery version 1.10.2, the HTML markup shows up prior to the page completing its loading process

I am trying to import one HTML page into another, but when I load or refresh the page, the HTML markup appears before the page fully loads. How can I fix this issue? <head> <script type="text/javascript" src="js/jquery-1.10.2.js" ></scr ...

The method I used to position a triangle at the bottom border of a parent element with relative

https://i.stack.imgur.com/RZjJ9.png I am trying to achieve a centered triangle within a border, but I am facing a challenge due to the parent component being relative. When I remove the relative positioning, I struggle to position the triangle in the cent ...

What methods can I use to prevent floated child divs from wrapping?

I'm currently working on creating a carousel-like feature where users can select radio buttons within various div elements. The concept involves having approximately 20 divs, each 150px wide, containing radio buttons. I want to prevent these 20 divs f ...

Height adjustment for flexbox children

Can someone assist me with marking up a todo list? I am attempting to set the height of div.main-tasks equal to div.tasks so that the former fills the entire latter. Unfortunately, I am unsure how to achieve this. You can refer to the following image for c ...