Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Seamless Background Pattern

This is a discussion on Seamless Background Pattern, within the CSS section. This forum and the thread "Seamless Background Pattern" are both part of the Designing Your Website category.

 
Reply to this topicStart new topic
> Seamless Background Pattern
holger
post May 27 2008, 02:23 PM
Post #1


Fresh Squeezed
**

Group: Members
Posts: 30
Joined: 6-March 08
From: Birmingham
Member No.: 196



I'm currently redesigning my website and I came across the problem of creating a seamless background in the header. I solved it eventually. Just wanted to let you guys know the answer in case anybody else has this problem.

I have a header that has a repeating pattern as background. The header also contains an image that sits centrally aligned on the page. Sounds easy, so I just did the following
CODE
  
   <body>  
   <div id="header"><img src="header.png" alt="Header Image"/></div>  
   </body>

The CSS looks like this
CODE
  
   * {  
     margin: 0px;  
     padding: 0px;  
   }  
    
   #header {  
     width: 100%;  
     text-align: center;  
     background-image: url(pattern.png);  
   }


The header image continues the background pattern. The transition between the two should be seamless. But as you can see here it doesn't really work. When you change the width of the browser window the header image slides across the background.

After a bit of searching on the web I finally found this very simple solution. By simply setting the horizontal background position to center, one can align the background with the centralised header image.

CODE
  
   * {  
     margin: 0px;  
     padding: 0px;  
   }  
    
   #header {  
     width: 100%;  
     text-align: center;  
     background-image: url(pattern.png);  
     background-position: center 0;  
   }



The working example can be seen here.


--------------------
Viventic Web Design and Application Development
Go to the top of the page
 
+Quote Post
Linda
post May 27 2008, 03:07 PM
Post #2


Co-Founder
******

Group: Co-Founders
Posts: 2,476
Joined: 13-February 08
From: Pink House in USA
Member No.: 3



Very nice post holger! Thank you.. thank_you2.gif


--------------------
Go to the top of the page
 
+Quote Post
unitedcraig
post May 27 2008, 05:52 PM
Post #3


Rapid Squeezer
****

Group: Advisors
Posts: 434
Joined: 14-February 08
From: Stockport
Member No.: 75



Nice post mate wink.gif

Could this be made into a tip and trick or a sticky, just so it doesn't get lost in the forums


--------------------

Working progress
Go to the top of the page
 
+Quote Post
MikeHopley
post May 28 2008, 05:20 AM
Post #4


Squeeze Machine
*****

Group: Mentor
Posts: 505
Joined: 15-February 08
From: UK
Member No.: 143



Note that this is a special case of a more general method.

background-position: center 0; is the same as background-position: 50% 0;

So if you ever want a "proportionately off-centre" background, you can use something like background-position: 35% 0;

**EDIT**

Actually, it's just occurred to me that "50%" may not be quite the same as "center", because 50% should position the top-left corner of the image in the centre, which is not quite the same as centring the image.

This post has been edited by MikeHopley: May 28 2008, 05:25 AM
Go to the top of the page
 
+Quote Post
holger
post May 28 2008, 08:41 AM
Post #5


Fresh Squeezed
**

Group: Members
Posts: 30
Joined: 6-March 08
From: Birmingham
Member No.: 196



QUOTE (MikeHopley @ May 28 2008, 11:20 AM) *
Actually, it's just occurred to me that "50%" may not be quite the same as "center", because 50% should position the top-left corner of the image in the centre, which is not quite the same as centring the image.


No.
Your first statement was correct. Interestingly percentages in the background position do not refer to the top left corner of the background image. For example background-position: 25% 0; will not only place the image 25% from the left of the surrounding div but also use the point 25% from the left border of the image itself as reference point. I think the reason for this behavior is exactly so that you can write

background-position: 0% 0%;
to place the top-left of the image in the top-left corner
background-position: 50% 50%; to place the center of the image in the center
background-position: 100% 100%; to place the bottom-right of the image in the bottom-right corner


--------------------
Viventic Web Design and Application Development
Go to the top of the page
 
+Quote Post
MikeHopley
post May 28 2008, 10:38 AM
Post #6


Squeeze Machine
*****

Group: Mentor
Posts: 505
Joined: 15-February 08
From: UK
Member No.: 143



QUOTE (holger @ May 28 2008, 02:41 PM) *
No.
Your first statement was correct. Interestingly percentages in the background position do not refer to the top left corner of the background image. For example background-position: 25% 0; will not only place the image 25% from the left of the surrounding div but also use the point 25% from the left border of the image itself as reference point. I think the reason for this behavior is exactly so that you can write

background-position: 0% 0%;
to place the top-left of the image in the top-left corner
background-position: 50% 50%; to place the center of the image in the center
background-position: 100% 100%; to place the bottom-right of the image in the bottom-right corner


Ah, how delightful. smile.gif

It just shows how much thought goes into the specifications. That's actually what I've come to expect from experience, but I suddenly realised a potential flaw in the idea of "50% = center, while 0% = left". Unsurprisingly, the smart W3C people had already thought of that.

This post has been edited by MikeHopley: May 28 2008, 10:40 AM
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   16 unitedcraig 318 20th February 2008 - 07:34 AM
Last post by: unitedcraig
No New Posts   11 shammy2007 388 22nd February 2008 - 11:09 AM
Last post by: Stuart
No New Posts   13 Antti 204 18th February 2008 - 12:51 PM
Last post by: thesealportalteam
No New Posts   11 unitedcraig 242 20th February 2008 - 02:48 AM
Last post by: Monie
No New Posts   6 thesealportalteam 203 25th February 2008 - 02:30 PM
Last post by: thesealportalteam