Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
Primer CSS Colors Background Color
Next article icon

Set Background Color using CSS

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Setting the background color in CSS involves using the background-color property to define the color displayed behind the content within an HTML element. This can be achieved through three primary methods: inline CSS, internal CSS, and external CSS. Each method offers different advantages depending on the use case.

The background color can be changed in three ways:

Table of Content

  • Setting the background color using Inline CSS
  • Setting the background color using Internal CSS
  • Setting the background color using External CSS

1. Setting the background color using Inline CSS

Inline CSS involves adding the style attribute directly within the HTML element's opening tag. This method is straightforward for quick, specific changes but can make your HTML code harder to manage if overused.

Setting the background color using Inline CSS Syntax

<tag style = " "></tag>

Setting the background color using Inline CSS Example

Below is an example that illustrates the use of inline CSS.

HTML
<!DOCTYPE html> <html>  <!--This line changes the color of background-->  <body style="background-color:pink">     <h1 style="color:green;text-align:center;">         GeeksForGeeks     </h1>      <h3 style="text-align:center;">         How to change color of Background?     </h3> </body>  </html> 

Output:

Setting the background color using Inline CSS

Explanation:

  • In this example by using Inline CSS sets the background color of the <body> element to pink using the style attribute.
  • Inline CSS also styles headings, making them green and center-aligned.
  • Renders headings with "GeeksForGeeks" and "How to change color of Background?" in the specified styles.

2. Setting the background color using Internal CSS

Internal CSS involves defining a <style> block within the HTML document's <head> section. This method keeps styles centralized within the document, making it easier to manage than inline styles for larger projects.

Setting the background color using Internal CSS Syntax:

<style>     /* Internal CSS starts here */ </style> 

Setting the background color using Internal CSS Example:

Below is the example that illustrates the use of internal CSS.

HTML
<!DOCTYPE html> <html>  <head>     <style>         body {             background-color: powderblue;         }          h1 {             color: green;             text-align: center;         }          h3 {             text-align: center;         }     </style> </head>  <body>     <h1>GeeksForGeeks</h1>      <h3>         How to change color of         Background?(Using Internal CSS)     </h3> </body>  </html> 

Output: This will be displayed when html file is opened in browser

Explanation:

  • In the above example Internal CSS sets the background to powder blue for the entire page.
  • <h1> text is green and center-aligned; <h3> is center-aligned.
  • Output displays "GeeksForGeeks" in <h1> and "How to change color of Background? (Using Internal CSS)" in <h3>.

3. Setting the background color using External CSS

External CSS involves defining styles in a separate CSS file and linking it to the HTML document using the <link> tag. This method is highly effective for large projects, as it separates style from content and allows for easy reuse across multiple pages.

Setting the background color using External CSS Syntax

body {     background-color: rgb(219, 176, 230); } h1 {     color: green;     text-align: center; } h3 {     text-align: center; } 

Setting the background color using External CSS Example:

Here is the basic implementation of Setting the background color using External CSS

HTML
<!DOCTYPE html> <html>  <head>     <link rel="stylesheet" type="text/css" href="style.css"> </head>  <body>     <h1>GeeksForGeeks</h1>     <h3>Changing Background Color (External CSS)</h3> </body>  </html> 
CSS
body {     background-color: rgb(219, 176, 230); }  h1 {     color: green;     text-align: center; }  h3 {     text-align: center; } 

Output:

chrome-capture-2024-1-26
Setting the background color External CSS

Explanation:

In this example we links an external stylesheet (styles.css) using the <link> tag in the <head> section.

  • CSS in styles.css sets the background to rgb(219, 176, 230); for the entire page.
  • CSS defines styles for <h1> (green color, center-aligned) and <h3> (center-aligned).
  • Renders "GeeksForGeeks" in <h1> and "Changing Background Color (External CSS)" in <h3> with specified styles.

Understanding how to set background colors in CSS using inline, internal, and external methods is crucial for effective web design. Inline CSS is useful for quick changes, internal CSS for centralized control within a single document, and external CSS for maintaining consistency and reusability across multiple documents.


Next Article
Primer CSS Colors Background Color
author
gauravl3hardwaj
Improve
Article Tags :
  • Web Technologies
  • HTML
  • CSS
  • HTML-Tags
  • CSS-Properties
  • HTML-Attributes
  • HTML-Questions
  • CSS-Questions

Similar Reads

  • Tailwind CSS Background Color
    This class accepts more than one value in tailwind CSS in which all the properties are covered in class form. It is the alternative to the CSS background-color property. This class is used to specify the background color of an element. The background covers the total size of the element with padding
    2 min read
  • CSS background-color Property
    The background-color CSS property sets the background color of an element, allowing you to create a solid color backdrop. You can define the color using named colors, hexadecimal values, RGB, RGBA, HSL, or HSLA. This property applies to both the content and padding areas of the element. By using the
    4 min read
  • Is background-color:none valid CSS ?
    CSS "background-color:none" is valid. But it is better to specify it as "transparent" instead of "none". The CSS background-color property is used to specify the background color of an element. The background covers the total size of the element with padding and border but excluding margin. It makes
    1 min read
  • Spectre Background Colors
    Spectre colors are one of the most useful utilities, there are two types of things where we can use colors. Like we can set colors for the text and we can set colors for the background. In this article, we will learn about the various classes available for the background, along with understanding ho
    2 min read
  • Primer CSS Colors Background Color
    Primer CSS is a free open-source CSS framework that is built upon GitHub design system to provide support to the broad spectrum of GitHub websites. It creates the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady
    4 min read
  • HTML Background Color
    HTML Background Color enables the inclusion of the background color on the web page, which can be accomplished with the help of the background-color property used with HTML elements. It applies to the total size of the element specified, but the margin is not included. The property has the default v
    4 min read
  • How to Set Transparent Background Color in CSS ?
    Creating a simple transparent background involves styling HTML elements using CSS to achieve the desired visual effect. We can use different approaches to achieve this effect including, RGBA color values and the Opacity property. Below are the approaches to set transparent background color in CSS: T
    2 min read
  • How to Change the Background Color of Table using CSS?
    Changing the background color of a table using CSS can help improve the visual appearance of a website or web application. we will learn how to change the background color of the table using CSS with different approaches. These are the following approaches: Table of Content Using Inline CSSUsing Int
    2 min read
  • Change Background color using onmouseover property
    In this article, we are going to make a background color changer using the onmouseover property. we will make different buttons for the color and on hovering the button the color of the background will change according to it.  We have used basic HTML and CSS for styling.  Syntax: document.bgColor =
    2 min read
  • How to set background color for an elements using jQuery ?
    In this article, we will set the background color for an element using jQuery. To add the background color, we use css() method. The css() method in JQuery is used to change style property of the selected element. The css() in JQuery can be used in different ways. The css() method can be used to che
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences