FREE HTML CODE
Search HTML Code

HTML Fixed Background

This article provides HTML fixed background code - code for fixing the background image of an HTML element so that it doesn't scroll when its containing block scrolls.

To fix the position of a background image, use the CSS background-attachment property.

Example code:


<div style="background-image:url(/pix/smile.gif);
  background-repeat:repeat;
  background-attachment:fixed;
  overflow:scroll;
  width:200px;
  height:100px;">

</div>


This results in:
HTML fixed background code is generated by applying the
CSS background-attachment property against an HTML element.
When the box scrolls, the background image stays fixed
in the same position.

Shorthand Code

You can use the CSS background tag to set all the background properties at once. Therefore, using the previous code example, we could rewrite it to this:



<div style="background:url(/pix/smile.gif) repeat fixed;
  overflow:scroll;
  width:200px;
  height:100px;">
HTML fixed background code is generated by applying the
CSS background-attachment property against an HTML element.
When the box scrolls, the background image stays fixed
in the same position.
</div>



HTML fixed background code is generated by applying the
CSS background-attachment property against an HTML element.
When the box scrolls, the background image stays fixed
in the same position.
| More