【发布时间】:2026-02-07 06:15:01
【问题描述】:
您好,希望您能帮到我。我正在尝试将 div 垂直居中,但我一直失败。 我认为问题在于 div 是一个由 2 个元素组成的画廊 - 因为适合屏幕而无法滚动的主要图像 - 缩略图(通过 jquery 显示隐藏/显示和隐藏主要图像单击特定图标)。 我无法将子 div(图库)居中,因为我无法为父级(页面容器)设置高度,因为我需要缩略图可滚动。但我希望主要图像在页面中居中...
这是我的 html 代码(我保留了必要的):
<!--page -->
<div id="gallery-container">
<!-- main images -->
<div class="cycle-slideshow">
<img src="img.jpg">
</div>
<!-- thumbnails -->
<div class="thumbs">
<img src="img.jpg">
</div>
<!-- jquery hide/show code -->
<script type="text/javascript">
$('.thumbsicon').on('click', function() {
$('.cycle-slideshow').hide(); $('.thumbs').fadeIn();
});
$('.thumbs li img').on('click', function() {
var imgSrc = $(this).attr('src');
$('.cycle-slideshow').fadeIn('slow').css('background-image', 'url( + imgSrc + )');
$('.thumbs').hide();
});
</script>
</div>
还有css(考虑一下我的网站左侧有一个250px的侧边栏菜单):
#gallery-container{width:700px;z-index:70;margin:0 auto;position:relative;overflow:hidden;padding:0 20px 0 270px;}
.cycle-slideshow {width:700px;height:517px;margin:0 auto;padding:0;position:relative;}
.thumbs {position:relative; width:700px; margin:0 auto; padding:0; display:none;}
我尝试了许多 css 解决方案,但似乎都没有奏效。我能做些什么?提前谢谢...
【问题讨论】:
标签: jquery css responsive-design center absolute