2017-10-12 25 views
1

我试图创建一个两列的部分,其中:使图像缩小到可用高度不扩大其母公司

  1. 列具有相同的宽度(响应)
  2. 块的高度被定义拉伸至其父项的100%后,左列中包含的图像的高度。
  3. 在右栏中有几个元素,其中之一是包含图像的链接。

我希望与来自上一段的图像的链接缩小它的包含原始图像纵横比的高度,而不会在图像具有纵向方向时伸展它的容器。

不确定它可能与纯CSS。尝试用flexbox和网格布局,但我必须错过一些东西。

我准备了一个小提琴:https://jsfiddle.net/Kuznets/8u6c70ku/3/

* { box-sizing: border-box } 
 
.wrap { max-width: 80%; margin: 0 auto; } 
 
.container { display: flex; } 
 
.left, .right { 
 
    flex: 0 0 50%; 
 
    border: 1px solid black; 
 
} 
 
.left { 
 
    position: relative; 
 
    display: flex; 
 
    align-items: flex-end; 
 
} 
 
.left div.left-text { 
 
    position: absolute; 
 
    color: white; 
 
    padding: 1em; 
 
    font-size: 200%; 
 
} 
 
.should-set-height { 
 
    width: 100%; 
 
} 
 

 
.right { 
 
    display: flex; 
 
    flex-direction: column; 
 
    align-items: center; 
 
    justify-content:space-between; 
 
}
<div class="wrap"> 
 
    <div class="container"> 
 
    <div class="left"> 
 
     <img class="should-set-height" src="https://dummyimage.com/200x240/aaaaaa/ffffff" alt=""> 
 
     <div class="left-text"> 
 
     This is a beautiful slogan 
 
     </div> 
 
    </div><!--/.left--> 
 
    <div class="right"> 
 
     <header>Product title</header> 
 
     <a class="fit-height" href="javascript:void(0)"> 
 
     <img class="should-shrink" src="https://dummyimage.com/200x400/aaaaaa/ffffff"> 
 
     </a> 
 
     <div class="price">$ 19.99</div> 
 
     <button class="button-black">Add to basket</button>  
 
    </div><!--/.right--> 
 
    </div><!--/.containter--> 
 
</div><!--/.wrap-->

回答

0

一个你能做到这一点是有左图像设置为具有width: 100%, height: auto;然后使用背景图片为右容器的方式。

这里有CodePen一个快速演示:https://codepen.io/patriziosotgiu/pen/NaBmZe?editors=1100

您还可以添加额外的规则,例如像min-width用于左栏中,或有这些列适合在一列移动。

注:我假设左图像大于200x240像素

+0

感谢您的输入。这个想法并没有跨越我的想法。看起来我无法在没有JavaScript的情况下实现响应(为移动/桌面加载不同尺寸的图片)。但它是一个选择。 –

+0

不客气。可能有其他方式使用断点。你能给我更多的要求吗?左图像的最大尺寸是多少?比例总是一样的,还是会使用不同的图像尺寸? –