gpt4 book ai didi

css - 如何在div中将图像居中?

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:16 26 4
gpt4 key购买 nike

我在一个较小的 320x460 像素的 div 中显示一张 1000x1000 像素的图像,并设置了 overflow:auto,这样你就可以在 div 中滚动图像。当我加载页面时,图像从图像的左上角开始显示,我想知道如何将图像居中显示?

感谢任何意见,谢谢。

这是代码。在我的索引文件中,我像这样加载页面:

$('#kartamap').load('imagemap.html', function() {
alert(".load has loaded");
imagescroll();

});

这是我的页面。

<div id="themap">

<div id="imagediven" style="height:460px; width:320px;">

<img src="image.png" width="1000px" height="1000px"/>

</div>

<style>
#imagediven {
overflow:auto;
height:460px;
width:320px;
position:relative;
}

</style>

<script type="text/javascript">

function imagescroll(){
var element = document.getElementById('imagediven'), /* This is your div */
scrollHeight = element.scrollHeight,
scrollWidth = element.scrollWidth;
clientHeight =$(window).height();
clientWidth = $(window).width();

alert(scrollHeight);//this is not displaying the image height?
alert(clientHeight);//this is getting a value off the screen

element.scrollTop = (scrollHeight - clientHeight) / 2;
element.scrollLeft = (scrollWidth - clientWidth) / 2;
}

</script>

</div>

最佳答案

您可以为容器 div 分配 320px 宽度和 460px 高度。然后在 css 中为您的图像应用 25% 的边距...它将使您的图像从各个方向居中对齐。

这是HTML

<div id="img">
<img src="Chrysanthemum.jpg" />
</div>

这是CSS

div#img
{
width:320px;
height:460px;
overflow:scroll;

}
div#img img{margin:0 auto;padding:0;margin:25%;}

关于css - 如何在div中将图像居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com