1

Is there a way to change the CSS of a single character on a complete page:

E.g.

I have few divs on my page

<div class="a">
abcd
</div>
<div class="b">
abcd
</div>
<div class="c">
abcd
</div>
<div class="d">
abcd
</div>

And I want to change CSS of character "d" but only in div that belongs to class "c"

Is there a way to do that.

P.S. I want to change the font of that one single character

4
  • 1
    you can't directly to it, but if you wrap the d in a <span>, you can put the styles on the span. so, yes you can, but you'll have to do some DOM modifications. Commented May 2, 2014 at 18:40
  • Is there just text inside the divs? Commented May 2, 2014 at 18:41
  • @AlexW Yes there is just text inside the div's that I want this feature for Commented May 2, 2014 at 18:42
  • possible duplicate of How to apply a style to a single special HTML character across the page Commented May 2, 2014 at 18:43

4 Answers 4

2

This works for me on Chrome:

var Cs = document.getElementsByClassName('c');

for(var i = 0; i < Cs.length; i++)
{
    var text = Cs[i].textContent;
    Cs[i].innerHTML = text.replace('d','<span class="red">d</span>');
}

http://jsfiddle.net/u5ZC8/

Sign up to request clarification or add additional context in comments.

2 Comments

@kapilchhattani If you want to support IE8 or below, just change textContent to innerHTML. And note that this will work if the div contains text and only text.
I might add something else soon to the div, like few more tags inside it, what will happen after that..
2

My approach (You can shorten/optimize code. This code is very detailed for explanation):

// config
var string = 'd';
var selector = '.c';
var font_size = 28;

// create regex from string
var regex = new RegExp(string,"g");

// wrap span around string and set font-size
var replace = '<span style="font-size: ' + font_size + 'px;">' + string + '</span>';

// find elements with given selector
var el = document.querySelectorAll(selector);
for (var i = 0; i < el.length; i++) {

    // get HTML from element and ...
    var text = el[i].innerHTML;

    // ... replace it with given span element
    var new_text = text.replace(regex, replace);

    // set new HTML
    el[i].innerHTML = new_text;
};

Working fiddle: http://jsfiddle.net/gopeter/tN3Bx/8/

Edit

Sorry, you asked for font-family. New fiddle: http://jsfiddle.net/gopeter/tN3Bx/9/

Comments

0

First you need to find tag which className is c.

var class = document.getElementsByClassName('c');

Then class is [], so we need to use for circulate to find everyone.

for (var i = 0, l = Cs.length; i < l; i++) {
    class[i].innerHTML = class[i].innerHTML.replace('d','<span style="color:red">d</span>');
}

Comments

0

A font can contain a single character customized however you please (FontForge is your friend):

@font-face { font-family: a; src: url(a); }
@font-face { font-family: b; src: url(b); }
@font-face { font-family: c; src: url(c); }
@font-face { font-family: d; src: url(d); }
.a { font-family: a; }
.b { font-family: b; }
.c { font-family: c; }
.d { font-family: d; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.