Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Centering Background Image In Css


11 replies to this topic

#1 Amezis

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Location:Oslo, Norway

Posted 21 January 2006 - 09:14 PM

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.

#2 moldboy

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 516 posts
  • Location:Canada Eh?!?

Posted 21 January 2006 - 11:14 PM

I believe
background-position: center center;
shoulde do it.

#3 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 21 January 2006 - 11:25 PM

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

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:

<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:

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..._background.asp

#4 Tyssen

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,161 posts
  • Location:Brisbane, QLD

Posted 22 January 2006 - 08:16 AM

You can simplify it further by doing this:

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.

#5 Lozbo

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 282 posts
  • Location:Wanatos
  • Interests:Multimedia Arts and Technologies =)

Posted 24 January 2006 - 07:20 PM

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!

#6 moldboy

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 516 posts
  • Location:Canada Eh?!?

Posted 24 January 2006 - 07: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.

#7 Tyssen

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,161 posts
  • Location:Brisbane, QLD

Posted 24 January 2006 - 10:31 PM

Lozbo, on Jan 25 2006, 05:20 AM, said:

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%).

#8 Lozbo

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 282 posts
  • Location:Wanatos
  • Interests:Multimedia Arts and Technologies =)

Posted 25 January 2006 - 01:34 AM

moldboy, on Jan 24 2006, 01:23 PM, said:

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.

View Post

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...

#9 Amezis

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Location:Oslo, Norway

Posted 26 January 2006 - 06:18 PM

Thanks alot :(

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

#10 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 02 March 2010 - 05:48 PM

Is there a CSS script for changing backgrounds on webpages (i.e. slideshows)?Centering Background Image In Css

 I have a homepage 'profile page' on Singsnap.Com, a karaoke community site. One can load on the profile comment area HTML webcode to design and customize each user's page to their liking. Due to problems with malicious and resource-intensive javascript code on people's pages, they have disabled the use of javascript, but will still allow the use of CSS and DHTML. I am a digital artist who wants to use my art images as backgrounds on my webpage, as a full-page background slideshow beneath all other web content, images cycling through my art images. Is this possible to do in CSS or DHTML, or can javascript code be embedded in CSS so the process can be enabled yet not disabled by the server as javascript? I thank you for your looking into this for me.

-question by Coconut Dave




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users