简体   繁体   English

Neat 2.0中的响应式图像

[英]Responsive Image in Neat 2.0

Is there a recommended way to get an image to be 100% of the height of a column in Neat based on the size that the text makes the column? 是否存在一种建议的方法,使文本基于整列的大小,使其成为Neat中一列的高度的100%? I've attached an example of what I'm trying to accomplish below. 我在下面附上了我要完成的示例。

I'm trying to make the image fill the 1/3 column it is placed in with object-fit: cover , the problem is that because the height of the container isn't set, it doesn't know what to fit the image to. 我正在尝试使用object-fit: cover将图像填充到放置它的1/3列中object-fit: cover ,问题在于,由于未设置容器的高度,因此它不知道适合图像的大小至。

Image With No Height Set 没有设置高度的图像

If I put the image in a div with display: flex; 如果我将图像放在带有display: flex;的div中display: flex; and height:100% , the height gets set to the height of the image, when I want it to get set to the height of the parent, which is the grid-container that has the two columns in it. height:100% ,将高度设置为图像的高度,当我希望将其设置为父级的高度时,父级是其中包含两列的网格容器。

Image With 100% Height Set 高度设置为100%的图像

What I'm trying to accomplish is have a responsive image that constantly fills the space of its column to match the height of the text in the next column. 我要完成的工作是提供一个自适应图像,该图像不断填充其列的空间以匹配下一列中的文本高度。

I should note that I'm trying to accomplish this without setting a specific height for the parent container such as height:200px , I want the height to be fluid to the content in it. 我应该注意,我试图在不为父容器设置特定高度的情况下完成此操作,例如height:200px ,我希望该高度能够适应其中的内容。

HTML Code: HTML代码:

<section class="image-block" style="background: #5777e0; color:#fff;">
  <div class="one-thirds-column-full no-padding third-image-box">
    <div class="flex">
      <img src="https://unsplash.it/1600/1200?image=148" />
    </div>
  </div>
  <div class="two-thirds-column-full">
    <div class="text">
      <h2>1/3 Image on Left with 2/3 Text on Right</h2>
      <p>
        The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's
        keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.
      </p>
    </div>
  </div>
</section>

CSS: CSS:

section {
  @include grid-container;

  @include grid-media($neat-small) {
       @include grid-container($neat-small);
  }
  @include grid-media($neat-medium) {
       @include grid-container($neat-medium);
  }
  @include grid-media($neat-large) {
       @include grid-container($neat-large);
  }
  @include grid-media($neat-larger) {
       @include grid-container($neat-larger);
  }
  @include grid-media($neat-super) {
       @include grid-container($neat-super);
  }
}

.one-thirds-column-full {
    @include grid-column(1, $neat-grid-full);
    padding:1em;

    @include grid-media($neat-medium-full) {
         @include grid-column(2);
    }
    @include grid-media($neat-large-full) {
         @include grid-column(4);
    }
}

.two-thirds-column-full {
    @include grid-column(1, $neat-grid-full);
    padding:1em;
    @include grid-media($neat-small-full) {
         @include grid-column(2);
    }
    @include grid-media($neat-medium-full) {
         @include grid-column(4);
    }
    @include grid-media($neat-large-full) {
         @include grid-column(8);
    }
}

.image-block{
  background: #fff;
  text-align: left;

  .third-image-box{
    height:auto;
  }

  .half-image-box{
    height:auto;
  }

  .flex{
    display:flex;
    height:100%;
  }

  img{
    height:auto !important;
    object-fit: cover;
  }
}
.text{
  max-width:$max-text-width;
}
  1. There is a bug in the documentation and grid-container does not require or need access to the grid so you can shorten the first part to ⤵ 文档中存在错误,并且grid-container不需要或不需要访问网格,因此可以将第一部分缩短为⤵

     section { @include grid-container; 
  2. I don't think you need that much markup in the html around the img. 我认为您不需要在img周围的html中添加太多标记。 You may be able to just do 您也许可以做到

     <section class="image-block"> <img src="https://unsplash.it/1600/1200?image=148" class="one-thirds-column-full"> <div class="two-thirds-column-full"> … </div> </section> 

    and then the css would be like… 然后,css就像...

     .image-block{ align-items: stretch; display: flex; } 

align-items: stretch will cause both objects to fill the vertical space. align-items: stretch将导致两个对象都填充垂直空间。 The rest is up to object-fit: cover , etc 其余的取决于object-fit: cover

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

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