How to Set Background Color in HTML

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

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 wikiHow teaches you how to change the background color of a web page by editing its HTML and CSS.

[Edit]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.

[Edit]Steps

[Edit]Setting a Solid Background Color

  1. Find your document's "html" header. It should be near the top of the document.
    2609629 1 3.jpg
  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:
    2609629 2 3.jpg
      body {
          background-color: 
      }
      
    • In this context, only one spelling of "color" will work; you can't use "colour" here.
  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:
    2609629 3 3.jpg
      body {
          background-color: #d24dff;
      }
      
  4. Review your "style" information. At this point, your HTML document's header should resemble the following:
    2609629 4 3.jpg
      <!DOCTYPE html>
      <html>
      <head>
      <style>
      body {
      background-color: #d24dff
      }
      </style>
      </head>
      </html>
      
  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]
    2609629 5 3.jpg
      <!DOCTYPE html>
      <html>
      <head>
      <style>
      body {
          background-color: #93B874;
      }
      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>
      

[Edit]Creating a Gradient Background

  1. Find your document's "html" header. It should be near the top of the document.
    2609629 6 3.jpg
  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]
    2609629 7 3.jpg
    background: linear-gradient(direction/angle, color1, color2, color3, etc);
    
    }}
  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:
    2609629 8 4.jpg
      html {
          min-height: 100%; }
      body {
          background: -webkit-linear-gradient(#93B874, #C9DCB9); 
          background: -o-linear-gradient(#93B874, #C9DCB9); 
          background: -moz-linear-gradient(#93B874, #C9DCB9); 
          background: linear-gradient(#93B874, #C9DCB9); 
          background-color: #93B874; }
      
    • Different browsers have different implementations of the gradient function, so you'll have to include several versions of the code.
  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]
    2609629 9 3.jpg
      html {
          min-height: 100%;}
      
      body {
          background: -webkit-linear-gradient(left, #93B874, #C9DCB9); 
          background: -o-linear-gradient(right, #93B874, #C9DCB9); 
          background: -moz-linear-gradient(right, #93B874, #C9DCB9); 
          background: linear-gradient(to right, #93B874, #C9DCB9); 
          background-color: #93B874; }
      
    • You can play around with the "left" and "right" tags to experiment with different directions for your gradient.
  5. Use other properties to adjust the gradient. There's a lot more that you can do with gradients.
    2609629 10 3.jpg
    • 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:
  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 function to define the color. The ending value determines the transparency: for solid and for transparent.
    2609629 11 3.jpg
  7. Review your completed code. The code to create a color gradient as your website's background will look something like this:
    2609629 12 3.jpg
      <!DOCTYPE html>
      <html>
      <head>
      <style>
      html {
          min-height: 100%;
      }
       
      body {
          background: -webkit-linear-gradient(left, #93B874, #C9DCB9); 
          background: -o-linear-gradient(right, #93B874, #C9DCB9);
          background: -moz-linear-gradient(right, #93B874, #C9DCB9); 
          background: linear-gradient(to right, #93B874, #C9DCB9); 
          background-color: #93B874; 
      }
      </style>
      </head>
      <body>
      </body>
      </html>
      

[Edit]Creating a Changing Background

  1. Find your document's "html" header. It should be near the top of the document.
    2609629 13 3.jpg
  2. Add the animation property to the "body" element. Type the following into the space below the "body {" bracket and above the closing bracket:[4]
    2609629 14 3.jpg
    • The top line of text is for Chromium-based browsers while the bottom line of text is for other browsers.
  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:[5]
    2609629 15 3.jpg
      @-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 ( and 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 (, , etc) are of the total animation length (). When the page loads, the background will be the color set at (). Once the animation has played for 25% of of 60 seconds, the background will turn to , and so on.
    • You can modify the times and colors to fit your desired outcome.
  4. Review your code. Your entire code for the changing background color should resemble the following:
    2609629 16 3.jpg
      <!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>
      

[Edit]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.
  • You can also set a an image as your website's background with HTML.

[Edit]Warnings

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

[Edit]Related wikiHows


[Edit]References

Comments

Popular Posts