【问题标题】:How to make different sized images responsive?如何使不同大小的图像响应?
【发布时间】:2021-09-11 01:13:14
【问题描述】:

我有两张在桌面上大小相同的图片,但在移动设备上高度不一样。如何在不设置特定高度的情况下在移动设备上保持相同的高度?

请在此处查看屏幕截图:https://imgur.com/a/agz5T2z

这是我的代码 (ReactJS):

HTML:

<div className="container beigeBackground">        
    <h1>Gallery</h1>
    <div className="homeGalleryContainer">
        <img src="https://images.pexels.com/photos/1449773/pexels-photo-1449773.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="Homepage Gallery 1"></img>
        <img src="https://images.pexels.com/photos/6341164/pexels-photo-6341164.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="Homepage Gallery 2"></img>            
    </div>        
    <button>GALLERY</button>
</div>

CSS:

.container {
  padding: 4em;
  background-color: rgb(240, 240, 240);
  display: flex;
  flex-direction: column; 
  align-items: center; 
  justify-content: center;
}
.beigeBackground {
  background-color: var(--Beige); 
  padding: 4em 2em; 
}                            
.beigeBackground * {
  color: white;
}
.homeGalleryContainer {
  display: flex;
  flex-direction: column;
  padding-top: 4em;
  padding-bottom: 3em;
}
.homeGalleryContainer img {
  width: 100%;
  margin-bottom: 2em;
  object-fit: cover;
}
.beigeBackground button {
  border-color: white;
}
.beigeBackground button:hover {
  background-color: white;
  color: var(--Beige);
}

@media screen and (min-width: 768px) {
  .beigeBackground {
    padding: 4em 0;
  }
  .homeGalleryContainer {
    flex-direction: row;
    padding: 4em 0;   
    padding-left: 2em;
      /* to balance out margin-right of each img */
    justify-content: center;
    align-items: initial; 
  }
  .homeGalleryContainer img {
    width: 40%;
    margin-bottom: 0;
    margin-right: 2em;
  }    
}

谢谢。

【问题讨论】:

    标签: css responsive-design responsive responsive-images


    【解决方案1】:

    这将使 img 响应式

    .homeGalleryContainer img {
      max-width: 100%;
      height: auto;
    }
    

    【讨论】:

    • 那行不通。高度还是不一样的。此外,这将允许不同的宽度,这不是我想要的。我希望两张图片的宽度和高度相同。
    猜你喜欢
    • 2018-10-02
    • 2021-07-28
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2020-10-30
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多