When adjusting the zoom level, a fixed position element seems to shift up and down in relation to another element. Is there a way to stop this from happening?

I'm attempting to vertically center some text within a speech bubble, and I've achieved this using the position: absolute method with a specified top value on the text wrapper element. However, upon zooming in and out in the browser window, the text seems to shift slightly up and down in relation to the bubble. Regardless of the top value I use, the text doesn't appear centered vertically at all zoom levels.

For example, here is the appearance at 75% zoom in Chrome: 75-percent-zoom

And here it is at 80% zoom in Chrome: 80-percent-zoom

You'll notice that in the 75% zoom screenshot, the text looks too high compared to the bubble.

Previously, I tried using px values for sizes and position, but encountered the same issue. Hence, I switched to using em, which hasn't seemed to solve the problem either.

<body>
    <div id="container">
        <img id="bubble" src="img/bubble.svg">
        <span id="text-wrapper">
            <p>888</p>
        </span>
    </div>
</body>
#container {
    position: relative;
    font-size: 100px;
    width: 4em;
    height: 3em;
}

#text-wrapper {
    position: absolute;
    top: 0.332em;
    left: 0.85em;
    height: 1em;
    font-size: 0.5em;
    line-height: 100%;
    z-index: 1;
    color: rgb(0, 0, 0);
}

#text-wrapper p {
    margin: 0;
}

Edit on CodeSandbox

Answer №1

  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

apply this style to absolutely positioned elements

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

Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the ...

Reflection mask feature in Chrome for Android that uses Webkit technology

Chrome Reflection on Desktop Computers By using Chrome on a desktop computer, I am able to create an image reflection with the following CSS code: img { -webkit-box-reflect: below 0 -webkit-gradient( linear, left top, left bottom, from(tran ...

The div swiftly clicks and moves beyond the screen with animated motion

Here is a code snippet that I am working with: $(document).ready(function(){ $(".subscriptions").click(function (){ if($(".pollSlider").css("margin-right") == "-200px"){ $('.pollSlider').animate({"margin-right": '+ ...

The syntax error in the Svelte conditional element class is causing issues

Trying to create an if block following the Svelte Guide for if blocks. The process appears straightforward, yet Svelte is flagging it as a syntax error: [!] (svelte plugin) ParseError: Unexpected character '#' public\js\templates\ ...

Employing an odd/even toggle for assigning class names

I need each this.state.title to be aligned based on a different classname. I attempted using css flex boxes/nth-of-type/nth-child, but it didn't work well with React. I'm utilizing this.state to access my objects. My failed strategy render: f ...

Surprise scrolling on a mobile device's screen

I am trying to figure out why there is a horizontal scroll bar on the page. I would like the content to fill the entire screen. https://i.sstatic.net/Y0ToM.png <section class="who" data-aos="fade-in"> <img class=" ...

Is there a way to eliminate the space between the gray background and the right edge of the browser window?

body{ font-family:'Montserrat'; } .content{ background-color:#d9d9d9; } p.dates{ display:inline-block; font-weight:800; margin-right:20px; width:120px; text-align:center; margin-top:0; margin-bott ...

Guide to making a vertical clickable separator/line using bootstrap

Seeking advice on creating a layout where the left side consists of a sidebar menu occupying 24% and the right side contains main content taking up 75%, with a clickable vertical divider at 1% between them. When this line is clicked, the left part will hid ...

Excessive vertical space is utilized by the vertical images in the CSS grid image gallery

My responsive CSS grid gallery is experiencing issues with vertical images disrupting the layout, as they appear at full size instead of remaining square like the others. How can I fix this problem? Thank you! Below is the code snippet: <div id=" ...

Tips for implementing custom styles on React components that have been imported locally

I'm currently struggling with applying CSS successfully to components imported from an npm library. My main challenge seems to be my use of css-loader in the react starter kit (https://github.com/kriasoft/react-starter-kit) without a full understandin ...

CSS documents are being displayed as type text/plain

My goal is to host my static blog (built with Jekyll) on my Ubuntu server, but I'm encountering a problem where the CSS isn't being applied and I keep seeing this error: "Resource interpreted as Stylesheet but transferred with MIME type text/pla ...

What could be causing my mobile page to require two scrolls?

I've encountered a peculiar issue where users need to scroll twice or the page gets stuck on mobile devices that are less than 600px wide. I've attempted the following solutions with no success. I am a complete novice, so please excuse my lack of ...

Strategies for managing grid overflow situations

Check out my codepen showcasing the current issue I'm dealing with: https://codepen.io/marcdaframe/pen/qeBWNd <div> 123 abc <div class="container"> <div class="wrapper"> <div>One</div> <div>Two</div> ...

Internet Explorer does not recognize the specific CSS code

My goal is to create a website that behaves differently on Internet Explorer browser. I am attempting to achieve this by using a separate CSS for IE and another CSS for different browsers. However, the IE-specific CSS does not seem to be working. <h ...

Allowing breaks with the :nth-child() selector

There must be a straightforward explanation for this perplexing issue. However, in my view, it ought to function as intended. We are currently utilizing the Bootstrap 3 CSS framework. The following code is present on a specific page: <div class="promo ...

Include an Open OnClick event in the React/JSX script

We have a dynamic menu created using JavaScript (React/JSX) that opens on hover due to styling. My inquiries are: Instead of relying on CSS for the hover effect, where in the code can I implement an OnClick event to trigger the menu opening using Java ...

Alignment in Bootstrap 4: How to Align Elements Both Vertically and Hor

I am having trouble aligning the content both horizontally and vertically. Does anyone have any suggestions for the best approach to achieve this? <div class="container"> <div class="row"> <div class="col-md-4"> <im ...

Populate a table cell with a div element in HTML

How can I make a div inside a rowspanned table cell 100% high of the cell? Check out this example for reference: https://jsfiddle.net/gborgonovo/zqohw286/2/ In the provided example, I want the red div to vertically fill the yellow cell. Any suggestions on ...

The issue with Bootstrap 4 Card Flex Width not functioning properly seems to be unique to Firefox

I'm currently facing an issue while creating a webpage using Bootstrap 4 and flex. I have Bootstrap cards with specific widths, and it seems to work perfectly fine in Google Chrome and Opera but not in Firefox. Here's the code snippet: <d ...

In the world of MUI, how should one interpret the symbols '&', '>', and '*' when used together?

Currently, I am in the process of building a website using React. To help with this project, I purchased a Material-UI React template. As I was going through the code, I came across the line 4 where it mentions '& > *', and I'm not qu ...