Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center align background-image of an input button

Tags:

jquery

css

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/ submit button

Now coming to the issues:

  1. I cannot change the <input> to a <button> tag since it comes from a CMS).
  2. The button text can be anything, which means I cannot crop the arrow image with an offset(equal to the width of the text) and use background-position:center.
  3. I cannot add :after pseudo element for the parent div, since it makes the arrow area unclickable.
  4. As I already said the button text can change. So I cannot use percentages for background-position.
  5. The button has a fixed width (sorry, forgot to add this point).
like image 504
gopalraju Avatar asked Jun 17 '15 07:06

gopalraju


People also ask

How do I center a background image in HTML?

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 .

How do I center an image in CSS?

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.

Can a button have a background image?

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.

Which property controls the image scroll in the background?

The background-attachment property sets whether a background image scrolls with the rest of the page, or is fixed.


2 Answers

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/

like image 156
Felix A J Avatar answered Sep 19 '22 21:09

Felix A J


@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');
like image 38
Praveen Vijayan Avatar answered Sep 22 '22 21:09

Praveen Vijayan