How to center an image that is wider than the browser window (not a background image)

后端 未结 4 1378
青春惊慌失措
青春惊慌失措 2021-01-04 10:16

How can I centre a large image to the browser window, so that if it is wider than the window it will still be centred?

See: http://carolineelisa.com/rob/

I d

4条回答
  •  自闭症患者
    2021-01-04 10:53

    Honestly, the best way to do it is with a background image. Since you're going to be using javascript to change the image, you can go ahead and add a little more to change the height as well. To do so, load the image into an invisible div (style="position:absolute; top:-3000px; left:-3000px;") in addition to setting it as a background-image. You can then use javascript to get its height from the img tag inside the invisible div.

提交回复
热议问题