Nov 22, 2009

Centering Background Image In Css

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > CSS (Cascading Style Sheets)

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.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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

 

 

 


Comment/Reply (w/o sign-up)

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.

Comment/Reply (w/o sign-up)

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!

Comment/Reply (w/o sign-up)

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.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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 )

Comment/Reply (w/o sign-up)



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*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : centering, background, image, css

  1. Problem Aligning In Firefox
    Problem aligning an image in Firefox. (9)
  2. Link Image Css Bug In Firefox
    Can anyone help me? (7)
    I have a problem in a site I am creating. I have styled my post content links in the following
    way. CODE #post-content a {     background-color: #bf6f3c;     color: #fff;     padding: 2px
    2px 1px 2px;     border-bottom: 1px solid #7f4926; } #post-content a:hover {     color: #f5fac7; }
    And when I insert an link image like so: There seems to be an extra border inherited from my
    #post-content a style. I have attached the screenshot to show the problem. This was how my link
    image should look like: CODE #post-content a img {     background-color: #b....
  3. 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 ....
  4. 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' /> ....
  5. 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 "> p id =" bread....
  6. 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 But if
    you go here , you can see that the text does not stay to the right of the thumbnails. What am I
    doing wrong? Thanks for your help!....
  7. 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
See Also,
advertisement


Centering Background Image In Css

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com