Css for tooltips positioned to the left and right

I have recently developed a react component tooltip that is designed to appear based on the direction (top, bottom, left, right) specified in the component parameters. Interestingly, when top or bottom is selected, the tooltip functions perfectly. However, if left or right is chosen, the tooltip ends up appearing within the element it is meant for rather than beside it.

React Component

import React, { useState } from "react";
import styles from "./toolTip.module.css";

const ToolTip = ({
  delay,
  direction,
  content,
  children
}: {
  delay?: number;
  direction: "top" | "right" | "left" | "bottom";
  content: any;
  children: any;
}) => {
  let timeout: any;
  const [directionClass] = useState(styles[direction]);
  const [active, setActive] = useState(false);

  const showTip = () => {
    timeout = setTimeout(() => {
      setActive(true);
    }, delay || 400);
  };

  const hideTip = () => {
    clearInterval(timeout);
    setActive(false);
  };

  return (
    <div
      className={styles.toolTipWrapper}
      onMouseEnter={showTip}
      onMouseLeave={hideTip}
    >
      {children}
      {active &&
        <div className={`${styles.toolTip} ${directionClass}`}>
          {content}
        </div>}
    </div>
  );
};

export default ToolTip;

CSS Styles

.toolTipWrapper {
  display: inline-block;
  position: relative;
}

.toolTip {
  position: absolute;
  border-radius: 4px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px;
  color: rgb(217, 212, 212);
  background: black;
  font-size: 14px;
  font-family: sans-serif;
  line-height: 1;
  z-index: 100;
  white-space: nowrap;
}

.toolTip::before {
  content: " ";
  left: 50%;
  border: solid transparent;
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}

.toolTip.top {
  bottom: 100%;
  margin-bottom: 2px;
}

.toolTip.bottom {
  margin-top: 2px;
}

.toolTip.left {
  right: 100%;
  margin-right: 2px;
}

.toolTip.right {
  left: 100%;
  margin-left: 2px;
}

Answer №1

Here is a solution that should address your issue.

.toolTip.left {
  top: 50%;
  transform: translateY(-50%);
  left: calc(-50% - 2px);
}

.toolTip.right {
  top: 50%;
  transform: translateY(-50%);
  left: calc(100% + 2px);
}

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

The alignment feature in the center is malfunctioning

I'm currently utilizing jQuery Mobile and have the following HTML along with CSS: .ui-grid-a { padding: 0; margin: 0; margin-top: 15px; height: 380px; } .ui-block-a, .ui-block-b { padding: 0; margin: 0; height: 33.3%; } .ui-block-a a, .ui-block ...

An effective way to transfer an Array from a Parent Component to a Child Component in React is by utilizing a Stateful Component

I'm currently developing a React project and facing the challenge of passing an array from a Parent component to a Child component using stateful components. Here is the code in App.js: import React, { Component } from 'react'; import &apo ...

Adjust the margin at the top and bottom of a printed HTML document

I am trying to achieve a 75px margin on the top and bottom of each printed page. So far, I have attempted adding the margin to both the body and a wrapper div, but it seems that these methods only apply the margin to the entire document. This results in a ...

Ways to adjust the dimensions of an SVG icon in a React application

Trying to figure out how to display an SVG icon and text on the same line in React and Next.js. After some research, I've managed to achieve it. However, I'm struggling to control the size of the SVG icon in relation to the text. Ideally, I want ...

Ways to have the "li" element displayed on a separate line

let selector = document.querySelector('.activate') selector.addEventListener('mouseover', function(){ document.querySelector('#hidden-li').classList.remove("hidden") }) selector.addEventListener('mouseleave', f ...

Arranging a content container beneath a div containing an image

On a webpage I am working on, there is a div that contains an animation with images that change using CSS. Below this animation is a wrapper div for the content structured like this: <div id="animation"> <img ...> ... <img .. ...

I am having trouble with Owl Carousel in my code. Can anyone help me figure out what is causing the issue

I'm having trouble getting my owl carousel to display when I try to run it. I've made sure to link the correct stylesheets and scripts, but nothing is showing up on the page. Even after trying to link the stylesheets from both a local source and ...

Configuring a background logo in WordPress using the Redux framework

I am attempting to integrate a "Logo upload" theme option using Redux framework, but I encountered an issue due to the logo being set up through a stylesheet in my HTML template. How can I configure this logo using an uploader? As a newcomer to WordPress a ...

Using Typescript to override an abstract method that has a void return type

abstract class Base{ abstract sayHello(): void; } class Child extends Base{ sayHello() { return 123; } } The Abstract method in this code snippet has a return type of void, but the implementation in the Child class returns a number. S ...

When a row is clicked, retrieve the data specific to that row

I have implemented a data-grid using react-table where I pass necessary props to a separate component for rendering the grid. My issue is that when I click on a particular row, I am unable to retrieve the information related to that row using getTrProps. ...

Encountering the error "Cannot access property 'stroke' of undefined" when utilizing constructors in React and p5

Hi there, I'm having trouble with my React code and could really use some assistance. Being new to React, I'm trying to create a collision system between multiple bubbles in an array, but I keep running into this undefined error: https://i.sstat ...

Place the bottom element of the top parent element in position

Creating a simple tooltip with bottom positioning at the top of the parent element involves setting a negative height for the tooltip element. However, when checking the height of the tooltip element upon hovering, it returns 0 according to console.log(). ...

Place a check mark in the corner of the button

I am looking to add a checkmark on my button in the right corner. I have set the input value and it displays a button with a check mark, but I want it to be positioned in the right corner. .buttoner{ background-color: #4CAF50; b ...

Is there a way to restrict the return type of a function property depending on the boolean value of another property?

I'm interested in creating a structure similar to IA<T> as shown below: interface IA<T> { f: () => T | number; x: boolean } However, I want f to return a number when x is true, and a T when x is false. Is this feasible? My attempt ...

Is it necessary for me to store every React component state property in the Redux store?

After reading numerous articles emphasizing the importance of using Redux and successfully creating two React+Redux applications, I am still left with a lingering question that even Quora couldn't provide a definitive answer to: Do I need to store e ...

Why is my PanResponder failing to detect changes in the updated state?

This is the code snippet I'm currently working on: const processPinch = (x1: number, y1: number, x2: number, y2: number) => { function calcDistance(x1: number, y1: number, x2: number, y2: number) { const dx = x1 - x2; const dy = y1 ...

Using Javascript to access the index of an element that has been checked and

I am working on a dynamic feature that involves a JavaScript variable called 'options'. This variable is designed to store and update a string with the .innerHTML content of every checkbox checked by the user. For instance, if the user checks Ins ...

Simple Jquery image rotator generating two galleries side by side

Currently, I am encountering an issue where I am unable to configure the Jquery rotator from Easy Jquery Auto Image Rotator and script source Google jquery.min.js I attempted to give the second gallery a different class and duplicated the js script. ...

Determine the data type of an object key based on the value of another key within the same

Consider having an Object Type named Options, which consists of two keys: strategy: a function that takes one parameter of an unknown type parameter: the same type as the first argument of strategy The desired functionality is as follows: type FooParam ...

Error Handling in Angular2 MVC 4 Project Route Issues

Here is the structure of my Mvc 4 Project with angular 2 implemented: Solution 'Angular2Starter' |-- angular2Starter | `-- Controllers | `-- HomeController.cs |-- src | |-- app | | |-- home | | | |-- home.component.ts | ...