Error found when employing absolute positioning and percentage values to vertically center a child div

There's a div that functions as my "popup," and I'm animating it to appear in the exact center of its parent div. To achieve this, I'm utilizing absolute positioning, negative margins, and adjusting the left and top properties. This allows me to easily animate it using

-webkit-transform: translateY(-100%)
on hover. The animation works perfectly on Safari and Mobile Safari.

In Chrome and Firefox, using pixels produces the desired effect. Even when all percentages are used but the parent div width is set in pixels, everything works fine. However, when solely relying on percentages, Firefox and Chrome seem to render the values strangely, based on an unclear factor.

Fiddle Example: Viewing this in both Safari and FF/Chrome reveals that Safari renders #pixels the same way the percentage-based #percents should be rendered equally in both browsers. Could it be Safari that's misinterpreting this?

<div class="container">
    <div class="pretendImage"></div>
    <div id="percents"></div>
</div>

This is my CSS:

#percents {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -50% 0 0 -50%;
}
.container {
    position: relative;
    width: 50%;
    height: auto;
}
.pretendImage {
    width: 200px;
    height: 200px;
}

I did find a workaround by introducing an additional wrapper. Nevertheless, I am interested in understanding the root cause behind this behavior.

My workaround: Fiddle utilizes a wrapper to eliminate the need for calculating the top and margins within the inner div which has dimensions set to 80% width and height.

Answer №1

Percentages for height and top are based on the containing block's height, specifically the div labeled "container". The height of this container is determined by the div called "pretendImage".

On the other hand, percentages for width, left, and margin-left rely on the width of the containing block (also known as "container"). This width is calculated relative to its own containing block, in this case the viewport. Make sure you're accounting for these percentages accurately. One workaround could be wrapping the element with a position:relative (using either display:inline-block; or float:left;) and adding another wrapper div for spacing within the viewport.

The tricky part comes with margin-top (and margin-bottom) percentages, which are tied to the width of the container block. Unfortunately, there isn't an easy fix for this unless the aspect ratio of the containing block is fixed and known. In that scenario, you can calculate the necessary percentage value for height from the width and aspect ratio.

It seems like Safari might have some bugs affecting how it interprets these measurements.

Answer №2

After extensive testing on various browsers and mobile devices (Chrome, IE, Firefox, Safari, iPad, iPhone 5 and 6, Android), I have found a solution that works consistently.

To ensure compatibility across all scenarios, it is important to include the complete set of transform rules as shown below:

img.ui-li-thumb {
    position: absolute;
    left: 1px;
    top: 50%;

    -ms-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: translateY(-50%);
}

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

Optimizing Image Fit and Positioning for Different Web Browsers

I have designed a webpage using HTML, JavaScript, and CSS that features a photo carousel allowing users to navigate through images by clicking left and right arrows. The carousel's container is set at 500px tall and takes up 100% width of its parent d ...

Bootstrap doesn't display in the dropdown menu

Struggling to get a dropdown menu to display on my page, my HTML code is: <div class="dropdown d-inline"> <div class="widget-header mr-3"> <button href="#" class="icon icon-sm rounded-circle border" data-toggle="dropdown"><i cl ...

What are some effective CSS styles that can be used to illustrate the concept of "loading . . ."?

I've been leveraging the power of blockUI successfully, but now I'm faced with the challenge of dynamically loading a table row. My research indicates that blockUI may not be compatible with HTML table rows. My idea is to use: jquery.AddClass(" ...

"Resolving the issue with unnecessary line breaks in Optimizepress when input is contained within a paragraph

I am experiencing an issue with the code on my server: <p>This is a test to determine why the <input type="text" style="text-align: center;" value="input"/> element appears on a new line when displayed on a wordpress page.</p> When view ...

Creating a CSS grid layout with an unspecified number of columns all set to equal width

Currently, I am in the process of designing a navigation bar using grids. In essence, when the user is an admin, there should be two additional links at the end of the bar. These links need to have the same width and should not be affected by the amount of ...

Only apply the CSS filter alpha to the parent element and not its children

I am working on implementing an alert message box using HTML and CSS. Below is the code I have so far: <div class="dim" id="msg"> <div class="dialog_wrapper"> <div class="dialog" id="msg_value"></div> ...

Tips for adjusting the width of text input in a bootstrap table

I have a table with 7 text input fields using Bootstrap 2.3 styles. I want to resize these input fields to take up less width in the table cell, but I haven't been successful. I attempted to use the span1 class, but it didn't shrink them as desi ...

Is there a way to perfectly center text both vertically and horizontally to the right side of an image?

I'm working on developing a mobile website using jQuery Mobile and I want to include images in my "collapsible" elements. Here is the code I am using: <div data-role="collapsible" data-collapsed="true"> <h3><img src="image ...

Increase the width of the div to extend it to the right side of the

After analyzing the issue, it seems like the solution lies in extending the wrapper div to the right side of the page without any wrapping. Whenever I attempt to set the width to 100vw or 100%, the content within the div gets pushed below all the other ele ...

Flickering Button Displayed After Fade-In Animation

After scouring the internet for a solution, I still can't seem to figure out why my code isn't working. Whenever an element fades in on Safari, it quickly flickers or flashes white, almost like a rapid refresh is happening. I've tried vario ...

Tips for preventing words from being split between lines in an error message

I am encountering a problem in Angular 2 where error messages are being split onto two lines when there isn't enough space for the entire word. Check out this screenshot for reference Is it possible to prevent words from being divided across lines? ...

What is the best way to collapse a button in asp.net using javascript after setting it to hidden?

I have a scenario where I have 3 buttons in a row on my asp.net page. Using JavaScript, I am setting the middle button (ButtonSR) to invisible. However, I want the button below it to move up and take its place instead of leaving an empty space. ...

Rearrange the sequence of specific child elements within the Div

Is there a way to reverse the order of specific div's children elements using just CSS? For instance: I need <div id="parent"> <a>A</a> <a>B</a> <a>C</a> <a>D</a> &l ...

Assigning a specific tag when utilizing ng-options in Angular

I'm struggling with setting the default value in a dropdown list using ng-option. Can someone take a look at my code and provide some guidance on how to resolve this issue? <div class="form-group"> <label c ...

What is the best way to render JSON information in JSP with JavaScript?

Using Spring MVC, my controller "Book.java" returns a JSON object. I would like to display the content of the JSON object in a table format in a JSP. How can I achieve this? Here is the function in my controller that returns the JSON object: @Controller ...

What could be causing the CSS and HTML connection to fail?

I am currently working with Flask on the Atom editor to build a website. Despite specifying the correct path for the CSS file in the link, every time I load the page, it appears without any styling. I have attempted changing the paths multiple times with n ...

How can I showcase both a username and email address in a Material UI chip?

I'm currently using Material UI chip to show name and email next to each other. However, when the name is long, the email goes beyond the chip boundary. Here's my function that generates the chips: getGuestList() { let {guests} = this.sta ...

Fluctuating problem arises when placing one div over another div while hovering

Sorry if the title isn't clear. Basically, I have a div that when hovered over, triggers another div to appear with display:block, showing it above the original div. It works well but there is some flickering when moving the cursor over the second div ...

Achieving Full Width Coverage for a div with 100% Width Using the Meta Viewport Tag "width=device-width"

Currently, I am working on optimizing my website to adjust for mobile browsers. In order to achieve this, I have two CSS files - one that is always included and another with media="screen and (max-width:500px)" To ensure proper functionality, I am utili ...

The functionality of jQuery's toggleClass is not functioning properly when clicking on a different thumbnail

My objective: I want to display a set of thumbnails. When a thumbnail is clicked, I want to toggle the hiddenElement class and show a large image inside a hidden div. If anywhere in the DOM is clicked, it should hide the current image or allow the user t ...