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

发布于 2024-12-13 13:29:58 字数 423 浏览 6 评论 0原文

我必须承认,我确实不太了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怎会甘心 2024-12-20 13:29:58

您可以尝试将位置更改为相对位置。 (参见http://jsfiddle.net/VLCqz/1/

至于原因.. 。

You can try changing position to relative. (see http://jsfiddle.net/VLCqz/1/)

As for the reason why...

风追烟花雨 2024-12-20 13:29:58

造成这种情况的原因可能有多种,请发布您的 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 to center in the css. (see demo)

爱人如己 2024-12-20 13:29:58

我也只学习 html,但我发现使用类似的东西

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

会得到 div 或任何样式位于父 div 的中间

希望它有帮助

I'm only learning html too but i have found that using something like

#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

hope it helps

梦言归人 2024-12-20 13:29:58

设置图像的宽度,然后将图像的边距设置为自动 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;
}

Set a width on the image and then set the margin on the image to be 0px auto.

<!-- 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;
}
你如我软肋 2024-12-20 13:29:58

你可以简单地这样做。 :)

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

You can simple do it like this. :)

<div style="margin:auto;">
      <img src="" id="" />
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文