Css font color one word

Change the Color of One Word in a String of Text in HTML

Change the Color of One Word in a String of Text in HTML

  1. Change the Color of One Word in a String of Text in HTML
  2. Change the Color of One Word in a String of Text in HTML Using Internal CSS
  3. Change the Color of One Word in a String of Text in HTML Using JavaScript

The main topic of this article is utilizing CSS to highlight or change the color of any particular word in a text. We’ll go over several techniques for implementing this feature.

We will learn to color a text using internal and inline CSS. Later, we’ll look at how to use JavaScript to implement the same functionality.

Change the Color of One Word in a String of Text in HTML

We commonly see on websites that a single word in a text has a different color than the others; of course, it captures the users’ attention. Let’s discuss how we can do the same on our web pages.

In the earlier versions of HTML, we had a tag that can be used to implement this feature like this:

But the tag is removed in HTML5 and is no longer supported. So, we will learn about an HTML tag to help us do the task.

The element is an inline container to mark up a section of a text or a section of a page. The id or class attribute of the tag allows simple styling with CSS and modifications with JavaScript.

The tag also allows us to apply inline styling, similar to the div element. However, is an inline element, whereas div is a block-level element.

Check the example below to understand this.

html>  body>  p>Hello, I am  span style="color: red">Redspan>  p>  body>  html> 

CSS’s color property gives the text a specific color. There are many ways to specify the required color; in the above example, we select the color by its name.

HTML can recognize 16 color names which are black, white, grey, silver, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, and aqua. New browsers can recognize 140 CSS color names.

You can check all the HTML-recognized color names from here. As we mentioned, many other ways to specify the required color exist.

Let’s have a look at different methods.

RGB Colors

RGB stands for red, green, blue. It uses an additive color scheme in which the three primary colors, Red, Green, and Blue, are combined to create each color.

Читайте также:  Форма обратной связи

The red, green, and blue parameters each have a value between 0 and 255 that describes the color’s intensity. This indicates that there are 256 x 256 x 256 = 16,777,216 distinct colors.

For instance, rgb(255, 0, 0) is rendered red because the color red is set to its greatest value, 255, while the other two colors, green and blue, are set to 0. Set all color parameters to zero like this rgb(0,0,0) to display black.

You can see the RGB value of different colors from here.

html>  body>  p>Hello, I am  span style="color: rgb(241, 196, 15 )">Yellowspan>  p>  body>  html> 

RGBA Colors

RGBA colors are an extension of RGB colors, including an Alpha channel that determines a color’s opacity. The syntax for an RGBA color value is:

rgba (red, green, blue, alpha) 

The value of the alpha parameter ranges from 0.0 (complete transparency) to 1.0 (full visibility). You can also use this property for the background colors, as sometimes we need different background colors with various opacity.

HEX Colors

Hex colors use hexadecimal values to represent colors from different color models. Hexadecimal colors are represented by the #RRGGBB , where RR stands for red, GG for green, and BB for blue.

The hexadecimal integers that specify the color’s intensity can range from 00 to FF ; an easy example is #0000FF . Because the blue component is at its highest value of FF while the red and green parts are at their lowest value of 00 , the color is entirely blue.

You can see the hex value of different colors from here.

html>  body>  p>Hello, I am  span style="color: #0000FF">Bluespan>  p>  body>  html> 

HSL Colors

  1. Hue: The hue ranges from 0 to 360 degrees on the color wheel. Red is 0; yellow is 60; green is 120; blue is 240, etc.
  2. Saturation: This quantity is measured as a percentage, with 100% denoting fully saturated (i.e., no shades of grey), 50% denoting 50% grey but with still-visible color, and 0% indicating entirely unsaturated (i.e., completely grey and color invisible).
  3. Lightness: This is also a percentage: 0% is black, and 100% is white. The amount of light we wish to give a color is expressed as a percentage, with 0% being black (where there is no light), 50% representing neither dark nor light, and 100% representing white (complete lightness).

The syntax for HSL color values is:

hsl(hue, saturation, lightness) 

You can see the HSL value of different colors from here.

html>  body>  p>Hello, I am  span style="color: hsl(23, 97%, 50% )">Orangespan>  p>  body>  html> 

HSLA Colors

HSLA colors are an extension of HSL with an Alpha channel specifying a color’s opacity. An HSLA color value is determined with:

hsla(hue, saturation, lightness, alpha) 

The value of the alpha parameter is a number having a range strictly between 0.0, which means fully transparent, and 1.0, which means not transparent.

Читайте также:  Python разделить строку пробелами

Change the Color of One Word in a String of Text in HTML Using Internal CSS

We have seen in detail all the methods of giving color in CSS. We have been using inline CSS for everything up until this point.

However, inline CSS is not a suggested method because it is only tied to the element. We must rewrite much if we want the same functionality on different page regions.

So let’s color our text using Internal CSS, defined in the HTML tag inside a tag.

html>  head>  title>CSS Color Propertytitle>  style>  #rgb  color:rgb(255,0,0);  >  #rgba  color:rgba(255,0,0,0.5);  >  #hex  color:#EE82EE;  >  #hsl  color:hsl(0,50%,50%);  >  #hsla  color:hsla(0,50%,50%,0.5);  >  #built  color:green;  >  style>  head>  body>  h1>  Hello this is span id="built">Built-in colorspan> format.  h1>  h1>  Hello this is span id="rgb">RGBspan> format.  h1>  h1>  Hello this is span id="rgba">RGBAspan> format.  h1>  h1>  Hello this is span id="hex">Hexadecimalspan> format.  h1>  h1>  Hello this is span id="hsl">HSLspan> format.  h1>  h1>  Hello this is span id="hsla">HSLAspan> format.  h1>  body>  html> 

Change the Color of One Word in a String of Text in HTML Using JavaScript

We can change the color of a specific word in a sentence using JavaScript. We need to give an ID to our tag and then get that element from JavaScript using document.getElementById(ID-name) and call the style property on it. Here’s how.

html>  body onload="myFunction()">  p>Hello, I am span id="color-text">Magenta.span>p>  script>  function myFunction()   document.getElementById("color-text").style.color = "magenta";  >  script>  body>  html> 

Источник

How to change the font type, size, and color on a web page

Font size

Computer Hope

This page contains instructions on how to change a font and its color on an HTML (hypertext markup language) web page. With the introduction of HTML5, the proper way to now configure web page fonts is to use CSS (cascading style sheets). The old method, of using an inline style attribute or font tag, is deprecated, and should no longer be used.

Although the deprecated methods may still render correctly in modern Internet browsers, they are no longer guaranteed to do so. To create web pages that display correctly for the maximum number of users, use the CSS methods described on this page.

The methods for changing font attributes on this page work for text contained in most HTML tags, including , , and . These methods also work for text in a table, using the , , and tags.

Читайте также:  Python list превратить в строку

Using CSS for a single application

If you plan on changing the font face and its color for one word, sentence, or paragraph on a web page, configure its attributes in the element tag. Using the style attribute, you may specify the font face and color with font-family, color, and the font size with font-size, as shown in the example below.

Example code

font-size: 20px;">This text has the font Courier, is Blue, and 20px.

Result

This text has the Courier font, is Blue, and 20px in size.

Using CSS for one or more pages

Custom font for one page

In the head portion of your web page, you may insert code between the tabs to change the appearance of your text in various elements. The next blue box contains example code that, once called, would change your font face to Courier and color it red. As you can see, we have defined the class name as «custom.»

Once defined, this styling can be applied to most elements in your page by attaching the class «custom» to them. The following box shows two lines of code and their respective results.

Example

Result

This whole sentence is red and Courier.

Only the word test is red and Courier.

Custom font for many pages

Importing an external CSS file can be very beneficial in that it allows users to change rules for multiple pages at the same time. The following section shows an example for creating a basic CSS file that changes the font and it’s color for most elements. This file may be loaded into more than one web page, even an entire site.

Using any basic text editor, saving the following text as a .css file will prepare it for import.

Once the preceding text is placed into a .css file (we have named ours basic.css), you can link to it from any other page using a line similar to the following example.

Users may change the attributes of elements on a page by changing the code in the imported .css file.

Using the font tag

Although deprecated, the HTML tag can still be used and may be necessary to be used with some online services. When using the font tag, you must include the face attribute, which describes the font to be used. In the example below, we are using the Courier font and the hexadecimal color code #005CB9, which is dark blue.

Example code

Result

  • How to change the font color, size, style, or type in Word.
  • See our color codes page for a complete listing of HTML color codes and examples.
  • See our font and font size definitions for further information and related links.
  • How to change the background and text color of a web page.
  • How to center text in HTML.
  • How to get started in HTML and web design.
  • HTML and web design help and support.
  • CSS and HTML color help and support.

Источник

Оцените статью