Automatically adjust the tooltip's position if it extends beyond the width of the browser

I am looking for a way to ensure that when the tooltip width exceeds the browser's viewable area, it will automatically reposition itself so that the content can be fully viewed. It is important that the tooltip does not overlap on the referenced div and that its width or height does not resize when it repositions. Any assistance would be greatly appreciated.

You can view my sample code here

Answer №2

Creating my own plugin for automatic object positioning:

(function($){
    $.fn.autoposition = function(ref, opt){     
            var _this = $(this);
            var position = function(_tip, _ref, opt){
            var o = $.extend({
                "topallowance":0,
                "leftallowance":0
            }, o || opt);

            var _window = $(window);
            var _widthOfWindow = _window.width();
            var _widthDifferenceFromXaxisOfRef = _widthOfWindow - _ref.offset().left;
            var _xAdjustmentOftipWhenExceedAtLeftSide = ((_tip.outerWidth() > _widthDifferenceFromXaxisOfRef) ? (_tip.outerWidth() - _widthDifferenceFromXaxisOfRef) + o.leftallowance : 0);
            var _leftOfTheTip = _ref.offset().left - _xAdjustmentOftipWhenExceedAtLeftSide;

            var _heightOfWindow = _window.outerHeight();
            var _heightDifferenceFromYaxisOfRef = _heightOfWindow - (_ref.offset().top + _ref.outerHeight());
            var _topOfTheTip = (_tip.outerHeight() > _heightDifferenceFromYaxisOfRef) ? (_ref.offset().top - _tip.outerHeight()) - o.topallowance : (_heightOfWindow - _heightDifferenceFromYaxisOfRef) + o.topallowance;



            _tip
                .css('position', 'absolute')
                .css('top', _topOfTheTip)
                .css('left', _leftOfTheTip)
            ;
        };

        position(_this, ref, opt);
        $(window).bind('resize', function(){
            position(_this, ref, opt);
        });
    };
})(jQuery);

To implement it:

$(".TooltipSample").autoposition($(".theObjectWhereTheTooltipWillShow"));  

Hopefully this will be useful to others.

Answer №3

I came across a solution and implemented it on codebins in the following manner:

1) First, include jquery-1.2.2.pack.js and htmltooltip.js JavaScript files in the header.

2) Then, write the HTML:

<div id="container">
  <div rel="htmltooltip">
    A
  </div>
  <div rel="htmltooltip">
    B
  </div>
  ...
</div>
<div class="htmltooltip">
  Showing Tooltip for Div A
...
</div>

Next, add the CSS:

#container div{
  display:inline-block;
  margin-left:5px;
  ...
}
div.htmltooltip{
  position: absolute;
  z-index: 1000;
  ...
}

In the HTML above, the "htmltooltip" attribute is set on each tag with a related tooltip assigned to the corresponding "htmltooltip" class. This setup ensures that the relevant tooltip appears when hovering over each tag in order.

The tooltip also automatically adjusts its position within the browser window if it would otherwise extend beyond the width of the browser.

Feel free to check out the demo here.

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

Can a never-ending CSS grid be used to design a unique gallery layout?

Hello fellow developers, I am looking to build a portfolio website with a gallery layout similar to Instagram, featuring 3 pictures in a row. I attempted to use CSS grid for this purpose. Here is how the grid currently looks: (Note that the 225px column/r ...

Characteristics of a high-end text editing tool

When working with the following RTE, one challenge I face is reducing the width. However, doing so may result in removal of some features such as bold and underline options. Is there a way to decrease the width without losing these buttons? In other word ...

Ways to activate a function upon validation of an email input type

Within my HTML form, there is an input type="email" field that requires a valid email pattern for acceptance. I am attempting to send an AJAX request to the server to confirm whether the entered email already exists in the database after it has been verif ...

In JavaScript, you can update the class named "active" to become the active attribute for a link

My current menu uses superfish, but it lacks an active class to highlight the current page tab. To rectify this, I implemented the following JavaScript code. <script type="text/javascript"> var path = window.location.pathname.split('/'); p ...

Navigating Data Retrieved from a Web Service in JSON Format Using jQuery

Working with JSON has presented some challenges for me! I recently followed the guidance provided in this article By leveraging Server Side Web Methods, I have successfully implemented AJAX actions. While most of my tasks involve simple operations like ...

Creating the document.ready function in Angular2 with JQuery

I am seeking assistance to modify the JQuery function so that it can run without the requirement of a button click. Currently, the function only executes when a button is clicked. Code declare var jQuery: any; @Component({ selector: 'home-component ...

Determining the height of a Bootstrap column in relation to another element

Utilizing the grid layout from bootstrap, I have the following structure: <div id="prof_cont_enclose"> <div class="row"> <div class="prof_cont_row"> <div class="col-xs-12 col-sm-4 col-md-2 col-lg-2 prof_elem"&g ...

Retrieve data from a MySQL table based on a specific condition in PHP

Can someone assist me with fetching data from a table where the valid_from date is less than a specified date (excluding the current date)? I have a table that looks like this: view my table here For instance, if my date is 02-04-2015, I would want to re ...

Contenteditable feature dynamically styling text upon input

Is it possible to prevent CSS properties like text-transform from affecting text entered via contenteditable? On my webpage, "CERTIFICATE PROGRAM" is shown in uppercase due to text-transform settings. However, the original input was: Certificate Program. ...

AngularJS drag-and-drop functionality for creating and rearranging lists

I am in need of a drag and drop list feature using angular, but the $scope.list must also be updated as I have to save the new order in my database. I came across this helpful answer and utilized it to create this http://jsfiddle.net/aras7/9sueU/1/ var m ...

Tips for increasing a variable by one with each button click?

I have a simple JavaScript function called plusOne() that is designed to increment a variable by 1 each time a button is clicked, and then display the updated value on a webpage. However, I'm encountering an issue where the addition only occurs once. ...

Center or left-align the divs

Can anyone offer assistance with this section of code? HTML: <div id="holder"> <div class="box"> </div> <div class="box"> </div> <div class="box"> </div> <div class="box"> </div> </div> ...

To properly document the results, I must utilize a button to record the identification of a specific color

I'm working on a code that creates a clickable box which changes colors from black to green to white by going through different shades of green whenever the mouse is clicked. What I now need to do is implement a feature that displays the current shade ...

avoid triggering the on hover state

When working with jQuery, I have a CSS style targeting a specific div: div.right-sm:hover{background-color: blue} Now, I am trying to prevent the hover effect using jQuery: $(this).parent('div').removeClass('right-sm:hover'); Howev ...

Achieving full opacity text within a semi-transparent div: a simple guide

Within my div, I have an a tag. I applied opacity:0.5 to the div, causing the text inside to also appear at 0.5 opacity. I am looking for a way to have the text display with opacity:1 inside my div that has an overall opacity of 0.5, without resorting to ...

Custom Tooltips arrow is not receiving the CSS styling

I have implemented ReactTooltip from the ReactTooltip library You can view an example here Component Setup <ReactTooltip className={styles.customTheme} id={id} place={placement} effect="solid"> {children} </ReactTooltip> Stylin ...

"Implement a CSS hover effect that targets the child elements within a div, rather than

Looking to experiment with a new product card layout and encountering an issue with the box shadow effect. When hovering over the product card, the hover function only affects the child elements of the div Product-Card hov instead of the actual div itself. ...

Setting the height of columns in a Bootstrap panel to 100%

Is there a way to achieve 100% height for all three columns even without content? Check out this JSFiddle: <div class="row"> <div class="col-md-12"> <div class="shadow panel panel-default"> <div class="blue white-bord ...

CSS transformation: skewing without any blurring effects

Is there a way to create tilted drivers with borders and text without sacrificing the sharpness of the font and border? I have used the transform: skew(); CSS rule for tilting, but it has caused blurriness. How can I add font smoothing and maintain sharp b ...

Adjusting the height of a div according to the changing heights of other divs

The sidebar contains a search field, two lists, and a text div. The height of the search field and text div always remains constant, while the heights of the two lists vary dynamically. I am exploring the possibility of using only CSS to make the height o ...