简体   繁体   English

如何基于图像大小在CSS中添加背景大小?

[英]How to add background-size in css based on image size?

I am working on a project where the user will be able to select or upload an image and it will be displayed inside of these circles. 我正在开发一个项目,用户可以选择或上传图像,该图像将显示在这些圆圈中。 The images are dynamically being added as background-images. 这些图像将动态添加为背景图像。 The problem comes in when I am trying to set the background-size. 当我尝试设置背景尺寸时,问题就来了。 If the image is larger than the circles, I would like it to have a background-size cover, so it scales down. 如果图像大于圆圈,我希望它有一个背景尺寸的封面,因此可以缩小。 If the image is smaller than the circle, I don't want it to scale up or anything so setting no background size achieves this effect. 如果图像小于圆形,则不希望其按比例放大或其他任何设置,因此不设置背景尺寸即可达到此效果。

Any ideas on how I could achieve this? 关于如何实现此目标的任何想法?

The main html looks like this: 主要的html看起来像这样:

<div class="image-boundary">
    <div class="content">
        <div class="image-boundary-photo img-responsive" style="background-image: url('https://www.fillmurray.com/g/100/100');" alt="">
        </div>
    /div>
</div>

css: CSS:

.module-list-home .content .image-boundary-photo {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  text-align: center;
  background-repeat: no-repeat;
  background-position: center;
}

Here is the codepen of my example. 这是我的示例的codepen。 http://codepen.io/johnsonjpj/pen/zKyGZp?editors=1100 The third image should have a background-size: cover; http://codepen.io/johnsonjpj/pen/zKyGZp?editors=1100第三张图片应具有背景尺寸:封面; because it is bigger while the others should stay the way they are. 因为它更大,而其他人应该保持原样。

You might get some mileage out of the background-size setting " contain ". 您可能会从background-size设置“ contain ”中获得一些里程数。

I've added it inline to your pen, for a view, here: http://codepen.io/cam5/pen/vXvNOq?editors=1100 我已将其内联添加到您的笔中,以查看以下位置: http : //codepen.io/cam5/pen/vXvNOq?editors=1100

It upsizes when they are too small, so might not be entirely what you're after. 当它们太小时,它会放大,因此可能不完全是您想要的。 But querying image's size is very likely going to involve some javascript. 但是查询图像的大小很可能会涉及一些javascript。 This is a CSS-only approach. 这是仅CSS的方法。

<div class="image-boundary-photo img-responsive" 
     style="background-image: url('https://www.fillmurray.com/g/100/100');
     background-size: contain;" alt="It's Bill Murray.">
</div>

From MDN : 从MDN

contain 包含

A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). 一个关键字,它将图像尽可能地缩放并保持图像的宽高比(图像不会被压缩)。 Image is letterboxed within the container. 图像在容器中带有信箱。 When the image and container have different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color. 当图像和容器的尺寸不同时,空白区域(左侧/右侧的顶部/底部)将填充背景色。 The image is automatically centered unless over-ridden by another property such as background-position. 图像会自动居中,除非被其他属性(例如背景位置)覆盖。

Have you tried setting the property background-size: contain; 您是否尝试过设置属性background-size: contain; in the .module-list-home .content .image-boundary-photo class? .module-list-home .content .image-boundary-photo类中?

To solve the problem of small images also getting too large using background-size: contain; 为了解决小图像也使用background-size: contain;变得太大的问题background-size: contain; use the size as in percentage. 使用百分比形式的大小。 eg 例如

set the property background-size:60%; 设置属性background-size:60%; in the .module-list-home .content .image-boundary-photo class instead of contain and i think your problem will be solved. .module-list-home .content .image-boundary-photo类中而不是包含,我认为您的问题将得到解决。

Hope this helps. 希望这可以帮助。

[Edit] [编辑]

Also wrote solution with jquery dynamically checking image size and adjusting. 还写了解决方案与jQuery动态检查图像大小和调整。 Here is the jquery solution: http://codepen.io/Nasir_T/pen/ZpVbJo . 这是jquery解决方案: http : //codepen.io/Nasir_T/pen/ZpVbJo Make sure your page has the jquery.min linked. 确保您的页面链接了jquery.min。

Try this 尝试这个

$('.img-responsive').each(function(key, el) {
    var img = new Image;
    img.src = $(el).css('background-image').match(/^url\("?(.+?)"?\)$/)[1];
    if ($(el).width() <= img.width || $(el).height() <= img.height)
        $(el).css('background-size', 'cover');
})

here is a working example 这是一个有效的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM