10

I would like to center some div which has background image. There is problem with response of this div, because if I set width on 80% and height on 80% the bg-image is not on center. I tried everything, but the picture can't just stand on center and if the browser is smaller or bigger this is very big problem.

So if you look at the picture

block on center

I want to make this white block responsive.

There is a little of css which I've already written, but for now is non-responsive:

top: 20%;
left: 30%;
display: block;
position: absolute;
background: url(images/background.png) no-repeat;
background-size: 750px 417px;
width: 750px;
height: 417px;
user1257255
  • 1,161
  • 8
  • 26
  • 55

7 Answers7

20

You could use CSS transform:

position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
Steve Glick
  • 698
  • 7
  • 19
14

I wanted to do the same thing 2 years ago, there's the solution:

Because you want it responsive, you may use the @media function in CSS3. Like this:

@media (max-width: 480px) {
    #div {
        top: 50%; /* IMPORTANT */
        left: 50%; /* IMPORTANT */
        display: block;
        position: absolute;
        background: url(images/background.png) no-repeat center center;
        width: 750px;
        height: 417px;

        margin-top: -208.5px; /* HALF OF THE HEIGHT */
        margin-left: -375px; /* HALF OF THE WIDTH */
    }
}

The max-width you use is the maximum width of the device screen. You just copy it and change the width, height, margin-left and margin-top for the image. Also, you should change the background tag!

It will center the image on the page.

You can see an exemple at: Créations MicroWeb - Carrières. The image is totally centered even if you change the window side.

You can add overflow: hidden; on the body to make the page unscrollable when the resolution is too low. Like I did.

EDIT: JSFiddle

Frederick Marcoux
  • 2,195
  • 1
  • 26
  • 57
  • Responsive like Bootstrap? That the background resize with the device screen? – Frederick Marcoux Sep 28 '12 at 18:44
  • Yes, if someone have smaller screen to show smaller background picture and if someone have bigger screen to show it only full width/height picture and nothing more. – user1257255 Sep 28 '12 at 19:26
  • The div is really centered now, but there is still problem with response. If I set max-width, this means that div will disappear if someone with too big screen open up this site. I need to make smaller image when someone with small screen visit my site, but I don't know how to do that. Do I need JavaScript to check width and height of it's browser? – user1257255 Sep 29 '12 at 17:21
  • Yeah, with `max-width` it will. But you can also use `min-width` like `@media (min-width: 1280px) { }` and any screen size over this will show the image selected. – Frederick Marcoux Sep 29 '12 at 17:29
9

Try with auto margins and display as table:

.your-class {
  margin-left: auto;
  margin-right: auto;
  display: table;
}
monteirobrena
  • 2,562
  • 1
  • 33
  • 45
2

You can use margin:0 auto; to center a div horizontally as long as its width is less than that of the container div.

thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
2

Please try this:

img { max-width:100%; max-height:100%; margin:auto; }
suresh gopal
  • 3,138
  • 6
  • 27
  • 58
2
.container{display: flex; justify-content: center; align-items: center}
1

I have used display: inline-block; on element to center and text-align: center; on parent div.

kravits88
  • 12,431
  • 1
  • 51
  • 53