【问题标题】:Make inner image respond to outer div size使内部图像响应外部 div 大小
【发布时间】:2018-11-27 07:53:13
【问题描述】:

我正在处理 WordPress 中的工具集视图(我正在使用 SCSS)。我有一些我想放入 300px x 300px div 的图像。我试过这个:

.container {
max-width: 300px;
max-height: 300px;

& img {
  width:100%;
  height:auto;
}

}

我知道我应该使用background-image,但我无法将自定义文件添加到工具集视图中的 div 中

编辑:

工具集视图代码:

<a href="[wpv-post-url]">
<div class="project_container">
    <div class="project_text">[wpv-post-title]</div>
    [types field="projekt-bild"][/types]
</div>

CSS:

.project_row {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center; }

.project_container {
max-width: 300px;
max-height: 300px;

.project_container img {
  width:100%;
  height:auto;
}
}

但是当我这样做时,所有项目的大小都像图像宽度和高度一样,而不是我希望它们成为 ex 的容器。 300 像素 x 300 像素

【问题讨论】:

  • 以及您的问题,因为它有效:jsfiddle.net/m3x6tc8p
  • 但是如何使所有容器的大小相同,而不会扭曲/裁剪里面的图像
  • 我认为您可能需要给我们一个可验证的示例问题是什么,因为您给我们的代码工作正常。 “容器”是什么意思?您的问题中只有一个容器。
  • @Krullmizter 为了帮助您,我们必须查看您的更多代码
  • 我在project_container中看不到任何img

标签: css wordpress custom-post-type


【解决方案1】:

如果您希望您的图像表现得像 CSS 背景图像,请使用 object-fit。

SCSS

.project_container {
    //  set relative position to make the
    //  image position to the post itself
    position: relative;

    //  random post size 
    //  not important for this to work
    width: 340px;
    height: 500px;



    img { 
        //  make image stretch to container 
        //  and show below content 
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: -1;

        //  object fit for modern browsers
        object-fit: cover;
        object-position: center;

        //  fallback 
        //  using js the image source is replaced with a transparant gif
        //  and the original is added as background image
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
    }
}

JS 修复,适用于不支持 object-fit 的旧版浏览器

//  is object fit supported
if (document.body.style.objectFit === undefined) {

    //  loop through all images    
    [].slice
        .call(document.querySelectorAll('img'))
        .map(img => {

        //  image has background-size cover or contain        
        if(['cover', 'contain']
           .indexOf(getComputedStyle(img).backgroundSize) !== -1) {

            //  use src as background image            
            img.style.backgroundImage = `url(${img.src})`;

            //  replace src with transparent gif
            img.src = 'data:;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
        }
    });
}



/*

Transpiled version
if (document.body.style.objectFit === undefined) {
    [].slice
        .call(document.querySelectorAll('img'))
        .map(function (img) {
        if (['cover', 'contain']
            .indexOf(getComputedStyle(img).backgroundSize) !== -1) {
            img.style.backgroundImage = "url(" + img.src + ")";
            img.src = 'data:;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
        }
    });
}

Transpiled and minified
void 0===document.body.style.objectFit&&[].slice.call(document.querySelectorAll("img")).map(function(A){-1!==["cover","contain"].indexOf(getComputedStyle(A).backgroundSize)&&(A.style.backgroundImage="url("+A.src+")",A.src="data:;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")});


*/

演示:https://codepen.io/jakob-e/pen/Pxwzpw

【讨论】:

【解决方案2】:

在评论中,您希望 image 行为类似于 backgroud-img

see working code here

.project_container {
max-width: 300px;
max-height: 300px;
position: relative;

& .project_text{
  position: absolute;
  top:0;
}
& img {
  width:100%;
}
}

【讨论】:

  • 乐于助人:)
猜你喜欢
  • 1970-01-01
  • 2014-03-26
  • 2013-12-30
  • 1970-01-01
  • 2014-02-21
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多