Home > Web Front-end > Front-end Q&A > How to set the background color of a web page in JavaScript

How to set the background color of a web page in JavaScript

PHPz
Release: 2023-04-19 14:33:41
Original
2510 people have browsed it

JavaScript is a scripting language that can be used for website development, including web page design and dynamic interaction. In JavaScript, you can control the appearance and behavior of web pages by manipulating HTML and CSS properties. This article will introduce how to set the background color of web pages in JavaScript.

Setting the background color of a web page in JavaScript can be achieved by accessing the properties of the document object. The Document object is the root node of the HTML page and can be accessed through the document attribute. The following are some of the properties related to document color:

document.bgColor: Sets the background color of the element in the HTML document.
document.fgColor: Set text color, all sub-content can be accessed further from the page.
document.linkColor: Set the link color for all unvisited elements.
document.VisitedLinkColor: Set the visited link color of the
element.
document.activeLinkColor: Set the active link color of the
element.

When operating these properties, you need to use JavaScript statements. Here are a few examples:

document.bgColor = "#FFFFFF"; //Set the background to white
document.fgColor = "#000000"; //Set the text color to black
document .linkColor = "#0000FF"; //Set the link color to blue
document.visitedLinkColor = "#800080"; //Set the visited link color to purple
document.activeLinkColor = "#FF0000"; / /Set the active link color to red

You can place the above statement between <script> and </script> within the <script> tag, or save it to a separate JavaScript file , and then include it in the HTML document. In a JavaScript file, you can include a script using the following statement: </p> <p><script type="text/javascript" src="myScript.js"></script>

Here, "myScript.js" is the name of your JavaScript file. When a browser parses an HTML document, it loads JavaScript files when needed.

In addition, you can also use CSS style sheets to set the background color of the web page. CSS is a markup language specialized for style sheets that can be used with HTML documents to control the layout and appearance of text, images, and other elements. The following is an example of a CSS style sheet:

body {
background-color: #FFFFFF; //Set the background to white
}

Save the above style sheet to a CSS file, and then include it in the tag of the HTML document:

When using CSS style sheets, you can combine more styles into one sheet, such as setting font color, font size, paragraph margins, title styles, etc. This approach can greatly simplify the code and also provide more flexible control.

In this article, we introduced how to set the background color of a web page in JavaScript. No matter which method you use, when designing your website, make sure the background coordinates with the color and style of text, images, and other elements to both engage users and provide them with an enjoyable browsing experience.

The above is the detailed content of How to set the background color of a web page in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template