为什么我的图像没有在 DIV 中居中?
我必须承认,我确实不太了解 HTML,但我正在尝试学习。我有一个位于 DIV 中心的图像,但它并不是真正水平居中。它的左边缘是中心所在的位置,如下所示:
[----------|------------]
[------------| image--]
[----------|----------]
做什么,做什么....
编辑 一些代码:
<style>
#Outer {
position:relative;
height:100%;
width:100%;
text-align: center;
}
#Picture {
position:fixed;
top: 30%;
}
</style>
此外,我将其垂直和水平居中。垂直工作得很好。
I've got to admit, I really don't much about HTML, but I'm attempting to learn. I have an image centered in a DIV, but it isn't truly centered horizontally. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试将位置更改为相对位置。 (参见http://jsfiddle.net/VLCqz/1/)
至于原因.. 。
You can try changing position to relative. (see http://jsfiddle.net/VLCqz/1/)
As for the reason why...
造成这种情况的原因可能有多种,请发布您的 html 和 css 代码以获得更好的答案。
应该有效的方法是将 css 中的
parent text-align
属性设置为center
。 (查看演示)There can be several reasons for this, please post your html and css code to get a better answer.
Something that should work is setting the
parent text-align
property tocenter
in the css. (see demo)我也只学习 html,但我发现使用类似的东西
会得到 div 或任何样式位于父 div 的中间
希望它有帮助
I'm only learning html too but i have found that using something like
will get the div or whatever the style is for to be in the middle of the parent div
hope it helps
设置图像的宽度,然后将图像的边距设置为自动 0px。
Set a width on the image and then set the margin on the image to be 0px auto.
你可以简单地这样做。 :)
You can simple do it like this. :)