How To Change The Background Color Of A Web Page Using ...
Maybe your like
Topic: JavaScript / jQueryPrev|Next
Answer: Use the JavaScript style property
You can easily change the background color of a webpage i.e. the <body> element or any other element dynamically by using its style property in JavaScript.
The style property is used to get as well as set the inline style of an element. Typically, any HTML element that supports the style attribute also has a style property. Here's an example:
Example
Try this code » <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Change the Background Color with JavaScript</title> <script> // Function to change webpage background color function changeBodyBg(color){ document.body.style.background = color; } // Function to change heading background color function changeHeadingBg(color){ document.getElementById("heading").style.background = color; } </script> </head> <body> <h1 id="heading">This is a heading</h1> <p>This is a paragraph of text.</p> <hr> <div> <label>Change Webpage Background To:</label> <button type="button" onclick="changeBodyBg('yellow');">Yellow</button> <button type="button" onclick="changeBodyBg('lime');">Lime</button> <button type="button" onclick="changeBodyBg('orange');">Orange</button> </div> <br> <div> <label>Change Heading Background To:</label> <button type="button" onclick="changeHeadingBg('red');">Red</button> <button type="button" onclick="changeHeadingBg('green');">Green</button> <button type="button" onclick="changeHeadingBg('blue');">Blue</button> </div> </body> </html>Related FAQ
Here are some more FAQ related to this topic:
- How to get element by class name in JavaScript
- How to add CSS properties to an element dynamically using jQuery
- How to add attribute to an HTML element in jQuery
Advertisements Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates.
About Us
Our Story Terms of Use Privacy PolicyContact
Contact Us Report Error AdvertiseInteractive Tools
Bootstrap Icon Search Utility HTML Formatter Title & Meta Length Calculator HTML Color Picker Bootstrap Button Generator SQL Playground Font Awesome Icon Finder HTML Editor
Copyright © 2025 Tutorial Republic. All Rights Reserved. Share This:
Tag » Colors Html5 Change
-
How To Set Background Color In HTML? - Tutorialspoint
-
HTML 5 Tutorial: Set Or Change HTML Background Color
-
How To Add & Change Background Color In HTML - HubSpot Blog
-
- HTML (HyperText Markup Language) | MDN"> - HTML (HyperText Markup Language) | MDN
-
In HTML, How Do I Change The Color Of The Text And Background Of A ...
-
How To Change The HTML Font Color - Kinsta
-
How To Change Text Color In Html - Javatpoint
-
HTML Colors - W3Schools
-
HTML Font Style – How To Change Text Color And Size With An ...
-
How To Change The Font Type, Size, And Color On A Web Page
-
How To Change Colors In HTML - Stack Overflow
-
Change Text Color In HTML | HTML5 Tutorial - YouTube
-
HTML Background Color Tutorial – How To Change A Div ...
-
HTML Text Color - HTML Color Codes