473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to center background in css?

132 100+
Hi all,

I've got a question concerning CSS.

I'm creating a page but I want to keep my background centered. I created a big background of 1920x1080 px which is being used as background of a div.

So I want to center the div depending on the screensize of the visitor.

How do I go about on this one because I can't get it to work.

Thanks
Mar 3 '11 #1
4 3321
drhowarddrfine
7,435 Expert 4TB
Generally, just adding 'margin: 0 auto' will do it but depends on the rest of the markup.
Mar 3 '11 #2
Cainnech
132 100+
Never mind, got it!

In my case where my background is 1920x1080 it results in this:

Expand|Select|Wrap|Line Numbers
  1. #container
  2. {
  3. position: absolute;
  4. width: 1920px;
  5. height: 1080px;
  6. background-image: url('images/bg.jpg');
  7. background-repeat: no-repeat;
  8. margin-top: -540px; <!-- = 1080/2 -->
  9. margin-left: -960px; <!-- = 1920/2 -->
  10. top: 50%;
  11. left: 50%;
  12. }
  13.  
Greets,
Cainnech
Mar 3 '11 #3
drhowarddrfine
7,435 Expert 4TB
Yes. That is why you must post your complete markup. Using 'position:absolute' changes everything.
Mar 4 '11 #4
background-image:url('images/img2.jpg') center;


or
background-position:center;

Bharat Parmar(Bharat383)
Apr 21 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Heath | last post by:
Is it proper to center images in a <div> by using the text-align: center property? Or is there a better way?
15
by: red | last post by:
How do I center two side by side divs ? I've been writing css pages for a while but there's one thing tha still eludes me. I can center a div with margin auto. I can place two divs side by side...
1
by: laredotornado | last post by:
Hello, Is it possible to center an image that I put as a background to a table cell? How could this be done using CSS? Thanks, - Dave
6
by: Jerry Camel | last post by:
I want to center an image on the screen and have text centered over the image. What's the best way to do this? I've been playing with style tags and I can get the stuff to center automatically...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
8
by: speralta | last post by:
I'm playing around with a test page that uses a <div id="main"within the context of a body with a width of 100% to center a fixed width field on a page. For some reason, the page is not centering...
5
by: Markus Ernst | last post by:
Hello This is a test example: http://www.markusernst.ch/anthracite/ http://www.markusernst.ch/anthracite/living_divani.html After googling and experimenting for several hours, I ended up...
2
by: tonsi | last post by:
I have a page but it wont center the div. Can anyone take a look at it and help. When I try to center in the body using text-align: center; it doesn't work with the main container (called Table_01)....
16
by: stevedude | last post by:
CSS newbie again. I have a problem trying to get coffee mug images within anchor tags to center with my link text for a vertical list menu. If I use the horizontal/vertical properties of...
14
by: issentia | last post by:
I'm working on this site: http://www.essenceofsoy.com/redesign/index2.html and I'm having a few problems with getting the layout exactly right. 1) When the menu items are rolled over, they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.