Snorlax Snorlax - 5 months ago 10
jQuery Question

Make specific character (or class/ID) be the center point of a string

I would like to align some text in the center, but not just by doing

text-align: center
.

I would like to make a specific character/class/ID be the center, while the rest of the string gets appended and prepended nicely. Here is what I mean:

https://jsfiddle.net/6w5Lb6ta/



As you can see, the red
c
in the middle is aligned somewhat in the middle (I used
margin-right
to make this small example). I want to say something like:


align all the text in the middle, but make the 'anchor point' of the string a specific character/class/ID


Is something like this doable?

Answer

This could be done with some jQuery:

var width = $('#text').width();
var left = $('#center').position().left;
$('#text').css('margin-left', (left-width)/2);

https://jsfiddle.net/6w5Lb6ta/1/

Comments