Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center image in HTML viewport (without JavaScript)

I have an image I'd like to show in a browser such that:

  1. If the image is smaller than the browser viewport, the image is centered horizotally and vertically.
  2. If the image is larger than the viewport, the image is scaled down to fill as much of the viewport as possible without adjusting the aspect ratio of the image. Again, the image is centered horizotally and vertically.

I do not want to use JavaScript; what's the best/most semantic HTML and CSS to do this?

Update I've been asked for clarification regarding semantics: the image is content; the only content within the HTML.

Solution

@GionaF ideas got me to a happy (and very simple) solution:

HTML

<!DOCTYPE html>
<head>
  <title></title>
  <LINK href="test2.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div>
      <img src="photo.jpg" alt="photo" />
    </div>
</body>

CSS

img {
    max-width:100%;
    max-height:100%;
    position:absolute;
    top:0; left:0; right:0; bottom:0;
    margin:auto;
}
like image 981
Mike Avatar asked Sep 30 '12 21:09

Mike


1 Answers

You can achieve it in many ways, but i can't be "semantic" without knowing the context (is the image the main/only content of the page? is it in the middle of a blog post?), so i'll go for a div.


1. position:absolute; + margin:auto;

Support: crossbrowser

HTML

<html>
<body>
    <div id="container">
        <img src="your-image.jpg">
    </div>
</body>
</html>​

CSS

html,body,#container {
    height:100%;
}
#container {
    width:100%;
    position:relative;
}
#container > img {
    width:100%;
    max-width:400px; /* real image width */
    position:absolute;
    top:0; left:0; right:0; bottom:0;
    margin:auto;
}

Demo


2. display:table; + display:table-cell; + vertical-align:middle;

Support: IE8+, all other browsers - with IE7 fallback (Source 1) (2) (3)

HTML

<html>
<body>
    <div id="container">
        <span> /* it's important that you use a span here
                  not a div, or the IE7 fallback won't work */
            <img src="your-image.jpg">
        </span>
    </div>
</body>
</html>​

CSS

html,body,#container {
    height:100%;
}
#container {
    width:100%;
    display:table;
    *display:block; /* IE7 */
}
#container > span {
    display:table-cell;
    *display:inline-block; /* IE7 */
    vertical-align:middle;
    text-align:center;
}
#container > span > img {
    width:100%;
    max-width:400px; /* real image width */
}

Demo


3. background-size:contain;

Support: IE9+, all other browsers - with vendor prefixes (Source 1) (2)

HTML

<html>
<body>
    <div id="container"></div>
</body>
</html>​

CSS

html,body,#container {
    height:100%;
}
#container {
    margin:0 auto;
    max-width:400px; /* real image width */
    background:url(your-image.jpg) 50% 50% no-repeat;
    background-size:contain;
}

Demo


Be careful for how IE8 renders height:auto;, may not keep the ratio.


Edit: i just realized that you wrote "without adjusting the aspect ratio of the image". If you really don't want to keep the ratio, it's easier ... but do you really mean that? 

like image 122
Giona Avatar answered Oct 21 '22 01:10

Giona