Is there a way to bring my popup closer to my button?

Check out my jsfiddle example here: https://jsfiddle.net/annahisenberg/ft10ersb/34/

Currently, I have the following code:


<div
  id="more_options_popup"
  style={{
    left: this.ref.current.offsetLeft - 140 + "px",
    top: this.ref.current.offsetTop - 150 + "px"
  }}
>

However, I'm facing an issue in my actual project with this approach. Adjusting the offsetTop to -130 brings it too close to my button when there are more items in the popup. On the other hand, if there's only one <p> tag in the popup, it ends up being too far away from the button.

I'm unable to display this behavior properly on the jsFiddle.

React Code:


// React component goes here...

CSS:


#more_options_button {
  // CSS styles for more options button
}

#more_options_popup {
  // CSS styles for more options popup
}  

Answer №1

To achieve the desired effect, consider utilizing the right and bottom positioning properties instead of top and left. I have tested this method and it appears to be successful. For reference, you can view an example here: https://jsfiddle.net/0ub8jhxa/

UPDATE An alternative approach is to nest your popup within the a tag. By doing so, you can utilize relative positioning to align your popover in relation to the a tag. Here is an example:

a {
  position: relative; /* or absolute */
}
a > div.popover {
  position: absolute;
  top: calc(100% - 20px);
  left: calc(100% - 20px);
}

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

I am currently utilizing MUI-Data-Tables and am seeking to customize the header text color after selecting a sorting option for the column

I am trying to find a way to change the default black text color when sorting a column, but I haven't had much success so far. The only progress I've made is changing the arrow icon color using the following code snippet. MUIDataTableHeadCell: { ...

It's proving difficult for me to align my header and navbar on the same line

Recently, I've delved into the world of HTML/CSS and encountered a challenge. My goal is to align my header (containing an h1 tag) and navbar on the same line. Ideally, the header should float left with the navbar positioned closely to its left. Howev ...

Jquery allows for the toggling of multiple checkboxes

I have a group of check-boxes that I would like to toggle their content when checked. Currently, I am using jQuery to achieve this functionality, but I am searching for a way to optimize my code so that I do not need to write a separate function for each ...

Incorporating a background image into a Material UI theme with CSS styling

When customizing a theme in material UI, one can adjust or override it using the function createMuiTheme. Below is the complete file where this process is carried out: import { createMuiTheme } from '@material-ui/core/styles'; import purple from ...

Steps for activating the HTML select option menu with an external button click

I'm currently in the process of building a React website and I'm faced with the challenge of customizing select options to align with my design in Figma. Check out this image for reference Here's how I've set it up in my code: const C ...

Troubleshooting my CSS navigation bar - What am I missing?

I've been developing a navigation bar using a combination of HTML, CSS, and JavaScript. After writing the code and setting the display of the bar to fixed, I encountered an issue where the content of the page was overlapping the nav bar. Below you&ap ...

What is the best way to include a class in an element once the scroll surpasses a certain value?

Can anyone help me understand why my attempt to assign a class to an element based on scrolling behavior is failing? The code snippet in question is: className={`arrowup ${window.pageYOffset >= 2 ? "showscroll":null}`} ...

Disable the option to unsort a column in an antd table (enforce sorting)

To activate sorting on a column, simply use the sortDirections parameter with values ['ascend', 'descend']. With these settings, there are three sorting possibilities: 'ascend', 'descend', and 'unsorted'. ...

What's the best way to set up multiple NestJS providers using information from a JSON file?

Recently diving into NestJS, I am in the process of building an application following the MVC architecture with multiple modules including: Project | +-- App.Controller +-- App.Service +-- App.Module | +-- SubModule1 | | | +-- SubModule1.C ...

The Express GET route does not support parameters or additional paths

I am facing an issue with making a fetch request when trying to add additional path or parameters... Here is what I want to achieve: const fetchOwnerCardList = () => { fetch("http://localhost:5000/api/card/ownerCards", { method: "GET", header ...

Sending information through a form via a POST request after clicking on a hyperlink

I am attempting to submit a form using POST by clicking on a link and passing some hidden values. This is the code I'm currently using: $( document ).ready(function() { $('a#campoA').click(function() { $.post('testForm2.php', { ...

Using framer-motion with Next.JS ensures that content remains consistent during navigation

I added a Link on my homepage that connects to the About Us page: <Link href="/about"><a>About us</a></Link> In my _app.js file, there is an AnimatePresence wrapper: <AnimatePresence exitBeforeEnter> <Component {...p ...

Error with Firebase authentication on a Next.js project using TypeScript

I recently started a personal project using Next.js, Typescript, and Firebase for authentication and database management. While working on a sign-in page with Google integration, I encountered an error labeled as auth/argument-error. According to the Fireb ...

Navigate to a new tab using this.router.navigate

Is there a way to redirect the user to a specific page with ${id} opening in a new tab, after clicking a button in an angular material dialog box? I want to leave the dialog box open while querying the new page. Currently, the redirect happens but not in a ...

What is the most effective way to eliminate just the last underscore in a string by employing the

I am currently facing an issue with table cell editing. I have the following code that removes all underscores (_) from a string if it contains "test_1_". However, my requirement is to only remove the last underscore and have the string appear as "test_1". ...

Exporting statically generated HTML using data fetched in Next.js

I am developing a React application that will display product ratings retrieved from the back-end. The rating of each product can be found at /product/{id}. Upon loading the page, the product's rating data will be fetched from the back-end and display ...

Tips for customizing the selection tip of a custom cursor using CSS

I have integrated a color picker jquery plugin on my website to allow users to pick colors from an image. I made a modification in the CSS to change the cursor to an eyedropper icon. However, currently when clicking on the image, the color pointed by the u ...

What is the resolution process for importing @angular/core/testing in TypeScript and what is the packaging structure of the Angular core framework?

When using import {Injectable} from '@angular/core';, does the module attribute in package.json point to a file that exports injectable? Also, for the format @angular/core/testing, is there a testing folder within @angular/core that contains anot ...

Encountering SUID Sandbox Helper Issue When Running "npm start" on WSL with Electron and Typescript

Can anyone help me with this issue? I have Node v8.10.0 and I'm attempting to follow a beginner tutorial on Electron + Typescript which can be found at the following link: https://github.com/electron/electron-quick-start-typescript Here is the full e ...

In JavaScript, the "this" keyword points to a different object

Here is a script that needs attention: Bla = function() { this.prop = 123; } Bla.prototype.yay = function() { console.log(this,this.prop); } X = new Bla(); $(document).ready(X.yay); // output: #document undefined --> why? $(document).ready(functio ...