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.

Default binding inline style with number in object to `px`

See original GitHub issue

When we want to bind inline style with object, we might use object in data like this way.

data() {
  return {
    style: {
      width: '400px',
    },
  };
}

I think it might be useful for users to just write number and it will suffix with px automatically.

data() {
  return {
    style: {
      width: 400,
    },
  };
}

Is it possible to default unit with px when using numbers?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
CYBAIcommented, Dec 5, 2016

@kazupon

Looks good to me though I hope vue will support suffix px automatically 😄 Besides, using filters or computed properties might be more flexible for users. Thanks for your reply and great example.

1reaction
kazuponcommented, Dec 5, 2016

@CYBAI I think it’s possible to implement with computed properties. Also, In Vue.js 2.1 later, we can use the filter of v-bind.

e.g.

const prefix = val => {
  return val + 'px'
}
new Vue({
  computed: {
    prefixedStyle () {
      return prefix(this.style)
    }
  },
  data () {
    return {
      style: {
        width: 400,
      }
    }
  },
  filters: { prefix }
}
<!-- computed property -->
<img :width="prefixedStyle">

<!-- v-bind filter-->
<img :width="style | prefix ">
Read more comments on GitHub >

github_iconTop Results From Across the Web

Style binding: implicitly assign 'px' as the default unit ... - GitHub
In React, this is the default behavior. It will add 'px' if the unit is not specified. What does the proposed API look...
Read more >
How to combine multiple inline style objects? - reactjs
I like to use this to make components with default properties. For example, here's a little stateless component with a default margin-right ...
Read more >
Class and Style Bindings - Vue.js
A common need for data binding is manipulating an element's class list and its inline styles. Since they are both attributes, we can...
Read more >
inline-size - CSS: Cascading Style Sheets - MDN Web Docs
The inline-size CSS property defines the horizontal or vertical size of an element's block, depending on its writing mode.
Read more >
Styling a Vue.js application using CSS - LogRocket Blog
In Vue. js, we can add an inline style to our element by binding the style attribute in the HTML tag. For reference,...
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