What is the best way to responsively center an after pseudo-element above its parent?

Looking to create a dynamic tooltip without any fixed widths or constraints on the parent element.

The process seems simple enough, but I'm facing an issue with centering the after element due to an existing transform attribute of transform: translateY(10px).

Unable to use the standard

left:50%; top:50%; transform: translate(-50%, -50%)'
method found online because of the conflicting transform attribute present.

Is this the root of the problem? Or should I investigate elsewhere?

If you have any suggestions or advice, feel free to share!

Also, just so you know, it's in vue; everything should be defined in the style section anyway.

Cheers!

CodeSandbox Example

Tooltip.vue

<script lang="ts">
import { defineComponent, computed } from 'vue'

export default defineComponent({
  props: {
    text: { type: String, default: 'Pop!' },
    backgroundColor: { type: String, default: '#000' },
    textColor: { type: String, default: '#fff' },
  },
  setup(props) {
    const text = computed(() => props.text)
    return { text }
  },
})
</script>
<template>
  <span :data-tooltip="text">
    <slot />
  </span>
</template>

<style lang="sass">
[data-tooltip]
  position: relative
  cursor: default
  &:after
    position: absolute
    width: max-content
    // left: calc(50% )
    bottom: 125%
    text-align: center
    box-sizing: border-box
    display: flex
    justify-content: center

    content: attr(data-tooltip)
    color: v-bind(textColor)
    background: v-bind(backgroundColor)
    padding: 8px
    border-radius: 1em
    font-weight: bold
    white-space: nowrap

    visibility: hidden
    opacity: 0
    transform: translateY(10px)
    transition: all 400ms
    // transition: opacity 500ms, transform 500ms
  &:hover::after
    opacity: 1
    visibility: visible
    transform: translateY(0)
</style>

Answer №1

The :after pseudo element is designed to align itself with its parent element, but if its width is greater than the parent <span>, it may appear off-center.

To resolve this issue, you can ensure that both the :after pseudo element and the <span> have the same width for perfect alignment:

<style lang="sass">
[data-tooltip]
  &:hover::after
    width: 100%
</style>

Check out the demo here

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

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

"Trouble arises with the match() function in relation to email regex validation

After retrieving the HTML content from a website with my function, I am using String.prototype.match along with a regex rule to extract email addresses from that page. However, the issue is that I am receiving a line that matches the regex but does not con ...

Mastering CSS Sprites: A Guide to Aligning Sprite Buttons at the Bottom

In my web app, I have a bottom navigation bar with 9 buttons, each represented by a Sprite Image having 3 different states. The challenge I'm facing is aligning all the images at the bottom of the nav bar or div. The icons vary slightly in size, and ...

What steps should I take in modifying my existing code to use jQuery to set my div to a minimum height rather than a fixed height?

Could someone assist me in adjusting my div to have a min-height instead of a regular height? Whenever I click on the "Learn more" button, it extends past my div because the function is designed to set a specific height rather than an equal height. $.fn.e ...

Tips for extracting designated link by employing a Selector CSS Query

Please note: While a similar question has been asked on JSoup:How to Parse a Specific Link, I have a more specific variation of this inquiry. Kindly read on for further details. In order to extract data from a particular site link, I am looking to utili ...

Having trouble installing the martinlindhe/laravel-vue-i18n-generator package with Laravel 8

I am attempting to install the martinlindhe/laravel-vue-i18n-generator package using Laravel 8, but I encountered an error message: ...

The process of organizing and arranging the content that appears on a webpage is in

Seeking a solution to achieve a similar effect like this example. Wanting the sections to transition nicely when clicked on. Should I use a Jquery plugin or implement it with CSS? ...

Can CSS stylesheets be set up to automatically load based on the browser being used to access the website?

I am inquiring because I recently completed a beautiful homepage design and, after viewing the website on Chrome and Safari on OSX, I noticed that certain elements were broken when I opened it in Firefox. In addition, an issue I was experiencing with the ...

The text alongside the radio button is not aligned vertically

Hello, I am attempting to vertically align text next to my radio button but encountering some issues. .radio { cursor: pointer; display: inline-flex; } .cui-a-radio-button__input { display: none; } .cui-a-radio-button__input:disabled + .cui-a-rad ...

Guide on creating a single build in GitLab for a Vue application using multiple .env files

I currently have a .gitlab-ci.yml file set up to build my Vue application. The process involves building once and then deploying the dist folder to different environments: stages: - build - deploy_dev - deploy_stg - deploy_prd build: image: no ...

Utilizing a combination of CSS and JavaScript, the traffic light can be altered to change based on

**I stumbled upon this online code snippet where a timer is controlled in CSS. I'm trying to figure out how to control it with JavaScript, but all my attempts have failed so far. Is there anyone who can help me solve this issue? What I'm attempti ...

The spacing between images on mobile devices is excessive

My images on mobile have too much space between them: mobile version They appear fine in Dev Tools: dev tools version This is my HTML: <div class="container-fluid"> <!-- <br> --> <div class="row"> ...

Only when the user has successfully completed the reCAPTCHA and checked the checkbox, Vue will be able to

For my new project developed with VueJs, I am implementing a Single Page Application (SPA). The objective is to incorporate a simple if statement functionality. When the user checks a checkbox and successfully solves the reCaptcha, Vue should send an email ...

The presence of a display block property within a div contained inside an li element results in extra whitespace

I am presenting the code below: <div class="settingsMenu" style="top: 135px; left: 149px; display: block;"> <ul> <li class="download" onclick="downTemplate('template1')">Download</li> <l ...

Css animations not functioning properly in Chrome

I've been working on this animation project, here is the link: http://codepen.io/Zeaklous/pen/dIomg It seems to work perfectly fine on Firefox but not on Chrome. Here is the code snippet: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/ ...

Is it possible to customize text or images using the window.location method?

Imagine I have a scenario with 3 unique servers as follows: https://server-1.com https://server-2.com https://server-3.com In my Vue application, I want to dynamically change an image based on the server being used. Can I achieve this by utilizing someth ...

What is the best way to apply CSS styles to different states in React?

I am endeavoring to adjust the size of an element by utilizing this code snippet. const [size, setSize] = useState({}); The initial size I wish to set is: transform: scale(1, 1) translate3d(0, 0, 0); Upon clicking on the element, my aim is to enlarge it ...

Here's a way to programmatically append the same icon to multiple li elements generated using document.createElement:

I am new to creating a to-do app and need some guidance. When a user enters a task, I successfully create a list item for that task. However, I am unsure how to add a delete icon next to the newly created li element. Can someone please help me out? Below ...

What causes the navbar-brand to be hidden on Chrome mobile browsers?

I recently completed building my website at emdashr.com and have been adjusting the mobile views. However, I'm facing an issue where the navbar-brand disappears when viewing the site on mobile Chrome or desktop Chrome with a small viewport. Interestin ...

What is the method for changing the color of a specific section of a header?

My header design consists of two lists of buttons, one on the left and one on the right. I am looking to customize the background color of the "Sign Up" button to make it stand out. Below is an example of what I want and the current design of my header alo ...

The CSS styles for a React component in Rollup are not being incorporated into the server-side rendering document in NextJS

I recently created a small npm package that exports a single component. The component is styled using SCSS modules and bundled with Rollup. Everything appeared to be working well until I imported it into a NextJS project, where I noticed that the styles ar ...