Centrar una imagen de 1250 px de ancho en un div de 950 px de ancho - problema de CSS

I have an image of 1250px wide on my web page. Example: http://img694.imageshack.us/img694/6171/unledvla.jpg

I have a div below this image of 950px wide.

I would like it so when I reduce the window size to 950px wide that the image is "centered" and that the 150px green areas (on the left and right of the 1250px image) are not visible.

¿Es esto posible?

Aquí está mi código actual:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<title>CSS Layout</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
div#container{background:green;width:950px;margin:0 auto}



<div style="width:1250px;background:url('image-1250px.jpg') 0 0 no-repeat;height:360px;margin:0 auto">&nbsp;</div>

<div id="container">

<div id="header"><h1>Header</h1></div>

    <div id="wrapper">

        <div id="content">
        <p><strong>1) Content here.</strong> column long long column very long fill fill fill long text text column text silly very make long very fill silly make make long make text fill very long text column silly silly very column long very column filler fill long make filler long silly very long silly silly silly long filler make column filler make silly long long fill very.</p>
        <p>very make make fill silly long long filler column long make silly silly column filler fill fill very filler text fill filler column make fill make text very make make very fill fill long make very filler column very long very filler silly very make filler silly make make column column </p>
        <p>fill long make long text very make long fill column make text very silly column filler silly text fill text filler filler filler make make make make text filler fill column filler make silly make text text fill make very filler column very </p>

        <p>column text long column make silly long text filler silly very very very long filler fill very fill silly very make make filler text filler text make silly text text long fill fill make text fill long text very silly long long filler filler fill silly long make column make silly long column long make very </p>




Many thanks for any pointers with this.

preguntado el 08 de noviembre de 11 a las 16:11

Look very muck alike this one stackoverflow.com/questions/7822167/… -

@alexey that's a much more complicated question -

3 Respuestas

(1) Change the position style of the background image from "0 0 no-repeat" to "top center no-repeat". (2) Remove "width:1250px;", as this causes horizontal scrolling when the window size is reduced.

To achieve what I think you're describing, you'd want the background image itself to be 1250px wide.

respondido 08 nov., 11:21

Great suggestions on Number 2. That has fixed my issue :D - michaelmcgurk

I think what you are probably looking to do here is to center the background image and have #container appear within the white center area.

To do this, you need to set the body background to the image and have it center horizontally, then repeat vertically to fill the vertical browser area:

body {
  background-image: url('image-1250px.jpg');
  background-position: center;
  background-repeat: repeat-y;
  background-color: green;

time to deal with the #container. we will set this to 950px wide and margins to automatic to center the element.

#container {
  width: 950px;
  margin-left: auto;
  margin-right: auto;

You should try to stay away from inline CSS styles.

respondido 08 nov., 11:21

Does it have to go on the div? Can it not go on your body tag?

body { background: url('http://img694.imageshack.us/img694/6171/unledvla.jpg') repeat-y center top; }

respondido 08 nov., 11:21

Thanks for your reply. Unfortunately, yes it has to go on the div. - michaelmcgurk

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.