If I have a fixed sized container div, and an unknown sized image, how do I horizontally and vertically center it?
You could use background-position
for that.
#your_div {
background-position: center center;
background-image: url('your_image.png');
}
Center image horizontally and vertically
DEMO:
http://jsfiddle.net/DhwaniSanghvi/9jxzqu6j/
.section {
background: black;
color: white;
border-radius: 1em;
padding: 1em;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
If setting the image as a background image and centering it that way isn't an option, the jQuery to adapt the answer you linked for static images would go:
$(".fixed-div img.variable").each(function(){
//get height and width (unitless) and divide by 2
var hWide = ($(this).width())/2; //half the image's width
var hTall = ($(this).height())/2; //half the image's height, etc.
// attach negative and pixel for CSS rule
hWide = '-' + hWide + 'px';
hTall = '-' + hTall + 'px';
$(this).addClass("js-fix").css({
"margin-left" : hWide,
"margin-top" : hTall
});
});
assuming a CSS class defined as
.variable.js-fix {
position:absolute;
top:50%;
left:50%;
}
with the fixed-width div having a height and position:relative
declared.
[important js edit: switched '.style()' to '.css()']
If you dont know the image sizes but you have uploaded the pictures next to the size of the container (maybe bigger or smaller images), this post can be useful. Something that is working for me is the next code:
<a class="linkPic" href="#">
<img src="images/img1.jpg" alt=""/>
</a>
<a class="linkPic" href="#">
<img src="images/img2.jpg" alt=""/>
</a>
<a class="linkPic" href="#">
<img src="images/img3.jpg" alt=""/>
</a>
And in the .css file you have the next rules:
a.linkPic{
position:relative;
display: block;
width: 200px; height:180px; overflow:hidden;
}
a.linkPic img{
position:absolute;
}
You can notice you have an image tag inside the a.linkPic, so first you have to set it as "position:relative" for containing the "img" absolute element. The trick to center the picture without problems is a little jQuery code. Follow the comments to understand what is going on (some lines were taken from the Vladimir Maryasov post in this page):
$("a.linkPic img").each(function() {
// Get container size
var wrapW = $("a.linkPic").width();
var wrapH = $("a.linkPic").height();
// Get image sizes
var imgW = $(this).width();
var imgH = $(this).height();
// Compare if image is bigger than container
// If image is bigger, we have to adjust positions
// and if dont, we have to center it inside the container
if (imgW > wrapW && imgH > wrapH)
{
// Centrar por medio de cálculos
var moveX = (imgW - wrapW)/2;
var moveY = (imgH - wrapH)/2;
// attach negative and pixel for CSS rule
var hWide = '-' + moveX + 'px';
var hTall = '-' + moveY + 'px';
$(this).addClass("imgCenterInDiv").css({
"margin-left" : hWide,
"margin-top" : hTall
});
} else {
$(this).addClass("imgCenterInDiv").css({
"left" : 0,
"right" : 0,
"top" : 0,
"bottom" : 0,
"margin" : "auto",
});
}//if
});
After this, all the pictures inside in a.linkPic containers will be placed perfectly centered. I Hope this post can be useful for someone!
I used
.on('load', function () {
instead
.each(function(){
used in this answer it helps to eliminate the problem with empty values of height and width when the picture is not yet loaded
<style type="text/css">
.fixed-div {
position: relative;
}
.variable {
width: 100%;
height: auto;
}
.variable.js-fix {
position:absolute;
top:50%;
left:50%;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function(){
$(".fixed-div img.variable").on('load', function () {
//get height and width (unitless) and divide by 2
var hWide = ($(this).width())/2; //half the image's width
var hTall = ($(this).height())/2; //half the image's height, etc.
console.log("width", $(this).width());
console.log("height", $(this).height());
// attach negative and pixel for CSS rule
hWide = '-' + hWide + 'px';
hTall = '-' + hTall + 'px';
$(this).addClass("js-fix").css({
"margin-left" : hWide,
"margin-top" : hTall
});
});
});
</script>
<div class="fixed-div">
<img class="variable" src=""/>
</div>
Use height:100% for the html tag, body tag, container and an empty placeholder element plus display:inline-block; and vertical-align: middle for the content and placeholder to vertically center content that has an undefined height across browsers. The placeholder element is given 100% height to prop up the line box, so that vertical-align: middle has the desired effect. Use a fixed width container to wrap the images.
Use display:inline for the content div and text-align center to the container div to do horizontal centering for content that has an undefined width across browsers.
Combine both techniques to create a centered image gallery:
<!doctype html>
<html>
<head>
<title>Centered Image Gallery</title>
<style type="text/css">
html, body, .container, .placeholder { height: 100%;}
img { margin-left: 20px; margin-top: 10px; }
.container { text-align:center; }
/* Use width of less than 100% for Firefox and Webkit */
.wrapper { width: 50%; }
.placeholder, .wrapper, .content { vertical-align: middle; }
/* Use inline-block for wrapper and placeholder */
.placeholder, .wrapper { display: inline-block; }
.fixed { width: 900px; }
.content { display:inline; }
@media,
{
.wrapper { display:inline; }
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="wrapper">
<div class="fixed">
<img src="http://microsoft.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://microsoft.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
<img src="http://mozilla.com/favicon.ico">
<img src="http://webkit.org/favicon.ico">
<img src="http://userbase.kde.org/favicon.ico">
<img src="http://www.srware.net/favicon.ico">
<img src="http://build.chromium.org/favicon.ico">
<img src="http://www.apple.com/favicon.ico">
<img src="http://opera.com/favicon.ico">
</div>
</div>
</div>
<span class="placeholder"></span>
</div>
</body>
</html>