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.

Style binding: implicitly assign 'px' as the default unit for numeric values for certain css properties

See original GitHub issue

What problem does this feature solve?

When assigning values to a CSS property via style binding like

    <div class="d-inline-block" :style="{width : width}"></div>

or using the shorthand syntax…

    <div class="d-inline-block" :style="{width}"></div>

this will actually not gonna work because we need to explicitly set the unit:

    <div class="d-inline-block" :style="{width : width + 'px'}"></div>

or via interpolation…

    <div class="d-inline-block" :style="{width : `${width}px`}"></div>

Now our code becomes more verbose. Combining all the number of occurrences of such binding, it’s becoming ridiculous.

I’m not sure if currently there is an elegant solution for this, but if there is. It would be also good if it is referenced in in the docs.

In React, this is the default behavior. It will add ‘px’ if the unit is not specified.

What does the proposed API look like?

For common css properties like width and height, having vue implicitly assign ‘px’ to the value it would definitely make our code much more cleaner.

This code…

    <div class="d-inline-block" :style="{width}"></div>

will be interpreted as:

    <div class="d-inline-block" :style="{width : width + 'px'}"></div>

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:22
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Justineocommented, Sep 5, 2018

TBH I’m not a fan of this feature. A CSS property can accept multiple value types and even it’s “unitless” today doesn’t mean it will stay “unitless” forever, and vice versa. As the language evolves, maybe someday a “unitless” property can take length units, or those accept length units can take “unitless” values. If a property use to not accept “unitless” values and later changed to accept them, we would never know the true semantics behind a number if we had supported appending px to it by default.

5reactions
posvacommented, Sep 5, 2018

Unfortunately, that wouldn’t be consistent and confusing to users

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Values and Units Module Level 3 - W3C
This CSS module describes the common values and units that CSS properties accept and the syntax used for describing them in CSS property...
Read more >
Using CSS custom properties (variables) - MDN Web Docs
Each CSS property can be assigned a defined set of values. If you try to assign a value to a property that is...
Read more >
Constraint CSS - GSS
When using a property known by CSS, the GSS computed value for that property will be assigned as an inline style in pixels...
Read more >
Dynamically updating css in Angular 2 - Stack Overflow
Try this <div class="home-component" [style.width.px]="width" [style.height.px]="height">Some stuff in this div</div>. [Updated]: To set in ...
Read more >
The "style" binding - Knockout.js
Note 2: Setting styles which require a unit ... If you apply a simple numeric value to a style that requires a unit,...
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