Style binding: implicitly assign 'px' as the default unit for numeric values for certain css properties
See original GitHub issueWhat 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:
- Created 5 years ago
- Reactions:22
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.Unfortunately, that wouldn’t be consistent and confusing to users