CSS 图像大小调整

发布于 2024-09-10 09:47:54 字数 766 浏览 16 评论 0原文

我有这样的 CSS 代码:

<style>
     body {
     position:absolute;
     background-image: url(art/c11.jpg);
     width:100%;
     height:100%;
     }
</style>

正如我在网上阅读的那样,我期望这会调整背景图像的大小并使其适合浏览器窗口。

但没有。我认为我显然做错了什么(我对 CSS 的了解不够)。有什么建议吗?

更新:

我添加了孔示例(不起作用):

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>No Title</title>
<style type="text/css">
     body {
     position:absolute;
     background-image:url(art/c11.jpg);
     background-size:100%;
     background-repeat: no-repeat;
     width:100%;
     height:100%;
     }
</style>
</head>
<body >
</body>
</html>

I have this CSS code:

<style>
     body {
     position:absolute;
     background-image: url(art/c11.jpg);
     width:100%;
     height:100%;
     }
</style>

As I read on the net, I expected that this would resize the background image and fit it to the browser window.

But no. I think I am obviously doing something wrong (I don't know enough CSS). Any tips?

UPDATE:

I add the hole example (not working):

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>No Title</title>
<style type="text/css">
     body {
     position:absolute;
     background-image:url(art/c11.jpg);
     background-size:100%;
     background-repeat: no-repeat;
     width:100%;
     height:100%;
     }
</style>
</head>
<body >
</body>
</html>

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

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

发布评论

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

评论(5

浅沫记忆 2024-09-17 09:47:54

添加背景重复属性。

完成的代码应该如下所示:

     body {
     position:absolute;
     background-image: url(art/c11.jpg);
     background-size:100%;
     background-repeat: no-repeat;
     width:100%;
     height:100%;
     }

add the background-repeat property.

finished code should look like this:

     body {
     position:absolute;
     background-image: url(art/c11.jpg);
     background-size:100%;
     background-repeat: no-repeat;
     width:100%;
     height:100%;
     }
东风软 2024-09-17 09:47:54

如果您要跨浏览器进行此工作,最好将图像放在页面上,然后将所有内容包装在位于顶部的 DIV 中。例如

CSS

#background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#content {
  position: relative;
  z-index: 1;
}

如果您计划支持 IE6,则添加以下代码段:

<!--[if IE 6]>
  <style type="text/css">
    html {
      overflow-y: hidden;
    }

    body {
      overflow-y: auto;
    }

    #background {
      position:absolute;
      z-index:-1;
    }

    #content {
      position:static;
    }
  </style>
<![endif]-->

HTML

<img id="background" src="art/c11.jpg" alt="" />

<div id="content">
  Your content
</div>

正在运行的代码。

If you're going to make this work cross browser, you're better off putting an image on the page and then wrap all your content in a DIV that's laid on top. E.g.

CSS

#background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#content {
  position: relative;
  z-index: 1;
}

If you plan on supporting IE6, then add this snippet:

<!--[if IE 6]>
  <style type="text/css">
    html {
      overflow-y: hidden;
    }

    body {
      overflow-y: auto;
    }

    #background {
      position:absolute;
      z-index:-1;
    }

    #content {
      position:static;
    }
  </style>
<![endif]-->

HTML

<img id="background" src="art/c11.jpg" alt="" />

<div id="content">
  Your content
</div>

Code in action.

柳若烟 2024-09-17 09:47:54

我认为没有办法使用 CSS 来拉伸或控制背景图像的大小。但是,您可以使用 background-positionbackground-attachmentbackground-repeat 属性来实现替代结果。

或者您可以使用一些 JavaScript 将图像放入较低的 z-index 图层并将该图层设置为背景,但是这有点黑客攻击

I dont think there is a way to stretch or control the size of a background image using CSS. However, you could use the background-position, background-attachment and background-repeat porperties to achieve an alternative result..

or you may use some JavaScript putting Image into a lower z-index layer and set the layer as you backgournd, however that is a little bit Hacking there

坐在坟头思考人生 2024-09-17 09:47:54

通过使用css3,您可以实现透视图像调整大小,

html { 
background: url(bg.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

这适用于所有浏览器和ie9+。
以下过滤器也适用于 ie7 和 ie8。

filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.bg.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='bg.jpg', sizingMethod='scale')";

By using css3 you can achieve perspective image resize,

html { 
background: url(bg.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

This works in all browsers and ie9+.
The following filters works in ie7 and ie8 too.

filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.bg.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='bg.jpg', sizingMethod='scale')";
孤独患者 2024-09-17 09:47:54

您可以使用 background-size:100% 的 CSS3 属性,但对该属性的支持尚未全面,并且无法在较旧的浏览器中工作。为了实现你想要的,你需要一些技巧才能让它发挥作用。有很多这样的博客一个可以向您展示你需要做什么。我不建议这样做,因为如果您有大图像/连接速度慢,您将看到图像加载。如果图像质量不是最好的,那么它在屏幕上或以不同的分辨率拉伸时看起来不会很好。

You can use the CSS3 property of background-size:100% but the support for this property is not across the board yet and won't work in older browsers. To achieve what you want you need a bunch of hacks to get it to work. There are a number of blogs like this one that will show you what you need to do. I don't recommend doing this since if you have a big image/slow connection you will see the image load. If the image isn't the best quality then it won't look good all stretched across a screen or in a different resolution.

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