Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to color specific word in a container using CSS

Tags:

Suppose I have a container:

 <div id="container"> This is a red apple </div> 

How to color a word "red" with red color? Something like (pseudocode)

#container:[word="red"]{    color:red; } 

Is it possible to do this in pure CSS without introducing JavaScript or modifying the existing HTML?

like image 936
nicael Avatar asked May 19 '14 12:05

nicael


People also ask

How do I color a specific word in CSS?

To colored just one word you can use <span style="your style"> WORD</span> . This way you don't have to style the whole paragraph.

How do you color individual letters in CSS?

From css you can only change an elements property so you need to insert the letter "A" in another element like this: ST<span>A</span>CK OVER FLOW just the 'A' letter is red! ST<span class="myRedA">A</span>CK OVER FLOW just the '<A' letter is red!

How do I select a specific word in CSS?

You can use Javascript or jQuery to achieve what you want though : You search the content of your div to try and find the words like "red", "blue" You wrap a span with a class="color" around the word. You apply the styling to that span in your CSS.

How do I color text in a div?

To change the font color of a div using JavaScript, get reference to the element, and assign required color value to the element. style. color property.


2 Answers

Background

This is a Vanilla JavaScript solution because it ain't possible with CSS. Not joking, read the specification. You can match on an element, the name of an attribute in the element, and the value of a named attribute in an element. I don't see anything for matching content within an element though.

Introduction

Here's my shot at it. I am sure there is a sleeker way, but this is the gist of how it would start off as. Also, since there are a finite number of colors that you will want to colorify, it's nice to use a bunch of if statements like I have.

A better technique of course would be to do it more programmatically by building a color dictionary and hence make the code organized. But this works, and it's Vanilla JS. Apparently, I didn't have expertise in Regex, so I am sure a few lines are unnecessary.

Features

  • Works for multiple color occurrences
  • Works for multiple colors as visible

http://jsfiddle.net/TB62H/5/

var text = document.getElementById("content"); var str = text.innerHTML,     reg = /red|blue|green|orange/ig; //g is to replace all occurances  //fixing a bit var toStr = String(reg); var color = (toStr.replace('\/g', '|')).substring(1);  //split it baby var colors = color.split("|");  if (colors.indexOf("red") > -1) {     str = str.replace(/red/g, '<span style="color:red;">red</span>'); }  if (colors.indexOf("blue") > -1) {     str = str.replace(/blue/g, '<span style="color:blue;">blue</span>'); }  if (colors.indexOf("green") > -1) {     str = str.replace(/green/g, '<span style="color:green;">green</span>'); }  if (colors.indexOf("orange") > -1) {     str = str.replace(/orange/g, '<span style="color:orange;">orange</span>'); }   document.getElementById("updated").innerHTML = str; 

Results

enter image description here

like image 96
Ali Gajani Avatar answered Sep 18 '22 04:09

Ali Gajani


Not to prove a point, but to answer your question - this is possible in CSS without JS: Example

In short: we set a black background color for text color and a red background image for the specific red string. We remove the original text fill using -webkit-text-fill-color. The background is clipped to the text outline using -webkit-background-clip: text; and the red image is sized and positioned over whatever text string we want to color.

Please note: I would never recommend using this for any live website. This works in webkit only as it's based on non-standard wekbit-specific CSS rules. And the color is not really bound to the colored text string - it's completely static.

Edit: Here's the CSS:

#container {     -webkit-text-fill-color: transparent;     -webkit-background-clip: text;     background-size: 1.5em 1em;     background-repeat: no-repeat;     background-position: 3.4em 0;     background-color: #000;     background-image: url(data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5rooor8DP9oD/2Q==); } 
like image 33
agrm Avatar answered Sep 19 '22 04:09

agrm