@Felix's solution is good. But in order to calculate the width of the text, it has the minus of adding a span element to the body. We can use the canvas to calculate the width of the text, but it will not be added to the DOM.
http://jsfiddle.net/s6rrzsa4/16/
var elem = $('input'), cs = window.getComputedStyle(elem[0]), context = document.createElement("canvas").getContext("2d"), metrics, pos; context.font = cs.fontSize + " " + cs.fontFamily; metrics = context.measureText(elem[0].value.toUpperCase()), pos = (elem.width() - metrics.width)/2 + 20; elem.css('background-position','calc(100% - '+pos+'px) center');
Praveen vijayan
source share