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

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

Tips on utilizing a function within a v-if statement

I am attempting to showcase countries from a database where the continentId for each country matches the scope of the logged-in user. The user's scope ranges between 1 and 5. Here is the Vue template I am using: <div class="container w-75&quo ...

What could be causing the dropdown menu to not display below its container when the overflow:hidden CSS property is applied to the container element

One issue I'm facing is that the dropdown submenu of my navigation menu does not extend beyond its container element when displayed. My code includes main menu items (HOME, ABOUT, SERVICES, PRODUCTS, and CONTACT) along with submenu items (A, B, C, D, ...

Images are not appearing on Github pages

My website on GitHub Pages is not displaying images properly. Here is the link to my site: However, when I run it locally everything works fine. You can find the repository here: https://github.com/rsgrw23/rate-your-beer Can anyone pinpoint what might be ...

Create a stylish design with Bootstrap 3.0 featuring a full-width color background and compact columns perfectly centered on the

As I ventured into creating a business theme with stripes, akin to the one developed by W3Schools and accessible here, I encountered a particular challenge. The layout consisted of horizontal sections with varying background colors. My main concern was th ...

How to create centered striped backgrounds in CSS

Attempting to achieve a background similar to the one shown Can this be accomplished using CSS? ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

Encountering difficulties in installing vue-property-decorator or vue-class-component after initializing project with vite

After setting up a Vue3 project with Vite, I encountered an issue when trying to install vue-property-decorator or vue-class-component. The error message indicated that it was unable to resolve the dependency tree, causing the following errors: npm ERR! ...

CSS rules for organizing the stacking order of elements in the SuperFish Menu with

I've been struggling with a z-index issue on a website I'm currently managing. It seems to stem from the z-index values in the SuperFish Menu and a specific div element. Despite my attempts to apply position:relative/absolute & z-index: 99999 dec ...

The header and footer elements must be included along with a main div to house the content on the webpage

I am currently working on building a website using HTML and CSS. I would like to create an index.php file that calls all the different parts of the page. For example, I want to include the header and footer, and also have the ability to replace content wit ...

Leveraging the power of Vue.js in Laravel to extract dynamic field data

While working on my Laravel project, I encountered an issue with retrieving dynamically generated field values in the controller method. Although I can successfully access data from simple fields that are not dynamically generated, the values from array fi ...

I am facing an issue where my Vue root file/component is not appearing in my Laravel project

I have been working through a tutorial that covers creating a Laravel + Vue CRUD application and I have encountered an issue with getting Vue to show up in the Laravel welcome blade. Here is a snippet of my code: app.js file require('./bootstrap&apo ...

Eliminating the impact of a CSS selector

I have a complex mark-up structure with multiple CSS classes associated: classA, classB, classC, classD. These classes are used for both styling via CSS and event binding using jQuery selectors. The Challenge: I need the jQuery events to be functional whi ...

What is the best way to adjust the volume vertically?

I successfully transformed my volume slider from horizontal to vertical using CSS. However, the functionality of the volume slider still behaves as if it were horizontal. In other words, I have to click horizontally to adjust the volume instead of vertical ...

Navigating back with the back button in Vue js without refreshing the prior page

I recently developed a website with 3 pages, where the first page links to the second and the second links to the third. However, I've encountered an issue when users click the back button. Instead of navigating to the previous page with the full brow ...

Difficulties encountered when adjusting the size or reducing the images in a Cycle2 slideshow

Greetings to all fellow coders! Thank you for taking the time to read this post. I am currently facing a challenge in my web development project where I am trying to make my Cycle 2 slideshow images resize and reposition alongside other divs when the wind ...

Tips on how to ensure the navigation menu is the same size as its child elements in the navigation menu

I'm currently designing a navigation menu that includes a child navigation menu. My goal is to have the size of the main navigation menu match that of the child navigation menu. To achieve this, I've created three hyperlink tags followed by a di ...

Using static methods within a static class to achieve method overloading in Typescript

I have a TypeScript static class that converts key-value pairs to strings. The values can be boolean, number, or string, but I want them all to end up as strings with specific implementations. [{ key: "key1", value: false }, { key: "key2&qu ...

Tips for Placing a Div in a Precise Location

I'm currently working with Bootstrap and I'm attempting to replicate a layout similar to the one used by Twitter, as shown in the screenshot below. The specific part I'm struggling with is where the profile picture is located. I want to add ...

What is the best way to align li elements in a ul list at the

How can I center ul elements on a web page? I have checked similar questions on this site and tried using text-align in my CSS file to center the elements, but it didn't work as expected. I also attempted to use margins which did center the elements, ...

"Selecting elements with CSS using the 'element'

While exploring some websites for studying purposes, I came across this code snippet. .site-header .widget-area span.but As a beginner in CSS, I'm curious to know more about the span.but part. Is it similar to the :not pseudo selector? ...