【问题标题】:height and width property are not applying to html img element高度和宽度属性不适用于 html img 元素
【发布时间】:2020-07-29 13:12:59
【问题描述】:

我正在构建一个完整的堆栈、服务器端渲染的 React 电子商务应用程序。然而,由于 css 是我的弱点,我尝试构建产品卡而不是使用库模板。但是问题是我无法弄清楚如何让img 元素覆盖 div。当我放置 heightwidth 属性时,img 消失了。对你们中的许多人来说,这可能是一个简单的问题,但我想不通:(

这里是 react 组件代码:

<div className="card-body ">
          <div className="card-img-container">
            <img src={imageUrl} alt={name} className="card-img" />
          </div>


<div className="card-footer">
          <span className="name">{name}</span>
          <span>
            <div className="price-and-dollar">
              <span className="dollar-sign">$</span>
              <span className="price"> {price}</span>
            </div>
          </span>
        </div>
</div>

这是css:

.collection-item {
  background-color: $gray-700;
  display: flex;
  flex-direction: column;
  width: 23vw;
  .card-body {
    width: 94%;
  }
}
.card-img-container {
  height: 100%;
  width: 100%;
}
.card-img {
  height: 100%;
  width: 100%;
}

这是结果

【问题讨论】:

  • 你想让这个购物车图标覆盖整张卡片吗?还是上半部分?直到它最正确?

标签: css reactjs sass


【解决方案1】:
.card-img-container {
  height: 100% !important;
  width: 100% !important;
}
.card-img {
  height: 100% !important;
  width: 100% !important;
}

试试这个希望对你有帮助

【讨论】:

  • 你必须检查依赖。我认为你的下一个 css 将覆盖当前的一个
【解决方案2】:

你可以试试

.card-img-container {
  height: 100%;
  width: 100%;
}

div.card-img-container > img {
 height: 100%;
 width: 100%;
 }

【讨论】:

  • 还是一样 :(
猜你喜欢
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2022-11-04
  • 2014-01-18
  • 2015-11-02
  • 2019-07-02
  • 2019-05-03
  • 1970-01-01
相关资源
最近更新 更多