CSS Background Without Causing Scrollbars

Quick note in case I spend as long as I did again.

A Client had some clouds on the background of his photoshop file for his website. They were to be positioned in the top right corner with the leftmost tip aligned to the right of a central content section. I separated off the clouds as another div layer, specified the width and height and specified it's left. It caused the horizontal scrollbar which is a bit annoying because you scroll to the right and theres nothing but some background image. Also the footer was at 100% so it got cut off if you scrolled horizontally.

Aim / Objective
To have a background of sky and grass with a centered content section (white semi-transparent layer) of 900 pixels width as shown in the following picture:
background of sky and grass with a centered content section (no clouds)

And I want the clouds as another layer ontop aligned to the top right:
background of sky and grass with a centered content section (with clouds)

Tried and failed:
  • Specifying the width of the div layer the same as the image and positioning the div. This causes a scrollbar at the bottom.
  • Tried different techniques of CSS overflow auto hidden etc. all to no avail.
What I didn't want when I resize the web browser, a scrollbar!
narrowed page with scrollbar showing to scroll right
What worked for me:
  • Specify the DIV layer width to be 100 percent of the page.
  • Position the module with absolute on your webpage with background (where left is 0 [zero] because your width is 100%).
  • Position the background image bearing in mind that your layer is 100% of the viewable browser page.

.templateclouds {
	position: absolute;
	background: transparent url("../images/soyatemplate_bg_cloudsblurred.png") no-repeat 975px 30px;
	width: 100%;
	z-index: 10;

/* where background breakdown is:
background-image: url("../images/soyatemplate_bg_cloudsblurred.png");
background-repeat-x: no-repeat;
background-repeat-y: no-repeat;
background-position-x: 975px;
background-position-y: 30px;
background-color: transparent;
  1.  .templateclouds { 
  2.      position: absolute; 
  3.      top:60px
  4.      left:0
  5.      background: transparent url("../images/soyatemplate_bg_cloudsblurred.png") no-repeat 975px 30px
  6.      width: 100%; 
  7.      height:400px
  8.      z-index: 10
  9.  } 
  11.  /* where background breakdown is: 
  12.  background-image: url("../images/soyatemplate_bg_cloudsblurred.png")
  13.  background-repeat-x: no-repeat; 
  14.  background-repeat-y: no-repeat; 
  15.  background-position-x: 975px; 
  16.  background-position-y: 30px; 
  17.  background-color: transparent; 
  18.  */ 
For the semi-transparent background (content section) I used the following CSS:
#contentbox {
	position: relative;
	top: 70px;
	width: 900px; 
	margin: 0 auto;
	padding: 7px 30px 75px 30px;
	clear: both;
	z-index: 7;
	/* Fallback for web browsers that doesn't support RGBa */
	background: rgb(255, 255, 255);
	/* RGBa with 0.6 opacity */
	background: rgba(255, 255, 255, 0.6);
	/* For IE 5.5 - 7*/
	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff000000, endColorstr=#ff000000);
	/* For IE 8*/
	-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff000000, endColorstr=#ff000000)";
	-moz-border-radius-topright: 20px;
	border-top-right-radius: 20px;
	-moz-border-radius-topleft: 20px;
	border-top-left-radius: 20px;
  1.  #contentbox { 
  2.      position: relative; 
  3.      top: 70px
  4.      width: 900px
  5.      margin: 0 auto; 
  6.      padding: 7px 30px 75px 30px
  7.      clear: both; 
  8.      z-index: 7
  9.      /* Fallback for web browsers that doesn't support RGBa */ 
  10.      background: rgb(255, 255, 255)
  11.      /* RGBa with 0.6 opacity */ 
  12.      background: rgba(255, 255, 255, 0.6)
  13.      /* For IE 5.5 - 7*/ 
  14.      filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff000000, endColorstr=#ff000000)
  15.      /* For IE 8*/ 
  16.      -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff000000, endColorstr=#ff000000)"; 
  17.      -moz-border-radius-topright: 20px
  18.      border-top-right-radius: 20px
  19.      -moz-border-radius-topleft: 20px
  20.      border-top-left-radius: 20px
  21.  } 
Category: Cascading Stylesheets :: Article: 347

Credit where Credit is Due:

Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman

Related Articles

Joes Revolver Map


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 bc1qjtp4l4ra452wzvuk9a45yfj82zkahsyy2z379y
© 2022 Joel Lipman .com. All Rights Reserved.