CSS: Ensuring consistent spacing between a custom popover and its control, regardless of container size modifications

I'm experimenting with creating a dynamic popover that adjusts its position based on whether it's viewed on a mobile device or desktop.

Although I've made progress, the challenge lies in ensuring reusability. When resizing the container, the popover fails to display in the intended location.

Here is the HTML structure:

<div class="button-cont">
    <div class="input-group mb-3 button-container">
        <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
        <div class="input-group-append button-calendar" (click)="show()">
            <span class="input-group-text">.00</span>
        </div>
        <div class="custom-tooltip" #tooltip>
            Hello
        </div>
    </div>
</div>

CSS styling:

.button-cont {
  height: 100%;
  width: 100%;
  display:flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}



.button-container {
  position: relative;
  padding: 1rem 1rem;
  height: 3rem;
  width: 30rem;
}

.button-calendar {
  cursor: pointer;
}

.custom-tooltip {
  height: 5rem;
  background-color: white;
  width: 5rem;
  position: absolute;
  box-shadow: 0px 0px 16px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  left: 26rem;
  bottom: -93px;
  opacity: 0;

  @media(max-width: 600px) { 
     margin-left: auto; 
     margin-right: auto; 
     bottom: -86px;
     left: 50%;
     transform: translateX(-50%)
  } 

}


.show-tooltip {
  opacity: 1
}

Additionally, here's the Angular code snippet used to trigger the popover:

import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";

@Component({
  selector: "app-ngx-bootstrap",
  templateUrl: "./ngx-bootstrap.component.html",
  styleUrls: ["./ngx-bootstrap.component.scss"]
})
export class NgxBootstrapComponent implements OnInit {
  @ViewChild("tooltip") tooltip: ElementRef;
  isOpen = false;
  constructor() {}

  ngOnInit() {}

  show() {
    if (!this.isOpen) {
      this.tooltip.nativeElement.classList.add("show-tooltip");
    } else {
      this.tooltip.nativeElement.classList.remove("show-tooltip");
    }
    this.isOpen = !this.isOpen;
  }
}

The focus currently is on achieving the correct positioning, rather than the visual appearance of the component. Here's the desired placement:

https://i.sstatic.net/fFrRl.png

Requirements:

1) On mobile devices, the popover should be centered horizontally below the input group.

2) For desktop views, the popover should appear below the button (calendar) and slightly to the right.

While there has been progress, adjustments are needed to maintain proper alignment when modifying the size of the input-group.

Check out the live demo on StackBlitz here

Answer №1

To reposition the tooltip, simply update the left CSS attribute to right. This adjustment will ensure that the tooltip remains within the boundaries of its input-group container:

.custom-tooltip {
  height: 5rem;
  background-color: white;
  width: 5rem;
  position: absolute;
  box-shadow: 0px 0px 16px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  right: -15px;
  bottom: -93px;
  opacity: 0;
  ...

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

Placing numbers at the top of the HTML table cell for better visibility

I am currently utilizing a two-column HTML table to showcase a list of rules. In one column, I have the rule's number, while in the other, I have the rule itself. However, I've noticed that the rule numbers are appearing near the bottom of their ...

What is the technique for creating a repeating image rather than stretching it?

Is there a way to repeat a background image to fit its content? I have a background image that needs to be repeated in this manner: https://i.sstatic.net/qVKkp.png However, when using the following code: .imgbord { background: url('https://i.imgur ...

What is the best way to automatically clear an input field after a specified delay?

On my landing page, I have a single input field where users can enter their email address. Upon successful entry... success: function(result){ console.log(result.status); if(result.status == true) { $('input').attr("style", "color:green" ...

Can a dropdown list be designed to appear as a box or div instead of the standard select option?

My idea is to use boxes to represent different options https://i.sstatic.net/rTmIj.jpg <label for="product">Choose:</label> <select name="product" id="product"> <option value="70A"> ...

Is there a way to integrate Bootstrap 4 into a Sapper application?

I recently developed a new application using Sapper and now I'm looking to integrate Twitter Bootstrap 4 into it. Can anyone advise on how to incorporate Bootstrap CSS and JS into my Sapper app? ...

changing text color on hover with Angular

Hello, I am new to the world of programming. I've been trying out this code and everything seems to be working fine except for the @hostlistener part. Just to provide some context, I am using Bootstrap version 4. The strange thing is that there are n ...

The Material UI - makeStyles function is having difficulties generating the intended styles

After implementing withStyles and makeStyles from material-UI, I encountered an issue where the CSS styling was not as expected, despite the correct class name being returned. For example, consider the following component: import React, { Component } fro ...

What is the best way to place division elements next to each other?

Is there a way to align the text "burger king" to the right of the logo on my webpage? I attempted using the bootstrap class pull-left, but it didn't produce any changes. This is how my page appears: body { font-size: 16px; background-color: # ...

Personalizing the share button by changing the icon font to an image

I've been working on incorporating this share button into my website.... Although it functions properly as is, I want to switch out the icon font for an image. I attempted to modify certain CSS properties, but encountered some issues. http://jsfidd ...

Issue with Bootstrap Carousel Indicators malfunctioning

I've been working on a website project for a client's portfolio, and I'm using Bootstrap Carousel in the index. The carousel showcases different projects with custom indicators made of text instead of the usual style. If you take a look at t ...

Struggling to get a sticky navigation bar to function properly on Chrome browser

My goal is to make the menu bar stick to the top of the viewport once the header has been scrolled past. To achieve this, I have used jQuery and implemented the following code to adjust the CSS when the menu bar reaches the top: jQuery(document).ready(fu ...

Can you explain the distinction between the fontawesome angular package, the standard npm package, and simple sass?

In my new Angular CLI project (version 8+) that is using Sass (SCSS), I have been exploring the Fontawesome documentation and discovered there are several methods for hosting Fontawesome icons. These include installing the npm package, integrating the angu ...

Troubleshooting problem with iPhone X responsiveness

Struggling with responsive issues on iPhone X. Issue is only appearing on actual device. Any tips for fixing this? I'm facing an issue where the website looks good and responsive on all devices in Chrome's responsive view. But when I access it th ...

What is the reason for passing the index instead of the FormControl object to the FormControlNameDirective?

Link to Angular FormsArrayName Directive: https://angular.io/docs/ts/latest/api/forms/index/FormArrayName-directive.html This is the HTML code snippet: <form [formGroup]="form" (ngSubmit)="onSubmit()"> <div formArrayName="cities"> ...

Display text dynamically on the canvas as soon as input is received

My goal is to allow the user to enter text outside of a canvas element and see it displayed on the canvas either in real-time or when a button is clicked. I have limited experience with canvas and JavaScript, so any guidance would be appreciated. Thanks ...

Shifting elements within a modal using AngularJS and CSS: Swapping one div out for another

I am currently developing an application and I am considering using AngularJs for this purpose: <div class="main"> <div class="one">Content 1 goes here ...</div> <div class="two">Content 2 goes here ...</div> </div> Wi ...

Guide to creating intricate animations using a CSS/JS user interface editor

Is there an easy way to create complex animations without blindly tweaking keyframes? I'm searching for a tool or editor that can generate the necessary animation code, similar to this example: https://codepen.io/kilianso/pen/NaLzVW /* STATE 2 */ .scr ...

Changing the order of Bootstrap columns

Below is the code snippet I am using in Bootstrap- .message-preview { background-color: green; height:100px; } .inbox-preview { background-color: blue; height:100px; } <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap. ...

Arrange three div elements to create a layout that is optimized for different screen sizes

Is there a way to align these 3 divs as follows: pickup on the left, phone in the middle, and delivery on the right? I've browsed through similar alignment issues on Stack Overflow, but none of the solutions seem to work for my specific code. It' ...

npm error: JSON input unexpectedly ended while parsing

Encountered an error when running ng new project-name in my Angular project. npm WARN deprecated [email protected]: CircularJSON is in maintenance only, flatted is its successor. npm ERR! Unexpected end of JSON input while parsing near '...: ...