Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 如何使图像的高度和兄弟内容相匹配并保持图像的外观?_Css - Fatal编程技术网

Css 如何使图像的高度和兄弟内容相匹配并保持图像的外观?

Css 如何使图像的高度和兄弟内容相匹配并保持图像的外观?,css,Css,我想为通知卡做一个布局,如下所示: <div class="hero-unit table"> <div class="table-row"> <div class="width-one table-cell scaled-image"></div> <div class="width-two table-cell text-area"> <h2>Text go

我想为通知卡做一个布局,如下所示:

<div class="hero-unit table">
    <div class="table-row">
        <div class="width-one table-cell scaled-image"></div>
        <div class="width-two table-cell text-area">
            <h2>Text goes in here</h2>
            <p>Text goes in here</p>
        </div>
    </div>
</div>
.table{
    position: relative;
    display:table;
}
.table-row{
    position: relative;
    display:table-row;
}
.table-cell{
    position: relative;
    display:table-cell;
}
.hero-unit{
    width:600px;
    padding:20px;
    background:yellow;
    margin-bottom:20px;
}
.scaled-image { 
    background: url('http://i.stack.imgur.com/jGlzr.png') no-repeat center center; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    border:1px solid green;
}
.text-area{
    background:blue;
}
.width-one {
    width:20%;   
}
.width-two {
    width:80%;
    padding:0 20px;
}

它遵循以下布局规则:

  • 通知的高度仅基于文本,而不是图像
  • 图像高度与文本高度匹配(因此,如果要删除字幕,通知会缩小,图像也会缩小)
  • 图像宽度从属于其高度,以保持图像的自然纵横比

我已经经历了flexbox、浮动、绝对定位的黑客攻击,我想不出一种方法来实现这一点。救命啊!如果这样做更简单,我会满足于要求图像为正方形。

我通过使用background size:cover属性,并在包含元素上使用居中的固定背景图像,在我的站点上绕过了缩放后的背景图像

技巧是将图像设置为div的背景,并根据可用空间自动调整该div的大小

下面是一个JSFIDLE:

标记应该是这样的:

<div class="hero-unit table">
    <div class="table-row">
        <div class="width-one table-cell scaled-image"></div>
        <div class="width-two table-cell text-area">
            <h2>Text goes in here</h2>
            <p>Text goes in here</p>
        </div>
    </div>
</div>
.table{
    position: relative;
    display:table;
}
.table-row{
    position: relative;
    display:table-row;
}
.table-cell{
    position: relative;
    display:table-cell;
}
.hero-unit{
    width:600px;
    padding:20px;
    background:yellow;
    margin-bottom:20px;
}
.scaled-image { 
    background: url('http://i.stack.imgur.com/jGlzr.png') no-repeat center center; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    border:1px solid green;
}
.text-area{
    background:blue;
}
.width-one {
    width:20%;   
}
.width-two {
    width:80%;
    padding:0 20px;
}

通过使用background size:cover属性和包含元素上的居中固定背景图像,我成功地绕过了站点上的缩放背景图像

技巧是将图像设置为div的背景,并根据可用空间自动调整该div的大小

下面是一个JSFIDLE:

标记应该是这样的:

<div class="hero-unit table">
    <div class="table-row">
        <div class="width-one table-cell scaled-image"></div>
        <div class="width-two table-cell text-area">
            <h2>Text goes in here</h2>
            <p>Text goes in here</p>
        </div>
    </div>
</div>
.table{
    position: relative;
    display:table;
}
.table-row{
    position: relative;
    display:table-row;
}
.table-cell{
    position: relative;
    display:table-cell;
}
.hero-unit{
    width:600px;
    padding:20px;
    background:yellow;
    margin-bottom:20px;
}
.scaled-image { 
    background: url('http://i.stack.imgur.com/jGlzr.png') no-repeat center center; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    border:1px solid green;
}
.text-area{
    background:blue;
}
.width-one {
    width:20%;   
}
.width-two {
    width:80%;
    padding:0 20px;
}

我猜如果图像的宽度增加,它必须将文本向右推,以避免重叠,对吗

但是文本的空间更小,这可能需要额外的一行。因此,图像的高度将增加以匹配文本的新高度,其宽度也将增加以保持纵横比

但是这样的话,文本的空间就更少了,等等


因此,我猜您的布局规则是递归定义的,因此您无法实现它。

我猜如果图像的宽度增加,它必须将文本向右推,以避免重叠,对吗

但是文本的空间更小,这可能需要额外的一行。因此,图像的高度将增加以匹配文本的新高度,其宽度也将增加以保持纵横比

但是这样的话,文本的空间就更少了,等等


因此,我猜您的布局规则是递归定义的,因此您无法实现它。

我在考虑背景,但确实希望找到一种保留图像元素的解决方案。但我认为你是对的,这将是我能得到的最接近的。是的,我连续几天都在努力得到一个与img标签一起工作的东西:(我在考虑背景,但我真的希望找到一个能保留图像元素的解决方案。但我想你是对的,这将是我能得到的最接近的解决方案。是的,我连续几天努力想得到一个能与img标签一起工作的东西:(好的一点——我想它可能是无限的,但我希望它是一个迭代的东西。好的一点——它可能是无限的,我想,但我希望它是一个迭代的东西。