What is the best way to implement relative units for background-position while working with a background-image set to background-size=cover in a consistent manner

I am facing an issue while using background-position in a div alongside background-size: cover. There appears to be some discrepancies in the browser's calculations, so I am seeking a reliable method to address this.

Further details...

The primary focus is on visual aesthetics and ensuring that all elements scale appropriately within the interface. In previous experiences, utilizing rem or em units for styling has yielded positive outcomes.

Upon initialization or screen size adjustments, I evaluate the available space and dynamically set a suitable font-size on the container as demonstrated below:

const size = calculateSize();
$("#container").css({fontSize: size + 'px'});

Generally, this approach works effectively, allowing elements to scale and position correctly. However, a recent addition of a graphic button – styled with a background image – introduced a minor yet bothersome flaw.

.button {
    background-image: url("img/button.png");
    background-size: cover;
    width: 10em;
    height: 4.5em;
    cursor: pointer;
}

.button:hover {
    background-position-x: -100%;
}

While experimenting with background-position-x: -10em, I leaned towards percentage notation due to its adaptability when resizing the button image. Yet, upon hovering over the button, a slight movement is noticed, with the displacement varying based on available space - a phenomenon observed across Chrome, Firefox, and Edge browsers.

While seemingly insignificant, this anomaly sparks curiosity if others have encountered similar behavior and devised solutions. One plausible fix could involve employing distinct images for various button states, albeit I prefer minimizing image count.

UPDATE: After creating two separate images and adjusting the CSS accordingly...

.button {
    background-image: url("img/button0.png");
    background-size: cover;
    width: 10em;
    height: 4.5em;
    cursor: pointer;
}

.button:hover {
    background-image: url("img/button1.png");
    /* background-position-x: -100%; */
}

This modification negated the wobble effect, hinting at a peculiar interpretation issue concerning background-position-x by browsers. Given the subtle nature of the motion, it likely stems from rounding errors.

Minimal, Reproducible Example:

To validate observations, a jsFiddle demonstration highlighting the issue was prepared...

https://jsfiddle.net/xtempore/nfLh86sm/8/

A simplified version of the button image was used, exhibiting black and pale grey hues split down the middle across four divs each configured with varying font-size settings.

Upon hovering, a color transition should occur, notably noticeable on the first and third divs. Conversely, unexpected residue emerges on the second and fourth divs' left edge during hover actions, particularly prevalent with odd-numbered pixel values and decimals in font-size measurements.

Answer №1

background-size: cover;
background-size: 200%;
background-size should be set to 200%. Similarly, for images three times the size, it would be 300%, and so forth.

cover to 200%.

https://jsfiddle.net/xtempore/nfLh86sm/8/

https://jsfiddle.net/xtempore/2vcg4h1L/

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

CSS/HTML - Issue with nested divs not displaying properly

Attached is an image that provides context for the issue I'm facing. https://i.stack.imgur.com/nQXh8.jpg I'm encountering difficulties with styling a nested div element. When double-clicking the text within this div, only half of it gets highlig ...

Use JavaScript to convert only the initial letter to uppercase

Once again, I am in the process of learning! Apologies for the simple questions, but learning is key... Attempting to implement a trick I found online to change all letters to uppercase, I am now trying to adjust it to only capitalize the first letters. T ...

Placement of Buttons Inside a Division Tag

Is there a better way to align button 3 & 4 in a vertical column next to button 1 & 2 without using tables in CSS? See the working example below: https://jsfiddle.net/Jaron787/0te3cs66/1/ Code: HTML <div id="lse" class="display"> <di ...

Superfish - Adjust the Gap between each button

Is there a way to adjust the spacing between buttons on my superfish menu while using the vertical Menu CSS? ...

Is there a way to have an image expand to fit the entire screen before scrolling to view another image?

I'm working on a website and I want to have an image that automatically adjusts to fit any screen size. Users should be able to scroll down to see more content or another image. A similar effect can be seen on this site: . Any suggestions would be rea ...

Styling a floating options menu in React Select

Hey there, I've been tackling the react-select implementation and ran into a css issue. When react-select is opened within a container with overflow:auto (which I can't modify since it's not part of my app), the options are displaying like ...

Content overlapped by Bootstrap footer

I've been grappling with this problem for quite some time now. Despite finding numerous discussions on the issue, none of the proposed solutions seem to work for me. My website utilizes a Bootstrap 3 front end and I am aiming to establish a sticky bo ...

Ways to switch out the background image using jQuery

I am currently using jQuery to dynamically change the background image of a web page. Right now, I have implemented two separate buttons that toggle between Image A and Image B. By default, Image A is displayed on the page. My goal is to enhance this func ...

Issue with React-Route not displaying components

Below is the code snippet from my app.js file: <Router> <Header title="My Todos List" /> <Routes> <Route path="/about" element={<About />} /> <Route path="/" ...

Creating a personalized stepper component (using custom icons) using HTML

Seeking guidance on how to achieve a design similar to the image below using HTML and CSS. After conducting extensive research, I have not been able to find a suitable solution. I attempted to modify this answer to fit my requirements, but it did not prod ...

Submitting a form that combines Dropzone JS and Django forms using a single submit button

I have a unique challenge where I am using dropzone js to create a form for users to upload information and images. Dropzone js requires a form with a class of dropzone for drag and drop image uploads to function properly, resulting in two forms: the reg ...

Setting the css height to 100% won't be effective if the elements are floated either to the right or left

I'm currently working on a web project and I need to create an HTML page. I'd like to adjust the height of an element to a percentage to ensure it fits the page well. However, when I try to use the float property in CSS and set it as follows: bo ...

"Aligning the logo and navbar links to appear on the same line

I'm looking to align the logo and the navigation bar links on the same line. The logo should be displayed alone when viewed on a mobile device. Here's how it currently appears: Currently shown on two lines. This is the HTML code for it: ...

Creating a glassy appearance: What's the process?

I want to achieve a similar glass effect as shown in this screenshot here: (), but despite trying to adjust the opacity, I can't seem to get it right. Here's my code: .Head { position: absolute; top:200px; margin-left: 20%; c ...

Exploring the power of Vue.js by utilizing nested components within single file components

I have been attempting to implement nested single file components, but it's not working as expected. Below is a snippet of my main.js file : import Vue from 'vue' import BootstrapVue from "bootstrap-vue" import App from './App.vue&apos ...

"Selecting multiple options is not possible as the size attribute cannot be utilized

My <select multiple="multiple">..</select> element is set to a height of 1-row due to the select{heigth: 30px;} style in an inaccessible stylesheet. The "size" attribute does not work in this case. What can I do to fix this issue? ...

Looking for the optimal width ranges for media queries on laptops and cell phones?

Can anyone suggest the optimal width parameters to use in my code for different screen sizes, from tablets and laptops to cellphones, in order to create a responsive design? ...

"How to Align Sliding Images in the Center Across Various Web Browsers

I am trying to adjust the sliding image on my webpage's page banner so that it appears in the center instead of floating to the left or right. Here is the HTML code: <ul class"rslides" id="slides"> <li><img src"image/test.jpg">&l ...

How can I place a submit button on a separate line using CSS?

Is there a way to position an input type=submit below a text area without relying on br tags or div elements? I'd prefer a solution using CSS. ...

Using jQuery to create a flawless animation

I am currently working on an animation project, and I have shared my progress on jsfiddle. Below is the code snippet I have utilized: /* JavaScript: */ var app = function () { var self = this; var allBoxes = $('.box&apos ...