Jul 24, 2008

Centering Background Image In Css

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > CSS (Cascading Style Sheets)

free web hosting

Centering Background Image In Css

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

Reply

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

Reply

jlhaslip
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

 

 

 


Reply

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

Reply

Lozbo
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!

Reply

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

Reply

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

Reply

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

Reply

Amezis
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 )

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. html centering background image in a website - 5.09 hr back. (1)
  2. css menus using background image - 8.20 hr back. (1)
  3. background centering - 15.10 hr back. (1)
  4. centering a url picture - 19.18 hr back. (1)
  5. css align background fixed center top - 24.21 hr back. (1)
  6. keep background image center in website - 25.32 hr back. (1)
  7. css center text on background image - 30.03 hr back. (1)
  8. how to blink background image in html in centre - 30.68 hr back. (1)
  9. inserting a background image in css - 33.60 hr back. (1)
  10. css images align background-image - 34.15 hr back. (1)
  11. background image in css - 39.03 hr back. (1)
  12. css set background image always in centre - 39.56 hr back. (1)
  13. centering background html - 40.39 hr back. (1)
  14. css center image code - 42.04 hr back. (1)
Similar Topics

Keywords : centering, background, image, css

  1. Link Image Css Bug In Firefox
    Can anyone help me? (7)
  2. Centering A Div Containing Image.
    I have a logo I need centered in a aujustable width page (9)
    http://sonesay.trap17.com/sonesayi/ Just a new layout I'm working on, Any ideas how I can get
    the logo to be centered? At the moment I can only float it left or right. I think is possible to get
    it done in tables but I would prefer to stay away from them in layouts. Any insight appreciated. by
    the way source code and css is at http://sonesay.trap17.com/sonesayi/index.html
    http://sonesay.trap17.com/sonesayi/main.css thanks ....
  3. Another Kind Of Css Background Float Trouble
    now with a <ul> (8)
    How'd you manage to post a topic with no real content? /blink.gif' border='0'
    style='vertical-align:middle' alt='blink.gif' /> ....
  4. Css Floating Trouble
    cant get a background color (15)
    I want #nav_info to have a background color CODE div#nav_info         {background:#000055;
    color:black;} div#nav_info p#breadcrumb    {float:left;} div#nav_info p#date
      {float:right;} But it doesnt show the background like i want, and id like to avoid as
    much as i can nesting into another div, does any body know how can i do this? If i put each
    paragraph a background color, it only shows for the part of the text they actually occupy in their
    own space, it leaves white areas between them... This is the xthml: HTML div id =" nav_info
    ">....
  5. Css Image Position Problem
    align image left or right (1)
    I am having problems figuring out why my image will not align how I want it. Here , you can see
    that the image is aligned to the left. This is the code for that block: CODE <a
    href="http://img355.imageshack.us/img355/844/r25870774779uu.jpg"><img
    src="http://img355.imageshack.us/img355/844/r25870774779uu.th.jpg" alt="bush
    bathroom break note" align="left" style="margin-right:
    5px;"></a>[text goes here] But if you go here , you can see that the
    text does not stay to the right of....
  6. Css Image Replacment
    using CSS to change the image path (5)
    I was wondering if it is possable to change the path to an image using CSS, so that I could provide
    a sort of template system rather then having to change a bunch of thing I could have astandard page
    and then by changing the external CSS I could change not only styles but the whole image, I soo how
    I could change the background-color { url(image.gif)} but whatabout an actual image say change the
    src of an img tag?....

    1. Looking for centering, background, image, css

Searching Video's for centering, background, image, css
advertisement



Centering Background Image In Css



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE