Javascript text font style

How to Change Font Family of HTML Element in JavaScript?

JavaScript – Change the Font Family of HTML Element

To change the font family of a HTML Element using JavaScript, get reference to the element, and assign required font family value to the element.style.fontFamily property.

In the following example, we will change the font family of HTML Element with id «myElement» to «monospace» , in JavaScript, using element.style.fontFamily property.

example.html

Conclusion

In this JavaScript Tutorial, we learned how to change the font family of a HTML Element using JavaScript.

  • How to Change Border Color of HTML Element in JavaScript?
  • How to Change Border Radius of HTML Element in JavaScript?
  • How to Change Border Style of HTML Element in JavaScript?
  • How to Change Border Width of HTML Element in JavaScript?
  • How to Change Bottom Border of HTML Element in JavaScript?
  • How to Change Font Color of HTML Element in JavaScript?
  • How to Change Font Size of HTML Element in JavaScript?
  • How to Change Font Weight of HTML Element in JavaScript?
  • How to Change Height of HTML Element in JavaScript?
  • How to Change Left Border of HTML Element in JavaScript?
  • How to Change Margin of HTML Element in JavaScript?
  • How to Change Opacity of HTML Element in JavaScript?
  • How to Change Padding of HTML Element in JavaScript?
  • How to Change Right Border of HTML Element in JavaScript?
  • How to Change Text in HTML Element to Bold in JavaScript?
  • How to Change Text in HTML Element to Italic in JavaScript?
  • How to Change Top Border of HTML Element in JavaScript?
  • How to Change Width of HTML Element in JavaScript?
  • How to Change the Background Color of HTML Element in JavaScript?
  • How to Change the Border of HTML Element in JavaScript?
  • How to Clear Inline Style of HTML Element in JavaScript?
  • How to Get Children of an HTML Element in JavaScript?
  • How to Get Class Names of an HTML Element as List in JavaScript?
  • How to Get Class Names of an HTML Element as String in JavaScript?
  • How to Get First Child of an HTML Element in JavaScript?
  • How to Get Height of an HTML Element in JavaScript?
  • How to Get Last Child of an HTML Element in JavaScript?
  • How to Get Next Sibling of an HTML Element in JavaScript?
  • How to Get Previous Sibling of an HTML Element in JavaScript?
  • How to Get Width of an HTML Element in JavaScript?
  • How to Hide HTML Element in JavaScript?
  • How to Insert Element in Document after Specific HTML Element using JavaScript?
  • How to Iterate over Children of HTML Element in JavaScript?
  • How to Tag Name of an HTML Element in JavaScript?
  • How to Underline Text in HTML Element in JavaScript?
  • How to get Attributes of HTML Element Element in JavaScript?
  • How to get Number of Child Elements of this HTML Element in JavaScript?
  • JavaScript – Change Style of HTML Element
Читайте также:  Html код для оставить комментарий

Источник

Style font Property

The font-size and font-family are required. If one of the other values are missing, the default values will be inserted, if any.

The properties above can also be set with separate style properties. The use of separate properties is highly recommended for non-advanced authors for better controllability.

Browser Support

Syntax

object.style.font = «font-style font-variant font-weight font-size/line-height|caption|icon|menu|
message-box|small-caption|status-bar|initial|inherit;»

Property Values

Value Description
style Sets the font-style
variant Sets the text in a small-caps font
weight Sets the boldness of the font
size Sets the size of the font
lineHeight Sets the distance between lines
family Sets the font face
caption The font used for captioned controls (like buttons, drop-downs, etc.)
icon The font used to label icons
menu The font used in menus
message-box The font used in dialog boxes
small-caption The font used in small controls
status-bar The font used in window status bars
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value: not specified
Return Value: A String, representing different font properties of the element
CSS Version CSS1

More Examples

Example

Return the font of an element:

Источник

Style fontStyle Property

The fontStyle property sets or returns whether the style of the font is normal, italic or oblique.

Browser Support

Syntax

Return the fontStyle property:

Set the fontStyle property:

Property Values

Value Description
normal Font is normal. This is default
italic Font is in italic
oblique Font is in oblique
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value: normal
Return Value: A String, representing the font style of the text in the element
CSS Version CSS1

More Examples

Example

A demonstration of possible values:

var listValue = selectTag.options[selectTag.selectedIndex].text;
document.getElementById(«demo»).style.fontStyle = listValue;

Example

Return the font style of an element:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Читайте также:  Javascript foreach value in array

Источник

How to set the font family for text with JavaScript?

In this tutorial, we will learn how to set the font family for text with JavaScript.

The font-family is a CSS property that specifies a prioritized list containing one or more font family names and generic family names for the text of an HTML element. To set the font-family for text with JavaScript, we have multiple ways, and in this tutorial, we will discuss two of them −

Using the style.fontFamily Property

The style.fontFamily property sets a list of font family names and/or generic family names for the text. The browser will set the first value from the list that it recognizes. The element object contains this property, so first, the document.getElementById() method is used to access the element object, and then we use the style.fontFamily property to set the font family for text with JavaScript.

Syntax

const element = document.getElementById('id') element.style.fontFamily = family_name | generic_family | inherit | initial

In the above syntax, the ‘id’ is the id of an element. Using the document.getElementById() method we are accessing that element object and set the style.fontFamily property.

Parameters

  • family_name − The name of one or multiple font families.
  • generic_family − The name of one or multiple generic families.
  • inherit − The font-family property is inherited by its parent element’s property.
  • initial − The font-family property is set to default.

Example

In the below example, we have used the style.fontFamily property to set the font family for text with JavaScript. We have used a button “Set Font Family” click event to execute the “setFontFamily()” function that sets the font-family property of multiple elements.

html> head> style> div padding: 10px; margin: 5px 0px; background-color: aliceblue; > /style> /head> body> h3> Set the font family for text using i> style.fontFamily property /i> with JavaScript /h3> button onclick="setFontFamily()"> Set Font Family /button> br />br /> div id="text1"> This text will use Arial font family /div> div id="text2"> This text will use cursive font family /div> div id="text3"> This text will use Helvetica, sans-serif font family /div> div id="text4"> This text will use Georgia, serif font family /div> script> // 'Set Font Family' button click event handler function function setFontFamily() // all text elements const text1 = document.getElementById('text1') const text2 = document.getElementById('text2') const text3 = document.getElementById('text3') const text4 = document.getElementById('text4') // Set the font-family property using the style.fontFamily property text1.style.fontFamily = 'Arial' text2.style.fontFamily = 'cursive' text3.style.fontFamily = 'Helvetica, sans-serif' text4.style.fontFamily = 'Georgia, serif' > /script> /body> /html>

Using the style.setProperty Method

In JavaScript, any new or existing property of an HTML element can be set using the style.setProperty method. The style.setProperty method is a part of the element object, so to use this method, we need to access the element object using the document.getElementById() method and then we can use this method. In the property name parameter of the style.setProperty method, it should be ‘font-family’, and the value and priority will be as per the user.

Syntax

const element = document.getElementById('id') element.style.setProperty(property_name, value, priority)

In the above syntax, we access the element object of an HTML element that has an id of ‘id’ using the document.getElementById() method and then use the style.setProperty method.

Parameters

  • property_name − The name of the property to be set.
  • value − The new value of the property.
  • priority − The priority of the property value (Optional).

Example

In the below example, we have used the style.setProperty method to set the font family for text with JavaScript. We have used an input field to take the user’s input for the list of font families. A button, “Set Font Family” is associated with a click event that executes the “setFontFamily()” function, which sets the font family of an element.

html> head> style> div padding: 10px; margin: 5px 0px; background-color: aliceblue; > /style> /head> body> h2> Set the font family for text using i> style.setProperty method /i> with JavaScript /h2> h4>Enter the list of font-families to set:/h4> input id="fonts" type="text" name="fonts" value="sans-serif"> button onclick="setFontFamily()"> Set Font Family /button> br />br /> div id="text"> This text will change it's font family according to the user's input! /div> script> // 'Set Font Family' button click event handler function function setFontFamily() const text = document.getElementById('text') // input field's value const fonts = document.getElementById('fonts').value // Set the font-family property using the style.setProperty method text.style.setProperty('font-family', fonts) > /script> /body> /html>

In this tutorial, we learned how to set the font family for text with JavaScript. We have seen two ways to set the font family using the style.fontFamily property and using the style.setProperty method. Users can follow these approaches to set the font family for text.

Источник

fontStyle style property

Syntax:

Possible values:

Takes the value of this property from the computed style of the parent element.
Font should be displayed in italic.
Default. Font is normal.
Font should be displayed in oblique.

Example HTML code 1:

head> style> .normal  font-style: normal; > .italic  font-style: italic; > style> head> body> span class="normal">font-style: normalspan>br /> span class="italic">font-style: italicspan> body>

Example HTML code 2:

head> script type="text/javascript"> function ChangeFont (selectTag)  // Returns the index of the selected option var whichSelected = selectTag.selectedIndex; // Returns the selected options values var selectState = selectTag.options[whichSelected].text; var fontTest = document.getElementById ("fontTest"); fontTest.style.fontStyle = selectState; > script> head> body> span id="fontTest"> Sample multiline text br />change it! span> br /> select onchange="ChangeFont (this);" size="3"> option />italic option selected="selected" />normal option />oblique select> body>

Supported by objects:

Источник

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