Html 绝对位置 div 中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4890790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:31:03  来源:igfitidea点击:

Image in absolute position div

htmlcss

提问by ruby_newbie

Possible Duplicate:
How to make an image center (vertically & horizontally) inside a bigger div

可能的重复:
如何在更大的 div 内制作图像中心(垂直和水平)

I have a div which is with absolute position (width/height are 100%). Notice, not px, percents.
Inside this div I have an image. How do I center this image in this div?

我有一个绝对位置的 div(宽度/高度为 100%)。注意,不是 px,百分比。
在这个 div 里面我有一个图像。我如何在这个 div 中将此图像居中?

回答by Rudu

CSS:

CSS:

/* where margin-left = {img width}/2, and margin-top= {img height}/2 */
.bigdiv {
   width:100%;
   height:100%;
   position:absolute;
}
.bigdiv img {
   position:absolute;
   left:50%;
   top:50%;
   margin-left:-10px;
   margin-top:-10px;
}

HTML:

HTML:

<div class="bigdiv"><img src="eg.png" /></div>

You could also put your margin-left, margin-top commands as a style on the img tag instead (since they're unique per image).

您也可以将 margin-left、margin-top 命令作为 img 标签上的样式(因为它们在每个图像上都是唯一的)。