Download Article
An easy-to-follow guide for coding with CSS and HTML to add the background colors of a page
Download Article

Did you want to change the background color of that page using HTML? Unfortunately, with HTML 5, this is no longer possible in just HTML coding. Instead, you'll need to use both HTML and CSS coding, which works even better. This How.com.vn teaches you how to change the background color of a web page by editing its HTML and CSS.

Things You Should Know

  • Although the attribute for HTML to manage background color is gone, you can still use HTML with CSS to change your background color easily.
  • You'll need a numeric code for the color you want if you want a specific color. If you don't need a specific color, you can use words like "orange" or "light blue."
  • When editing a web page with HTML and CSS, you can create a solid background, gradient, or changing background.
Method 1
Method 1 of 3:

Setting a Solid Background Color

Download Article
  1. How.com.vn English: Step 1 Find your document's "html" header.
    It should be near the top of the document.
  2. Step 2 Add the "background-color" property to the "body" element.
    Type background-color: between the body brackets. You should now have the following "body" element:
      body {    background-color: }
    • In this context, only one spelling of "color" will work; you can't use "colour" here.
    Advertisement
  3. Step 3 Add your desired background color to the "background-color" property.
    Type your selected color's numeric code followed by a semicolon next to the "background-color:" element to do so. For example, to set your page's background to pink, you would have the following:
      body {    background-color: #d24dff;}
  4. Step 4 Review your "style" information.
    At this point, your HTML document's header should resemble the following:
      <!DOCTYPE html><html><head><style>body {background-color: #d24dff}</style></head></html>
  5. Step 5 Use "background-color" to apply background colors to other elements.
    Just as you set it in the body element, you can use "background-color" to define the backgrounds of other elements such as headers, paragraphs, and so on. For example, to apply a background color to a main header (<h1>) or a paragraph (<p>), you would have something resembling the following code:[1]
      <!DOCTYPE html><html><head><style>body {    background-color: #216fdb;}h1 {    background-color: #00b33c;}p {    background-color: #FFFFFF);}</style></head><body><h1>Header with Green Background</h1><p>Paragraph with white background</p></body></html>
  6. Advertisement
Method 2
Method 2 of 3:

Creating a Gradient Background

Download Article
  1. How.com.vn English: Step 1 Find your document's "html" header.
    It should be near the top of the document.
  2. How.com.vn English: Step 2 Understand the basic syntax of this process.
    When making a gradient, there are two pieces of information you'll need: the starting point/angle, and the colors that the gradient will transition between. You can select multiple colors to have the gradient move between all of them, and you can set a direction or angle for the gradient.[2]
    background: linear-gradient(direction/angle, color1, color2, color3, etc);
    }}
  3. How.com.vn English: Step 3 Make a vertical gradient.
    If you don't specify the direction, the gradient will go from top to bottom. To create your gradient, add the following code between the <style></style> tags:
      html {    min-height: 100%; }body {    background: -webkit-linear-gradient(#216fdb, #C9DCB9);     background: -o-linear-gradient(#216fdb, #C9DCB9);     background: -moz-linear-gradient(#216fdb, #C9DCB9);     background: linear-gradient(#216fdb, #C9DCB9);     background-color: #216fdb; }
    • Different browsers have different implementations of the gradient function, so you'll have to include several versions of the code.
  4. How.com.vn English: Step 4 Make a directional gradient.
    If you'd rather create a gradient that isn't strictly vertical, you can add direction to the gradient in order to change the way the color shift appears. Do so by typing the following in between the <style></style> tags:[3]
      html {    min-height: 100%;}body {    background: -webkit-linear-gradient(left, #216fdb, #C9DCB9);     background: -o-linear-gradient(right, #216fdb, #C9DCB9);     background: -moz-linear-gradient(right, #216fdb, #C9DCB9);     background: linear-gradient(to right, #216fdb, #C9DCB9);     background-color: #216fdb; }
    • You can play around with the "left" and "right" tags to experiment with different directions for your gradient.
  5. How.com.vn English: Step 5 Use other properties to adjust the gradient.
    There's a lot more that you can do with gradients.
    • For example, not only can you add more than two colors, you can also put a percentage after each one. This will allow you to set how much spacing you want each color segment to have. Here's a sample gradient that uses this principle:
      background: linear-gradient(#93B874 10%, #C9DCB9 70%, #000000 90%);
  6. How.com.vn English: Step 6 Add transparency to your colors.
    This will make the color fade. Use the same color to fade from the color to nothing. You'll need to use the rgba() function to define the color. The ending value determines the transparency: 0 for solid and 1 for transparent.
      background: linear-gradient(to right, rgba(147,184,116,0), rgba(147,184,116,1));
  7. How.com.vn English: Step 7 Review your completed code.
    The code to create a color gradient as your website's background will look something like this:
      <!DOCTYPE html><html><head><style>html {    min-height: 100%;} body {    background: -webkit-linear-gradient(left, #216fdb, #C9DCB9);     background: -o-linear-gradient(right, #216fdb, #C9DCB9);    background: -moz-linear-gradient(right, #216fdb, #C9DCB9);     background: linear-gradient(to right, #216fdb, #C9DCB9);     background-color: #216fdb; }</style></head><body></body></html>
  8. Advertisement
Method 3
Method 3 of 3:

Creating a Changing Background

Download Article
  1. How.com.vn English: Step 1 Find your document's "html" header.
    It should be near the top of the document.
  2. Step 2 Add the animation property to the "body" element.
    Type the following into the space below the "body {" bracket and above the closing bracket:
          -webkit-animation: colorchange 60s infinite;     animation: colorchange 60s infinite;
    • The top line of text is for Chromium-based browsers while the bottom line of text is for other browsers.
  3. How.com.vn English: Step 3 Add your colors to the animation.
    Now you'll use the @keyframes rule to set the background colors through which you'll cycle, as well as the length of time each color will appear on the page. Again, you'll need separate entries for the different sets of browsers. Enter the following lines of code below the closed "body" bracket:[4]
      @-webkit-keyframes colorchange {     0%  {background: #33FFF3;}    25%  {background: #78281F;}    50%  {background: #117A65;}    75%  {background: #DC7633;}    100% {background: #9B59B6;}}@keyframes colorchange {     0%  {background: #33FFF3;}    25%  {background: #78281F;}    50%  {background: #117A65;}    75%  {background: #DC7633;}    100% {background: #9B59B6;}}
    • Note that the two rules (@-webkit-keyframes and @keyframes have the same background colors and percentages. You'll want these to stay uniform so the experience is the same on all browsers.
    • The percentages (0%, 25%, etc) are of the total animation length (60s). When the page loads, the background will be the color set at 0% (#33FFF3). Once the animation has played for 25% of of 60 seconds, the background will turn to #7821F, and so on.
    • You can modify the times and colors to fit your desired outcome.
  4. How.com.vn English: Step 4 Review your code.
    Your entire code for the changing background color should resemble the following:
      <!DOCTYPE html><html><head><style>body {    -webkit-animation: colorchange 60s infinite;     animation: colorchange 60s infinite;}@-webkit-keyframes colorchange {     0%  {background: #33FFF3;}    25%  {background: #78281F;}    50%  {background: #117A65;}    75%  {background: #DC7633;}    100% {background: #9B59B6;}}@keyframes colorchange {     0%  {background: #33FFF3;}    25%  {background: #78281F;}    50%  {background: #117A65;}    75%  {background: #DC7633;}    100% {background: #9B59B6;}}   </style></head><body></body></html>
  5. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How do I set a background color for a specific width?
    How.com.vn English: Community Answer
    Community Answer
    Use the background-size property inside of the "body" element. For example, "background-size: 300px 150px" makes the background 300 pixels wide and 150 pixels high.
  • Question
    It does not work. What can I do?
    How.com.vn English: UsernameHere11
    UsernameHere11
    Community Answer
    To make it black, try: body { background-color: #190707}
  • Question
    What is the correct HTML for adding a background color?
    How.com.vn English: Community Answer
    Community Answer
    My text goes here!Replace the html code above with your text and selected your preferred color.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • Use online HTML pickers if you want a very specific color for your background. If, for example, you want the background to be the same color as your walls, you can match HTML colors with paint splotches at certain sites.
      • If you want to use basic colors within your HTML code, you can type the colors' names without the pound sign (#) instead of using an HTML color code. For example: to create an orange background, you would type in background-color: orange; here.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

      • Make sure you test any changes to your website before publishing them online.
      Advertisement

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about dealing with html, check out our in-depth interview with Jessica Andzouana.

      About This Article

      How.com.vn English: Jessica Andzouana
      Written by:
      Software Engineer
      This article was written by Jessica Andzouana and by How.com.vn staff writer, Darlene Antonelli, MA. Jessica Andzouana is a Software Engineer based in the San Francisco Bay Area. With over five years of professional experience in front-end development, digital art, and design, she is passionate about emerging technologies such as blockchain and AI. Her background as both a programmer and artist, paired with a highly design-conscious mindset, provides her a fresh perspective and unique skill set to produce creative solutions in her field. She works at Alcacruz as a Software Engineer, and received a dual BS/BA degree from Santa Clara in Computer Science and Studio Art. This article has been viewed 1,515,953 times.
      How helpful is this?
      Co-authors: 36
      Updated: February 2, 2024
      Views: 1,515,953
      Categories: Featured Articles | HTML
      Thanks to all authors for creating a page that has been read 1,515,953 times.

      Reader Success Stories

      • How.com.vn English: Gab G.

        Gab G.

        Aug 30, 2017

        "Thanks, this is exactly what I was looking for, very clear. I can copy the code as well and it took me like 20..." more
      Share your story

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement