Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Created August 10, 2014 18:29
Show Gist options
  • Save obenjiro/6b215d7378bc186eacbb to your computer and use it in GitHub Desktop.
Save obenjiro/6b215d7378bc186eacbb to your computer and use it in GitHub Desktop.
Css only background-size: cover emulation
/* you can see it in action here http://jsbin.com/joxinizo/4/edit */
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.bgd {
position: relative;
width: 100%;
height: 33%;
overflow: hidden;
}
.bgd-cover {
position: absolute;
top: 0;
left: -50%;
width: 200%;
height: 100%;
overflow: hidden;
}
.bgd-cover-img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 13%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<meta name="viewport" content="width=device-width">
</head>
<body>
<div class="bgd">
<div class="bgd-cover">
<img
src="http://placehold.it/800x400"
alt=""
class="bgd-cover-img">
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment