有关如何使div居中的问题
当div有一个固定的宽度(y px)时,我只使用左边的: 50%和空白处的左边:-y/2 px;
但是,如果div的宽度是100%,最大宽度是固定的,该如何居中呢?即
body_container
{
position: absolute;
margin: auto;
max-width: 1750px;
height: 100%;
width: 100%;}
发布于 2013-06-01 21:19:03
尝试使用此jQuery创建页面的div中心:
<script>
jQuery.fn.vh_center = function (absolute) {
return this.each(function () {
var t = jQuery(this);
t.css({
position: absolute ? 'absolute' : 'fixed',
left: '50%',
top: '50%',
}).css({
marginLeft: '-' + (t.outerWidth() / 2) + 'px',
marginTop: '-' + (t.outerHeight() / 2) + 'px'
});
if (absolute) {
t.css({
marginTop: parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(),
marginLeft: parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
});
}
});
};
$(document).ready(function(){
$('#Your_div').vh_center();
});
</script>https://stackoverflow.com/questions/16873049
复制相似问题