question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS `backgroundSize` property lost when re-rendering an image URL inside of an adjacent `background` property

See original GitHub issue

The CSS background property can shorthanded in many various ways. When inlining multiple styles including that property, React seems to optimize them on re-render.

For example, if we define a style like so:

var style = {
    background: `#ffffff url(${this.state.url})`,
    backgroundPosition: `${this.state.x}% ${this.state.y}%`,
    backgroundSize: 'cover'
};

The initial render returns a style in the form: style="background:#ffffff url([URL]);background-position:[X]% [Y]%;background-size:cover;"

If we change the state (such as this.state.x) to cause a re-render, the style format is changed to: style="background: url([URL]) [X']% [Y]% / cover rgb(255, 255, 255);"

Where this seems to break down is while changing the state of a URL inside of the background property while also defining a backgroundSize.

var style = {
    background: `#ffffff url(${url})`,
    backgroundSize: 'cover'
};

The initial render will return the expected style: style="background:#ffffff url(http://i.imgur.com/aPd8qDo.png);background-size:cover;"

However, changing the URL results in rendering a style without background-size: style="background: url([URL]) rgb(255, 255, 255);"


Example Demo

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:13
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
zpaocommented, Oct 1, 2015

This is a known issue with CSS shorthand expansion. For now the recommendation is to list each explicit property and not rely on the shorthand. Usually these bugs only surface on update because initial render simply creates an inline style as a part of setting innerHTML. Updates work on individual properties so when you set background on the update we notice that you only changed background so only set that property, which since it’s a shorthand, clears out the backgroundSize property.

See #4661 for more discussion.

0reactions
huoyingjianshencommented, Apr 30, 2020

use code follow:

getStyle = () => {
    var style = {
                background: "url(xxxxxxxxxx") no-repeat center",
                backgroundSize: "100% 100%"
            }
    return style;
}
<div style={getStyle}></div>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Resizing background images with background-size - CSS
The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at ...
Read more >
background-size not working. How do I fix the obvious?
No matter what, the image always renders at full size within the div. Whatever I set in background-size, nothing works.
Read more >
background-position | CSS-Tricks
The background-position property in CSS allows you to move a background image (or gradient) around within its container.
Read more >
A Deep Dive Into object-fit And background-size In CSS
We're not always able to load different-sized images for an HTML element. If we use a width and height that isn't proportional to...
Read more >
CSS Backgrounds and Borders Module Level 3 - W3C
5.1 Image Source: the border-image-source property; 5.2 Image ... dropped and the missing values for background-origin and background-repeat ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found