I am writing a blogger which is about programming. I want it to automatically change a color of specific character without manually put a class or id into it. For example,
<blockquote class="tr_bq">
<span style="color: #bb60d5;">$</span>
<span>php app/console doctrine:schema:create</span>
</blockquote>
When I put the "$"
character inside the blockquote tag
, It would automatically add color: #bb60d5
to change its color.
Is there a way to do that?
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. Example: <p> The quick brown <span style="color: brown">fox</span> jumps over... </p> .
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! Save this answer.
How Do I Highlight Text In CSS? To Highlight text in HTML you have to use an inline element such as the <span> element and apply a specific background style on it. This will create the highlighting effect, which you can tweak in many different ways to create different looks.
In order to style something in CSS, you need a selector to target it. If you're going to style individual letters, you need to wrap each one in an HTML element so that you can then select it in CSS.
There's no way to do this using CSS, you can however use javascript to scan the page on load and wrap all instances of $
in a span with your custom style, here's an implementation using jQuery:
$('blockquote').each(function () {
$(this).html($(this).html().replace(/(\$)/g, '<span style="color: #bb60d5;">$1</span>'));
});
Demo fiddle
CSS can't find words or something related to that and it can't automatically just add colors or apply styles.
You might use JavaScript, or you can use some server side coding to find out this word '$' and apply some style to that div. For example:
<div style="color: red;">
$<span style="color: black">php app/console doctrine:schema:create</span>
</div>
This will work when you know that the server will read the files, in ASP you can use string.Split()
to find out some parts. You can use Regex, for PHP, you might need to search!
But it won't work in CSS, its just JavaScript or Server Side. You can use CSS to just apply colors, or just use style
in each element itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With