Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Centering Background Image In Css
Amezis
post Jan 21 2006, 09:14 PM
Post #1


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



Well, the title says what I need. Basically, I want a background image that can be used in the body or inside a div (or table), which is centered.
Go to the top of the page
 
+Quote Post
moldboy
post Jan 21 2006, 11:14 PM
Post #2


Privileged Member
*********

Group: Members
Posts: 516
Joined: 29-April 05
From: Canada Eh?!?
Member No.: 6,408



I believe
CODE
background-position: center center;
shoulde do it.
Go to the top of the page
 
+Quote Post
jlhaslip
post Jan 21 2006, 11:25 PM
Post #3


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,083
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



To insert a centred background Image, use this in your body definition inside a linked or @import css file:

HTML
body { background-image: url(named_image.gif/jpg/png); background-position: center;}


To include the same information inside the Html file instead of the css file, use:

HTML
<body style="background-image: url(named_image.gif/jpg/png); background-position: center;">


At least that's what the book tells me to do.
I believe IE has a few quirks with respect to background Images in anything besides the body, so if you or your clients are using IE to view background Images, there may be some problems doing so.

The above html will centre the image on the 'page'. If the page is longer than the viewport, it may not display properly, if you want to center it at the top of the page and keep it 'fixed' to the top of the page even when you scroll, add the top and fixed properties as below:

HTML
body
{
background-image: url('named_image.gif/jpg/png');
background-repeat: no-repeat; background-position: center top fixed;
}


For reference, use this link: http://www.w3schools.com/css/css_background.asp
Go to the top of the page
 
+Quote Post
Tyssen
post Jan 22 2006, 08:16 AM
Post #4



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



You can simplify it further by doing this:

HTML
body
{
background: url('named_image.gif/jpg/png') no-repeat center top fixed;
}

Keep in mind that for IE, only the body can accept 'fixed' properties. Also, if you wanted to a background image centred horizontally and vertically, it would be background-position: center center. Or it could be background-position: 50% 50%. Or if you knew the exact size of the container it was going into background-position: XXpx XXpx.
Go to the top of the page
 
+Quote Post
Lozbo
post Jan 24 2006, 07:20 PM
Post #5


Super Member
*********

Group: Members
Posts: 282
Joined: 1-September 05
From: Wanatos
Member No.: 11,382



Alright Tyssen! You are indeed a CSS guro hehe, I didnt know you could set % or pixels when positioning a background, thanx!

I would also recommend Amezis to use a background color, so that that while the page loads the image, it still shows the main color of the image you are using. Depending on the image, this might not always be a good idea but if you make a coupple of tests you would see what i mean...

Good luck!
Go to the top of the page
 
+Quote Post
moldboy
post Jan 24 2006, 07:23 PM
Post #6


Privileged Member
*********

Group: Members
Posts: 516
Joined: 29-April 05
From: Canada Eh?!?
Member No.: 6,408



Good point, if your image is a stary night (aka black) and you are writing in yellow, before the image loads the yellow text will be hard to read.
Go to the top of the page
 
+Quote Post
Tyssen
post Jan 24 2006, 10:31 PM
Post #7



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



QUOTE(Lozbo @ Jan 25 2006, 05:20 AM)
I didnt know you could set % or pixels when positioning a background

You can also mix them up - you can have pixels for one value and % for another (although I don't think it works as well if you're using words and values together, e.g. center 50%).
Go to the top of the page
 
+Quote Post
Lozbo
post Jan 25 2006, 01:34 AM
Post #8


Super Member
*********

Group: Members
Posts: 282
Joined: 1-September 05
From: Wanatos
Member No.: 11,382



QUOTE(moldboy @ Jan 24 2006, 01:23 PM)
Good point, if your image is a stary night (aka black) and you are writing in yellow, before the image loads the yellow text will be hard to read.
*


Yes moldboy, that was exactly what I mean, whatever you have in front of it, will become visible before the image loads, but as i said, this might not be always the best idea, like for example, when you are using the background as a trick to fix a problem with some layouts with floated columns, in order to get visually different background colours between the menu and the main content (with a menu floated to the right, for example) you need to put an image to repeat vertically (the image could be 1px height, and the width should be exactly the same width as the container, lets say 700px, so we will have 500 px red and 200 px blue, so the menu will look like it has a blue bg-color and the content a red one.

I hope you understand me...
Go to the top of the page
 
+Quote Post
Amezis
post Jan 26 2006, 06:18 PM
Post #9


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



Thanks alot biggrin.gif

By the way, I also have a background color. But thanks for the code, especially to you Tyssen (the CSS guru tongue.gif )