This is a Premium Blog

HTML and CSS Background

Written on July 27, 2007 by admin

In this tutorials, you will learn how to set the background color with CSS.

How to set background color

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.  
  5. <title>this is the title of the web page</title>
  6.  
  7. <style type="text/css">
  8. body {background-color: yellow;}
  9. h1 {background-color: #00ff00;}
  10. h2 {background-color: red;}
  11. p {background-color: rgb(250,0,255);}
  12. </style>
  13.  
  14. </head>
  15. This is a level 1 header
  16. </h1>
  17. This is a level 2 header
  18. </h2>
  19. This is a paragraph
  20. </p>
  21. </body>
  22. </html>

How to set an image as the background

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.  
  5. <title>this is the title of the web page</title>
  6.  
  7. <style type="text/css">
  8.  
  9. body
  10. {
  11. background-image:
  12. url('csshtmltutorial-image.jpg')
  13. }
  14.  
  15. </style>
  16.  
  17. </head>
  18. </body>
  19. </html>

How to repeat a background image

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.  
  5. <title>this is the title of the web page</title>
  6.  
  7. <style type="text/css">
  8.  
  9. body
  10. {
  11. background-image:
  12. url('csshtmltutorial.jpg');
  13. background-repeat: repeat;
  14. }
  15.  
  16. </style>
  17.  
  18. </head>
  19. </body>
  20. </html>

How to repeat an image vertically only

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.  
  5. <title>this is the title of the web page</title>
  6.  
  7. <style type="text/css">
  8.  
  9. body
  10. {
  11. background-image:
  12. url('csshtmltutorial.jpg');
  13. background-repeat: repeat-y;
  14. }
  15.  
  16. </style>
  17.  
  18. </head>
  19. </body>
  20. </html>

How to repeat a background image horizontally only

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.  
  5. <title>this is the title of the web page</title>
  6.  
  7. <style type="text/css">
  8.  
  9. body
  10. {
  11. background-image:
  12. url('csshtmltutorial.jpg');
  13. background-repeat: repeat-x;
  14. }
  15.  
  16. </style>
  17.  
  18. </head>
  19. </body>
  20. </html>

How to repeat one background image at a time

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.  
  5. <title>this is the title of the web page</title>
  6.  
  7. <style type="text/css">
  8.  
  9. body
  10. {
  11. background-image:
  12. url('csshtmltutorial.jpg');
  13. background-repeat: no-repeat;
  14. }
  15.  
  16. </style>
  17.  
  18. </head>
  19. </body>
  20. </html>

If you enjoyed this post Subscribe to our feed

One Comment on “HTML and CSS Background”

  1. cyberst0rm |

    Thanks. Been trying to find how to do this for a while now. Helpful.

    cyberst0rm’s tech blog
    http://cyberst0rm.blogspot.com

Leave a Reply