This might sound like a simple fix, but let me explain it in detail.
I've an <input type="submit"/>
button and I'm trying to align the arrow to the right of the button text (which is already center aligned).
Here's the fiddle: http://jsfiddle.net/s6rrzsa4/4/
Now coming to the issues:
<input>
to a <button>
tag since it comes
from a CMS).First, set a background image using the background-image property. Next, set the background-repeat property to no-repeat . Then, write the fixed option for the background-attachment property. After that, apply the background-position property to the center center option and the background-size property to cover .
Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.
The default button in HTML can be changed to an image using CSS. The required button is selected using the respective CSS selector. The background property can then be set to include a background image and change the image type as required.
The background-attachment property sets whether a background image scrolls with the rest of the page, or is fixed.
For finding the text length, used a dummy span
and used some javascript to calculate the background-position-x
.
var a = $("input").outerWidth();
var s = $('<span class="dummy" />').html($("input").attr('value'));
s.appendTo('body');
var b = s.outerWidth();
var pos = b + (a-b)/2;
$("input").css({
'background-position': pos +'px center'
});
s.remove();
Please check the fiddle - http://jsfiddle.net/afelixj/s6rrzsa4/9/
@felix solution is good. But it has downside of appending span element to body, in order to calculate width of the text. We can use canvas to calculate the width of the text yet it won't be append to 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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With