保持图像居中 [英] Keep image centered

查看:150
本文介绍了保持图像居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的html:

 < img src =http://i1.ytimg.com/vi /XWGszmviDpA/maxresdefault.jpgclass =bg> 

到目前为止,只要窗口不小于图像,现在我有问题,保持脸在中间,当窗口较小!我如何解决这个问题?
对我来说,图像高度保持在窗口的100%的重要性。



我的css:

  img.bg {
width:auto;
height:100%;
position:fixed;
left:0;
right:0;
margin:0 auto;

}



谢谢! >

http://jsfiddle.net/67jDp/1/

解决方案

您可以使用这个javascript。



css :

  img.bg {
height:100%;
position:fixed;
left:50%;
}

JS:

  var i = $('img'); 
var w = i.width();

i.css({marginLeft:-w / 2});

$(window).resize(function(){
i.css({marginLeft:-w / 2});
});

DEMO: http://jsfiddle.net/67jDp/4/


I have this simple html:

<img src="http://i1.ytimg.com/vi/XWGszmviDpA/maxresdefault.jpg" class="bg">

So far i centered the image correctly as long as the window is not smaller than the image. Now i have problems to keep the face in the middle when the window is smaller! How can i fix this? For me its important that the image height stays 100% of the window.

My css:

img.bg {  
  width: auto;
  height: 100%;
  position: fixed;
  left:0;
  right:0;
  margin:0 auto;   

}

Thanks!

http://jsfiddle.net/67jDp/1/

解决方案

You can use this with little javascript

css :

img.bg {
    height: 100%;
    position: fixed;
    left:50%;
}

JS :

var i = $('img');
var w = i.width();

i.css({marginLeft: -w/2});

$( window ).resize(function() {
    i.css({marginLeft: -w/2});
});

DEMO : http://jsfiddle.net/67jDp/4/

这篇关于保持图像居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆