在带有文本的div中缩放图像

时间:2019-01-07 14:22:37

标签: css angular sass flexbox angular-material

对于具有Angular,Angular Material和Flex-Layout的个人项目,我正在尝试实现Bring! App使用的相似布局: enter image description here

具有不同大小的图像(不是全部平方)我想按比例将它们居中,并在它们下面允许一些文本。 我有以下模板和scss样式:

<div fxLayout="row" fxLayoutGap="5px" class="cards-container">
 <div class="item-card">
   <div class="image">
     <img src="../../../../assets/icons/apple.png" alt="Mela" />
   </div>
   <span class="item-name">Mela</span>
   <span class="description">12</span>
 </div>
 <div class="item-card">
  <div class="image">
     <img src="../../../../assets/icons/milk.png" alt="Latte" />
  </div>
  <span class="item-name">Latte</span>
  <span class="description">1 description comes here, must be hidden if long text</span>
 </div>
</div>

//---------------------------------------------------

.cards-container {
  flex-wrap: wrap;

  .item-card {
    display: flex;
    flex-direction: column;
    justify-items: end;
    color: white;
    width: 7em;
    height: 7em;
    text-align: center;
    background-color: darkslategray;
    margin: 5px 0;
    img {
        width: 40%; // TODO: how to scale?
        height: 40%;
    }

    .text-container {
        display: flex;
        flex-direction: column;

        .item-name {
            display: inline-block;
            font-size: 1.1em;
        }

        .description {
            width: 99%;
            font-size: 0.8em;
            text-align: center;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            display: inline-block;
        }
    }
  }
}

但是,图像不会按比例缩小以与其他图像保持比例,特别是在窄而长的情况下。

3 个答案:

答案 0 :(得分:0)

您尝试过吗:

   img {
        width: 40%; 
        height: auto;
    }

应该使图像比例保持一致。

答案 1 :(得分:0)

一切似乎都很好,但是您的HTML代码似乎缺少了.text-container div和类。

<div class="item-card">
   <div class="image">
     <img src="../../../../assets/icons/apple.png" alt="Mela" />
   </div>
   <span class="item-name">Mela</span>
   <span class="description">12</span>
 </div>

应该是

<div class="item-card">
   <div class="image">
     <img src="../../../../assets/icons/apple.png" alt="Mela" />
   </div>
   <div class="text-container">
     <span class="item-name">Mela</span>
     <span class="description">12</span>
   </div>
 </div>

现在text-overflow: ellipsis;不能在多行上使用,除非您实现一些JavaScript或其他功能。

如果您的代码有任何更改,我会将图像设置为background-image。没有其他方法,可能还有其他方法,但这是我用来确保容器响应的图像始终处于响应状态并居中的方式。

例如:https://codepen.io/StudioKonKon/pen/wRjOzr(包括SCSS)

.image {
  background-position: center center;
  background-size: contain;
  background-repeat: no-repeat;
  font-size: 0;
}
.image-mela {
  background-image: url("https://via.placeholder.com/150x175");
}
.image-latte {
  background-image: url("https://via.placeholder.com/200x50");
}
.image-long {
  background-image: url("https://via.placeholder.com/50x100");
}

.cards-container {
  width: 100%;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
}
.cards-container .item-card {
  display: flex;
  flex-direction: column;
  justify-items: end;
  color: white;
  width: 7em;
  height: 7em;
  text-align: center;
  background-color: darkslategray;
  margin: 5px;
  padding: 0.5em;
  box-sizing: border-box;
  overflow: hidden;
}
.cards-container .item-card .image {
  display: block;
  margin: auto;
  width: 40%;
  height: 40%;
}
.cards-container .item-card .text-container {
  display: flex;
  flex-direction: column;
}
.cards-container .item-card .text-container .item-name {
  display: inline-block;
  font-size: 1.1em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cards-container .item-card .text-container .description {
  font-size: 0.8em;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
}
<div fxLayout="row" fxLayoutGap="5px" class="cards-container">
  <div class="item-card">
    <div class="image image-mela">Mela</div>
    <div class="text-container">
      <span class="item-name">Mela</span>
      <span class="description">12</span>
    </div>
  </div>
  <div class="item-card">
    <div class="image image-latte">Latte</div>
    <div class="text-container">
      <span class="item-name">Latte</span>
      <span class="description">1 description comes here, must be hidden if long text</span>
    </div>
  </div>
  <div class="item-card">
    <div class="image image-long">Long Image</div>
    <div class="text-container">
      <span class="item-name">Long Image Text</span>
      <span class="description">must be hidden if long text</span>
    </div>
  </div>
</div>

答案 2 :(得分:0)

很简单。我认为它将解决问题。 :)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>item</title>
    <style type="text/css" media="screen">
    .item-single{
        width: 100px;
        background-color: #e7e7e7;
        height: 100px;
        text-align: center;
    }
    .item-single span{
        display: block;
    border: 1px solid #000; /* just to show the alignment  */
    }
    .item-single img{
        width: 50%; /*  you can scale it down using width */
    border:1px solid #000; /* just to show the alignment */
        display: block;
        margin: auto;
    }
</style>
</head>
<body>
    <div class="item-single">
        <img src="http://sugamparajuli.com.np/wp-content/uploads/2019/01/banana.png">
        <span class="item-name">Mela</span>
        <span class="description">12</span>
    </div>
</body>
</html>

这是结果。 item-image