How do I style a specific word with CSS in an HTML element?
You should do this:
p {
font-size: 12px;
}
span {
font-size: 16px;
font-weight: bold;
}
<p>
<span>STUDIO X</span> is the best studio ever
</p>
How to add style for specific words at HTML using JavaScript?
This is the best solution. It works fine at any html, just call matchText("text","black","red");
function replaceInElement(element, find, replace) {
for (let i= element.childNodes.length; i-->0;) {
let child= element.childNodes[i];
if (child.nodeType==1) {
let tag= child.nodeName.toLowerCase();
if (tag!='style' && tag!='script')
replaceInElement(child, find, replace);
} else if (child.nodeType==3) {
replaceInText(child, find, replace);
}
}
}
function replaceInText(text, find, replace) {
let match;
let matches= [];
while (match= find.exec(text.data)){
matches.push(match);
}
console.log(matches);
for (let i= matches.length; i-->0;) {
match = matches[i];
text.splitText(match.index);
text.nextSibling.splitText(match[0].length);
text.parentNode.replaceChild(replace(match), text.nextSibling);
}
}
let matchText = (word, backColor, textColor) => {
let regexp = new RegExp(`\\b(${word})\\b`, 'gmi');
replaceInElement(document.body, regexp, function(match) {
var elem= document.createElement('span');
elem.style.backgroundColor = backColor;
elem.style.color = textColor;
elem.appendChild(document.createTextNode(match[0]));
return elem;
});
}
How to color specific word in a container using CSS
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==);
}
Change text style for specific words within CSS Content
This would be the simplest javascript I could come up with: https://jsfiddle.net/vctbszc2/1/
HTML
<p>
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah DEMO blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
</p>
Javascript
var par = document.getElementsByTagName('p');
for(var i = 0; i < par.length; i++)
{
var s = par[i].innerHTML;
s = s.replace('DEMO', '<span style="color:green; font-size: 40px;">DEMO</span>');
par[i].innerHTML = s;
}
How can we style a word in css with special character?
This is not something that can be done with only CSS. You will need JavaScript and the example below in raw JS without the need for any framework, but it can be use in any framework.
Try this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Twitter CSS 1</title>
<style>
#input {
font: 14px/1.2em Tahoma;
height: 12em;
width: 500px;
}
.class-at {
color: blue;
}
.class-hash {
color: green;
}
</style>
</head>
<body>
<h3>Enter Text Here</h3>
<textarea id="input">@_NAN_DINI @InTheWordsOfK @maidros78 Self immolation is still a form of social protest in India. Remember the guy w…</textarea>
<hr/>
<div id="output"></div>
<script>
var inEl = document.getElementById('input');
var outEl = document.getElementById('output');
function encodeStr(str) {
return str.replace(/(\@\S+)/g, (key) => `<span class="class-at">${key}</span>`).replace(/(\#\S+)/g, (key) => `<span class="class-hash">${key}</span>`);
}
function inputHandler() {
outEl.innerHTML = encodeStr(inEl.value);
}
inEl.addEventListener('input', inputHandler);
inputHandler();
</script>
</body>
</html>
See: https://jsfiddle.net/intervalia/bu3rxq8q/
The function encodeStr
contains two calls to replace
. And those add either a <span class="class-at">
or a <span class="class-hash">
around the words you are looking for.
All that is needed
The function
encodeStr
is all the JS that is really needed to do the conversion. It is up to you to get the string into the function and use the result. You will also need the CSS that colors your fields the color you want..class-at
and.class-hash
. Of course you can change those to whatever you want them to be called.
function encodeStr(str) {
return str.replace(/(\@\S+)/g, (key) => `<span class="class-at">${key}</span>`).replace(/(\#\S+)/g, (key) => `<span class="class-hash">${key}</span>`);
}
Related Topics
CSS Which Takes Precedence, Inline or the Class
Amp: How to Toggle a CSS Class
How to Change the Size of a Bootstrap Checkbox
CSS Transition Opacity Fade Background
Looping Animation of Text Color Change Using CSS3
Immediate Child Selector in Less
How to Set a Common Image Path for Less Files
How to Generate CSS with Loop in Less
Rotating Table Header Text with CSS Transforms
Set Bootstrap Navbar Transparency on Scroll
How to Make Circular Background Using CSS
Are There Any Tools for Merging CSS