Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style certain characters with CSS

Tags:

css

I'm assuming it's not possible, but just in case it is or someone has a nice trick up their sleeves, is there a way to target certain characters using CSS?

For example make all letters z in paragraphs red, or in my particular case set vertical-align:sup on all 7 in elements marked with the class chord.

like image 419
Svish Avatar asked Apr 18 '13 19:04

Svish


People also ask

How do I style specific elements in CSS?

The CSS id Selector The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Is it possible to apply CSS to half of a character?

All you need to do is to apply . halfStyle class to each element that contains the character you want to be half-styled. For each span element containing the character, you can create a data attribute, for example here data-content="X" , and on the pseudo element use content: attr(data-content); so the .

How do I style a single letter in CSS?

The ::first-letter selector is used to add a style to the first letter of the specified selector.


Video Answer


3 Answers

Hi I know you said in CSS but as everybody told you, you can't, this is a javascript solution, just my 2 cents.

best...

JSFiddle

css

span.highlight{
   background:#F60;
    padding:5px;
    display:inline-block;
    color:#FFF;
}

p{
   font-family:Verdana;   
}

html

<p>
    Let's go Zapata let's do it for the revolution, Zapatistas!!!   
</p>

javascript

jQuery.fn.highlight = function (str, className) {    
    var regex = new RegExp(str, "gi");

    return this.each(function () {
        this.innerHTML = this.innerHTML.replace(regex, function(matched) {return "<span class=\"" + className + "\">" + matched + "</span>";});
    });
};

$("p").highlight("Z","highlight");

Result enter image description here

like image 93
ncubica Avatar answered Oct 13 '22 09:10

ncubica


That's not possible in CSS. Your only option would be first-letter and that's not going to cut it. Either use JavaScript or (as you stated in your comments), your server-side language.

like image 44
mattytommo Avatar answered Oct 13 '22 08:10

mattytommo


The only way to do it in CSS is to wrap them in a span and give the span a class. Then target all spans with that class.

like image 1
Boy Buysman Avatar answered Oct 13 '22 08:10

Boy Buysman