简体   繁体   English

为什么我的图像不在我的DIV中居中?

[英]Why is my image not centered in my DIV?

I've got to admit, I really don't much about HTML, but I'm attempting to learn. 我必须承认,我对HTML的了解并不多,但是我正在尝试学习。 I have an image centered in a DIV, but it isn't truly centered horizontally. 我有一个以DIV为中心的图像,但它并不是真正地水平居中。 The left edge of it is where the center would be, like this: 它的左边缘是中心所在的位置,如下所示:

[----------|----------] [---------- | --------------]

[----------|image--] [---------- |图片-]

[----------|----------] [---------- | --------------]

What to do, what to do.... 怎么办,怎么办....

EDIT Some code: 编辑一些代码:

<style>
#Outer {
position:relative;
height:100%;
width:100%;
text-align: center;
}

#Picture {
position:fixed;
top: 30%;

}
</style>

Also, I am centering it vertically AND horizontally. 另外,我将其垂直和水平居中。 Vertical is working just fine. 垂直工作正常。

You can simple do it like this. 您可以像这样简单地做到这一点。 :) :)

<div style="margin:auto;">
      <img src="" id="" />
</div>

There can be several reasons for this, please post your html and css code to get a better answer. 可能有多种原因,请发布您的html和css代码以获得更好的答案。

Something that should work is setting the parent text-align property to center in the css. 应该起作用的是将parent text-align属性设置为在CSS中center ( see demo ) 请参见演示

I'm only learning html too but i have found that using something like 我也只学习html,但是我发现使用类似

#picture
{
    margin-left:auto;
    margin-right:auto;
}

will get the div or whatever the style is for to be in the middle of the parent div 将获得div或父div中间的任何样式

hope it helps 希望能帮助到你

You can try changing position to relative. 您可以尝试将位置更改为相对。 (see http://jsfiddle.net/VLCqz/1/ ) (请参阅http://jsfiddle.net/VLCqz/1/

As for the reason why... 至于为什么...

Set a width on the image and then set the margin on the image to be 0px auto. 在图像上设置宽度,然后将图像上的边距设置为0px自动。

<!-- Your HTML -->
<img src="path_to_image.jpg" alt="set alternate text" id="img_id" />

/* Your CSS: */
#img_id {
width: set the width of the image in pixels here (i.e. 250px);
margin: 0px auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM