ntcolor() - JavaScript - MDN Web Docs

  1. References
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. String
  6. String.prototype.fontcolor()
Article Actions
  • English (US)
    • Remember language
    • Deutsch
    • Español
    • Français
    • 日本語
    • 한국어
    • Português (do Brasil)
    • Русский
    • 中文 (简体)
  • Syntax
  • Description
  • Examples
  • Specifications
  • Browser compatibility
  • See also
  1. Standard built-in objects
  2. String
  3. Constructor
    1. String() constructor
  4. Static methods
    1. String.fromCharCode()
    2. String.fromCodePoint()
    3. String.raw()
  5. Instance methods
    1. String.prototype.anchor() Deprecated
    2. String.prototype.at()
    3. String.prototype.big() Deprecated
    4. String.prototype.blink() Deprecated
    5. String.prototype.bold() Deprecated
    6. String.prototype.charAt()
    7. String.prototype.charCodeAt()
    8. String.prototype.codePointAt()
    9. String.prototype.concat()
    10. String.prototype.endsWith()
    11. String.prototype.fixed() Deprecated
    12. String.prototype.fontcolor() Deprecated
    13. String.prototype.fontsize() Deprecated
    14. String.prototype.includes()
    15. String.prototype.indexOf()
    16. String.prototype.isWellFormed()
    17. String.prototype.italics() Deprecated
    18. String.prototype.lastIndexOf()
    19. String.prototype.link() Deprecated
    20. String.prototype.localeCompare()
    21. String.prototype.match()
    22. String.prototype.matchAll()
    23. String.prototype.normalize()
    24. String.prototype.padEnd()
    25. String.prototype.padStart()
    26. String.prototype.repeat()
    27. String.prototype.replace()
    28. String.prototype.replaceAll()
    29. String.prototype.search()
    30. String.prototype.slice()
    31. String.prototype.small() Deprecated
    32. String.prototype.split()
    33. String.prototype.startsWith()
    34. String.prototype.strike() Deprecated
    35. String.prototype.sub() Deprecated
    36. String.prototype.substr() Deprecated
    37. String.prototype.substring()
    38. String.prototype.sup() Deprecated
    39. String.prototype.toLocaleLowerCase()
    40. String.prototype.toLocaleUpperCase()
    41. String.prototype.toLowerCase()
    42. String.prototype.toString()
    43. String.prototype.toUpperCase()
    44. String.prototype.toWellFormed()
    45. String.prototype.trim()
    46. String.prototype.trimEnd()
    47. String.prototype.trimStart()
    48. String.prototype.valueOf()
    49. String.prototype[Symbol.iterator]()
  6. Instance properties
    1. String: length
  7. Inheritance
  8. Object/Function
  9. Static methods
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toString()
    5. Function.prototype[Symbol.hasInstance]()
  10. Static properties
    1. Function: displayName Non-standard
    2. Function: length
    3. Function: name
    4. Function: prototype
    5. Function.prototype.arguments Non-standard Deprecated
    6. Function.prototype.caller Non-standard Deprecated
  11. Instance methods
    1. Object.prototype.__defineGetter__() Deprecated
    2. Object.prototype.__defineSetter__() Deprecated
    3. Object.prototype.__lookupGetter__() Deprecated
    4. Object.prototype.__lookupSetter__() Deprecated
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toString()
    10. Object.prototype.valueOf()
  12. Instance properties
    1. Object.prototype.__proto__ Deprecated
    2. Object.prototype.constructor
  • Syntax
  • Description
  • Examples
  • Specifications
  • Browser compatibility
  • See also

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The fontcolor() method of String values creates a string that embeds this string in a <font> element (<font color="...">str</font>), which causes this string to be displayed in the specified font color.

Note: All HTML wrapper methods are deprecated and only standardized for compatibility purposes. For the case of fontcolor(), the <font> element itself has been removed from the HTML specification and shouldn't be used anymore. Web developers should use CSS properties instead.

Syntax

jsfontcolor(color)

Parameters

color

A string expressing the color as a hexadecimal RGB triplet or as a string literal. String literals for color names are listed in the CSS color reference.

Return value

A string beginning with a <font color="color"> start tag (double quotes in color are replaced with &quot;), then the text str, and then a </font> end tag.

Description

The fontcolor() method itself simply joins the string parts together without any validation or normalization. However, to create valid <font> elements, if you express color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072".

Examples

Using fontcolor()

The code below creates an HTML string and then replaces the document's body with it:

jsconst contentString = "Hello, world"; document.body.innerHTML = contentString.fontcolor("red");

This will create the following HTML:

html<font color="red">Hello, world</font>

Warning: This markup is invalid, because font is no longer a valid element.

Instead of using fontcolor() and creating HTML text directly, you should use CSS to manipulate fonts. For example, you can manipulate color through the element.style attribute:

jsdocument.getElementById("yourElemId").style.color = "red";

Specifications

Specification
ECMAScript Language Specification # sec-string.prototype.fontcolor

Browser compatibility

BCD tables only load in the browser

See also

  • Polyfill of String.prototype.fontcolor in core-js
  • HTML wrapper methods
  • <font>

Help improve MDN

Was this page helpful to you?YesNoLearn how to contribute.

This page was last modified on Jul 25, 2024 by MDN contributors.

View this page on GitHub • Report a problem with this content

Từ khóa » Html Color Text Javascript