Home>Article>Web Front-end> How to change font size in javascript

How to change font size in javascript

青灯夜游
青灯夜游 Original
2021-06-18 14:46:13 15513browse

In JavaScript, you can use the fontSize property of the Style object to change the font size. This property can set the font size of the text. The syntax format is "element object.style.fontSize="value"".

How to change font size in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, you can use the fontSize property of the Style object to change the font size.

Let’s take an example to see how the fontSize attribute changes the font size.

First of all, let’s look at the layout, write a simple sentence and two buttons:

  

这是一些文字这是一些文字这是一些文字这是一些文字

The rendering is as follows:

Then , you need to control the size of the text by clicking the plus and minus buttons. The js code is as follows:

Click the " " button once, and the text will increase by one pixel. If you keep clicking, it will keep getting bigger:

Click the "-" button once, and the text will be reduced by one pixel. If you keep clicking, it will continue to become smaller:

It’s that simple. Be sure to note that when defining the initial variable, it must be a global variable. If you need to operate the button to make the text change, then the increase and decrease of the variable must be a local variable.

[Related recommendations:javascript learning tutorial]

Description: HTML DOM Style fontSize attribute

## The #fontSize property sets or returns the font size of text.

Syntax:

  • Return the fontSize property:

  • object.style.fontSize
  • Set the fontSize property:

  • object.style.fontSize = "value"
Attribute value:

##Value ##xx-small smaller Reduce the font size by one relative unit larger Reduce the font size Increase the size by a relative unit length Defines the font size in length units % Will font-size is set to % of the font size of the parent element For more programming-related knowledge, please visit: Programming Video
Description
    x-small
  • small
  • medium
  • large
  • x-large
  • xx-large
  • Set the font size to different fixed sizes, from xx-small to xx-large
! !

The above is the detailed content of How to change font size in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn