有没有办法对背景图像使用最大宽度和高度?

发布于 2024-11-14 22:22:46 字数 324 浏览 2 评论 0原文

就这么简单。

将我的布局移动到一个流动的区域,处理可缩放的图像。使用 img 标签并将 max-width 设置为 100% 效果很好,但我宁愿使用将图像设置为背景的 div。

我遇到的问题是图像无法缩放到其背景中的 div 的大小。有什么方法可以将标记添加到后台代码以将其设置为容器的 100% 宽度吗?

#one {
    background: url('../img/blahblah.jpg') no-repeat;
    max-width: 100%;
}

That simple.

Moving my layout into a fluid territory, working on scalable images. Using the img tag and setting max-width to 100% works perfectly, but i'd rather use a div with the image set as its background.

The issue I'm running into is that the image doesn't scale to the size of the div it's in the background of. Any way to add markup to the background code to set it to 100% width of it's container?

#one {
    background: url('../img/blahblah.jpg') no-repeat;
    max-width: 100%;
}

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

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

发布评论

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

评论(6

坦然微笑 2024-11-21 22:22:46

正如三十点所说,您可以使用 CSS3 background-size 语法:

例如:

-o-background-size:35% auto;
-webkit-background-size:35% auto;
-moz-background-size:35% auto;
background-size:35% auto;

但是,正如三十点所说,这在 IE6、7 和 8 中不起作用。

有关 background-size 的更多信息,请参阅以下链接:
http://www.w3.org/TR/css3-background/#the-background-尺寸

As thirtydot said, you can use the CSS3 background-size syntax:

For example:

-o-background-size:35% auto;
-webkit-background-size:35% auto;
-moz-background-size:35% auto;
background-size:35% auto;

However, as also stated by thirtydot, this does not work in IE6, 7 and 8.

See the following links for more information about background-size:
http://www.w3.org/TR/css3-background/#the-background-size

恬淡成诗 2024-11-21 22:22:46

您可以使用 background-size 来做到这一点:

html {
    background: url(images/bg.jpg) no-repeat center center fixed; 
    background-size: cover;
}

除了 cover 之外,您还可以将 background-size 设置为很多值,看看哪些一个适合你: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size

规范:https://www.w3.org/TR/css-backgrounds-3/#the-background-size

适用于所有现代浏览器:http://caniuse.com/#feat=background-img-opts

You can do this with background-size:

html {
    background: url(images/bg.jpg) no-repeat center center fixed; 
    background-size: cover;
}

There are a lot of values other than cover that you can set background-size to, see which one works for you: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size

Spec: https://www.w3.org/TR/css-backgrounds-3/#the-background-size

It works in all modern browsers: http://caniuse.com/#feat=background-img-opts

不打扰别人 2024-11-21 22:22:46

看起来您正在尝试缩放背景图像?下面的参考文献中有一篇很棒的文章,您可以使用 css3 来实现此目的。

如果我错过了这个问题,那么我会谦虚地接受否决票。 (不过还是很高兴知道)

请考虑以下代码:

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

这适用于所有主要浏览器,当然在 IE 上并不容易。然而,有一些解决方法,例如使用 Microsoft 的过滤器:

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

有一些替代方案可以通过使用 jQuery 来放心地使用:

HTML

<img src="images/bg.jpg" id="bg" alt="">

CSS

#bg { position: fixed; top: 0; left: 0; }
.bgwidth { width: 100%; }
.bgheight { height: 100%; }

jQuery:

 $(window).load(function() {    

var theWindow        = $(window),
    $bg              = $("#bg"),
    aspectRatio      = $bg.width() / $bg.height();

function resizeBg() {

    if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
        $bg
            .removeClass()
            .addClass('bgheight');
    } else {
        $bg
            .removeClass()
            .addClass('bgwidth');
    }

}

theWindow.resize(resizeBg).trigger("resize");

});

我希望这会有所帮助!

源:完美的整页背景图片

It looks like you're trying to scale the background image? There's a great article in the reference bellow where you can use css3 to achieve this.

And if I miss-read the question then I humbly accept the votes down. (Still good to know though)

Please consider the following code:

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

This will work on all major browsers, of course it doesn't come easy on IE. There are some workarounds however such as using Microsoft's filters:

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

There are some alternatives that can be used with a little bit peace of mind by using jQuery:

HTML

<img src="images/bg.jpg" id="bg" alt="">

CSS

#bg { position: fixed; top: 0; left: 0; }
.bgwidth { width: 100%; }
.bgheight { height: 100%; }

jQuery:

 $(window).load(function() {    

var theWindow        = $(window),
    $bg              = $("#bg"),
    aspectRatio      = $bg.width() / $bg.height();

function resizeBg() {

    if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
        $bg
            .removeClass()
            .addClass('bgheight');
    } else {
        $bg
            .removeClass()
            .addClass('bgwidth');
    }

}

theWindow.resize(resizeBg).trigger("resize");

});

I hope this helps!

Src: Perfect Full Page Background Image

打小就很酷 2024-11-21 22:22:46

不幸的是,CSS 中没有 min (或 max)-background-size,您只能使用
背景大小。但是,如果您正在寻找响应式背景图像,则可以使用 background-size 属性的 VminVmax 单位来实现类似的效果。

示例:

#one {
    background:url('../img/blahblah.jpg') no-repeat;
    background-size:10vmin 100%;
}

这会将高度设置为垂直或水平方向上较小视口的 10%,并将宽度设置为 100%。

在这里阅读有关 css 单元的更多信息: https://www.w3schools.com/cssref/css_units.asp

Unfortunately there's no min (or max)-background-size in CSS you can only use
background-size. However if you are seeking a responsive background image you can use Vmin and Vmaxunits for the background-size property to achieve something similar.

example:

#one {
    background:url('../img/blahblah.jpg') no-repeat;
    background-size:10vmin 100%;
}

that will set the height to 10% of the whichever smaller viewport you have whether vertical or horizontal, and will set the width to 100%.

Read more about css units here: https://www.w3schools.com/cssref/css_units.asp

沙与沫 2024-11-21 22:22:46

这就是您正在寻找的解决方案!

background-size: 100% auto;

This is the solution you are looking for!

background-size: 100% auto;
少跟Wǒ拽 2024-11-21 22:22:46

使用 :before:afterma​​x-height

section {
  height: 100vh;
  width: 100%;
  background-color: #222;
}

section::before {
  content: ' ';
  display: block;
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 50%;
  height: 100%;
  max-height: 100px;
  transform: translateX(-50%);
  opacity: 0.8;
  background-image: url('https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg');
  background-repeat: no-repeat;
  background-position: 50% bottom;
  background-size: contain;
}
<section></section>

Use :before Or :after with max-height

section {
  height: 100vh;
  width: 100%;
  background-color: #222;
}

section::before {
  content: ' ';
  display: block;
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 50%;
  height: 100%;
  max-height: 100px;
  transform: translateX(-50%);
  opacity: 0.8;
  background-image: url('https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg');
  background-repeat: no-repeat;
  background-position: 50% bottom;
  background-size: contain;
}
<section></section>

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