无法使图像达到我想要的宽度

  • 本文关键字:我想要 图像 css reactjs
  • 更新时间 :
  • 英文 :


我希望卡中的图像宽度为855px,但我也希望它在屏幕挤压时自动调整到较小的东西。我已经尝试了flex Grow/Flex-Basis/Align-Items的不同组合:strech/...等适用于不同的类,但似乎没有任何作用。

我用一个例子制作了沙盒。

https://codesandbox.io/s/adoring-thompson-2itst

代码:

import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
  return (
    <div className="App">
      <div className="Page">
        <div>
          <div className="Page--top">
            <p>
              Nullam nec imperdiet lorem, at imperdiet nulla. Pellentesque
              habitant morbi tristique senectus et netus et malesuada fames ac
              turpis egestas.
            </p>
          </div>
          <div className="Page--bottom">
            <div className="Page--left">
              <div className="Filters">
                <p>abcd1111</p>
                <p>abcd2222</p>
                <p>abcd3333</p>
              </div>
            </div>
            <div className="Page--right">
              <div className="Sort">
                <p>1111</p>
                <p>2222</p>
                <p>3333</p>
                <p>4444</p>
              </div>
              {new Array(4).fill(0).map(x => (
                <div className="Card--container">
                  <img
                    src="https://i.imgur.com/2TX2rmL.jpg"
                    alt="card pic"
                    className="Card--image"
                  />
                  <div className="Card--info">
                    <h1>abcde</h1>
                    <h2>12345</h2>
                  </div>
                  <button type="button" className="Card--button">
                    xyz123
                  </button>
                </div>
              ))}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
html {
  height: 100%;
  width: 100%;
}
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: "Montserrat", sans-serif, -apple-system, BlinkMacSystemFont,
    "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans",
    "Droid Sans", "Helvetica Neue";
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
#root {
  height: 100%;
  width: 100%;
}
.App {
  box-sizing: border-box;
  /* min-width: 100vw; */
  min-height: 100vh;
  background-color: #1e1e1e;
  display: flex;
  flex-direction: column;
}
.Page {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  background-color: rgb(219, 219, 219);
  padding: 50px 90px;
}
.Page > * {
  background-color: red;
}
.Page--bottom {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: flex-start;
  flex: 1 1 auto;
}
.Page--top,
.Page--left,
.Page--right {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.Page--left {
  flex: 1 1 auto;
}
.Page--right {
  align-items: center;
  align-items: stretch;
}
.Page--right > *:not(:last-child) {
  margin-bottom: 30px;
}
.Page--top > * {
  margin-bottom: 15px;
}
.Page--left > *:first-child {
  margin-bottom: 30px;
}
.Page--left {
  margin-right: 50px;
}
.Card--container {
  box-sizing: border-box;
  /* flex-basis: 855px; */
  width: 100%;
  max-width: 855px;
  max-height: 435px;
  background-color: blue;
  display: grid;
  grid-template-rows: 3fr 1fr;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  grid-template-areas:
    "image image image image image"
    "info info info button button";
}
.Card--image {
  grid-area: image;
  height: 100%;
  max-width: 100%;
  object-fit: cover;
}
.Card--info {
  grid-area: info;
  padding: 16px 32px;
  align-self: center;
}
.Card--info > * {
  color: white;
}
.Card--button {
  grid-area: button;
  justify-self: center;
  align-self: center;
}
.Filters {
  width: 315px;
  background-color: white;
  box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
  padding: 30px;
}
.Filters > *:not(:last-child) {
  padding-bottom: 15px;
  border-bottom: 2px solid #ebe8e8;
}
.Filters > *:not(:first-child) {
  padding-top: 15px;
}
.Sort {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  align-self: flex-start;
  background-color: white;
  width: 100%;
  padding: 15px 30px;
  box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
}
.Sort > *:not(:last-child) {
  margin-right: 90px;
}
.Sort > *:not(:first-child) {
  cursor: pointer;
}

您可以将其添加到CSS:

img {
    object-fit: contain;
}

相关内容

最新更新