Home >Web Front-end >CSS Tutorial >How Can I Change a Web Page's Background Color with JavaScript?
Modifying a Web Page's Background Color with JavaScript
Enhancing the visual appeal of web pages involves manipulating elements, including the background color. JavaScript provides a straightforward approach to achieve this.
To alter the background color of a webpage using JavaScript, the document.body.style.background property comes into play. This property allows you to specify a color value for the entire page's background.
Example Implementation:
function changeBackground(color) { document.body.style.background = color; } window.addEventListener("load",function() { changeBackground('red') });
By incorporating this code into your project, you can dynamically change the background color to any desired value upon page load.
Customization Options:
Depending on the structure of your web page, it may be necessary to adjust the implementation. If a DIV container holds your content with a custom background color, you will need to modify the background color of the DIV itself instead of the document body.
The above is the detailed content of How Can I Change a Web Page's Background Color with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!