Home >Web Front-end >CSS Tutorial >How Can I Change a Webpage's Background Color Using JavaScript?

How Can I Change a Webpage's Background Color Using JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2025-01-04 22:19:39696browse

How Can I Change a Webpage's Background Color Using JavaScript?

Modifying Webpage Background Color with JavaScript

This article addresses the query of how to alter the background color of a webpage using JavaScript. Here's a solution:

Solution:

Utilize the JavaScript property document.body.style.background to change the background color.

Example Implementation:

function changeBackground(color) {
    document.body.style.background = color;
}

window.addEventListener("load", function() {
    changeBackground('red');
});

Considerations:

Keep in mind that the effectiveness of this method depends on the layout of your web page. If you've employed a DIV container with a distinct background color, you'll need to adjust the background color of that container, rather than the document body.

The above is the detailed content of How Can I Change a Webpage's Background Color Using 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