如何针对所有屏幕尺寸将网页上的图像居中

发布于 2024-11-05 10:54:13 字数 238 浏览 5 评论 0原文

我的 HTML 有问题。我在互联网上进行了搜索,但仍然没有真正的答案。

我有一个包含一些图像的网站,我希望它们位于中间。现在,在我的屏幕上,它们位于中间,但那是因为我通过将它们移动到一侧来将它们放置在那里。当我的朋友们看它时,图像偏离了中心。

这是网站;如果您使用的是 13.5 英寸屏幕,它看起来会位于中间。

I'm having a problem with my HTML. I've searched all over the internet, but still no real answer.

I have a website with some images, and I want them to be in the middle. Now, on my screen they're in the middle, but that's because I've put them there by moving them to one side. When my friends to look at it, the image is off-center.

Here's the website; if you are on a 13.5" screen it will look to be in the middle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

人事已非 2024-11-12 10:54:13

尝试这样的事情......

<div id="wrapper" style="width:100%; text-align:center">
<img id="yourimage"/>
</div>

Try something like this...

<div id="wrapper" style="width:100%; text-align:center">
<img id="yourimage"/>
</div>
那伤。 2024-11-12 10:54:13

text-align:center

text-align:center 样式应用于包含元素的元素将使这些元素居中。

<div id="method-one" style="text-align:center">
  CSS `text-align:center`
</div>

Thomas Shields 提到了这个方法



margin:0 auto

margin:0 auto 样式应用于块元素会将其置于其所在元素的中心。

<div id="method-two" style="background-color:green">
  <div style="margin:0 auto;width:50%;background-color:lightblue">
    CSS `margin:0 auto` to have left and right margin set to center a block element within another element.
  </div>
</div>

user1468562 提到了这一点method


Center 标签

我原来的答案是可以使用

标签。为此,只需将所需的内容放在标签之间的中心即可。 从 HTML4 开始,此标记已被弃用,尽管。
今天仍然受到技术支持(9 年后更新此内容),但我建议使用上面包含的 CSS 替代方案。

<h3>Method 3</h1>
<div id="method-three">
  <center>Center tag (not recommended and deprecated in HTML4)</center>
</div>

您可以在此 jsfiddle 中查看这三个代码示例的实际效果。

我决定应该修改这个答案,因为我之前给出的答案已经过时了。当我建议它作为解决方案时,它已经被弃用了,这就是 9 年后现在避免使用它的更多理由。

text-align:center

Applying the text-align:center style to an element containing elements will center those elements.

<div id="method-one" style="text-align:center">
  CSS `text-align:center`
</div>

Thomas Shields mentions this method



margin:0 auto

Applying the margin:0 auto style to a block element will center it within the element it is in.

<div id="method-two" style="background-color:green">
  <div style="margin:0 auto;width:50%;background-color:lightblue">
    CSS `margin:0 auto` to have left and right margin set to center a block element within another element.
  </div>
</div>

user1468562 mentions this method


Center tag

My original answer was that you can use the <center></center> tag. To do this, just place the content you want centered between the tags. As of HTML4, this tag has been deprecated, though. <center> is still technically supported today (9 years later at the time of updating this), but I'd recommend the CSS alternatives I've included above.

<h3>Method 3</h1>
<div id="method-three">
  <center>Center tag (not recommended and deprecated in HTML4)</center>
</div>

You can see these three code samples in action in this jsfiddle.

I decided I should revise this answer as the previous one I gave was outdated. It was already deprecated when I suggested it as a solution and that's all the more reason to avoid it now 9 years later.

山有枢 2024-11-12 10:54:13

在您的具体情况下,您可以将包含的 a 元素设置为:

a {
    display: block;
    text-align: center;
}

JS Bin demo。

In your specific case, you can set the containing a element to be:

a {
    display: block;
    text-align: center;
}

JS Bin demo.

从来不烧饼 2024-11-12 10:54:13
<div style='width:200px;margin:0 auto;> sometext or image tag</div>

这是水平工作的

<div style='width:200px;margin:0 auto;> sometext or image tag</div>

this works horizontally

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