CSS 和不同尺寸的显示器?

发布于 2024-09-04 20:06:22 字数 258 浏览 3 评论 0原文

我正在做一个网站(WIP)。我遇到了一些CSS问题,希望大家能帮助我。

我目前的情况:

  • 我有一张图像,我们将其命名为“bg.png”,宽度约为2500px,背景图像的中心有我的徽标。
  • 我的网站应该针对所有尺寸(从小 800 x 600 到 2400 x XXX)用户。

我的问题:

  • 如何居中背景图像(bg.png),以便徽标始终位于不同尺寸的显示器屏幕的中心(水平)?

I am doing a website (WIP). I encountered some CSS problems, hope you guys can help me.

My current situation:

  • I have am image, lets called it "bg.png" with the width about 2500px, and at the center of the background image has my logo.
  • My website should target all size (from small 800 x 600 to 2400 x XXX) users.

My problems:

  • How do I centralize the background image(bg.png), so that the logo always positioned in the centered(horizontally) of different size of monitors screen?

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

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

发布评论

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

评论(4

陪你搞怪i 2024-09-11 20:06:22
body { background: url('bg.png') 50% 50% no-repeat; }

这会将全尺寸图像放置在页面中央,用户将在浏览器窗口允许的范围内看到尽可能多的背景图像。

body { background: url('bg.png') 50% 50% no-repeat; }

This will place the image at full-size at the center of the page, and the user will see as much of this background image as their browser window permits.

孤寂小茶 2024-09-11 20:06:22
#your_img {
  width: 2500px; //In example 2500px
  margin: 50%;
  padding: -1250px; // 2500 divided by 2
}

它可能不适用于 Internet Explorer,但您可以使用position:absolute的一点技巧,如上所述

#your_img {
  width: 2500px; //In example 2500px
  margin: 50%;
  padding: -1250px; // 2500 divided by 2
}

It probably don't work on Internet Explorer, but you can use a little hack with position: absolute, as above

三生一梦 2024-09-11 20:06:22
#your_img {

   position: absolute;
   left: 50%;
   margin: -1250px;
}

当您将 img 元素放入另一个盒子时,此解决方案是必要的。但要小心 - 它可能会改变父母盒子的大小。

#your_img {

   position: absolute;
   left: 50%;
   margin: -1250px;
}

This solution is necessary, when you put img element onto another box. But be careful - it might change size of parent's box.

沉鱼一梦 2024-09-11 20:06:22

如果它只是一个徽标文件,您可以使用

<style>
img
{
    position:absolute;
    left:-50%;
    top:-50%;
    z-index:-1;
}
</style>

If it was just a logo file you could use

<style>
img
{
    position:absolute;
    left:-50%;
    top:-50%;
    z-index:-1;
}
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文