css 无法在所有设备中正确显示内容

31moq8wy  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(133)

我正在做一个汽车研究/排名网站使用React。来的问题,我有3张卡,内容上,一旦你悬停在它。
它在桌面上运行得非常好,但在较小的设备上不能正确显示内容:它溢出到另一部分。
为了解决这个问题,我添加了一个媒体查询,但它也不工作。
请检查我的代码:

.sec2 {
  height: 100vh;
  width: 100%;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  background: linear-gradient(to left, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)),
    url(https://static.feber.se/article_images/48/81/62/488162_1920.jpg)
      no-repeat top center;
}


.wrapper {
  display: flex;
  width: 90%;
  justify-content: space-around;
}

.card {
  width: 350px;
  height: 370px;
  border-radius: 15px;
  padding: 1.5rem;
  position: relative;
  object-fit: fill;
  display: flex;
  align-items: flex-end;
  transition: 0.4s ease-out;
  box-shadow: 0px 7px 10px rgba(0, 0, 0, 0.5);
}
.card:hover {
  transform: translateY(20px);
}
.card:hover:before {
  opacity: 1;
}
.card:hover .info {
  opacity: 1;
  transform: translateY(0px);
}
.card:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 15px;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2;
  transition: 0.5s;
  opacity: 0;
}
.card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 15px;
}
.card .info {
  position: relative;
  z-index: 3;
  color: white;
  opacity: 0;
  transform: translateY(30px);
  transition: 0.5s;
}
.card .info h1 {
  margin: 0px;
  font-size: 2rem;
}
.card .info p {
  letter-spacing: 1px;
  font-size: 15px;
  margin-top: 8px;
}
.card .info button {
  padding: 0.6rem;
  outline: none;
  border: none;
  border-radius: 3px;
  background: white;
  color: black;
  font-weight: bold;
  cursor: pointer;
  transition: 0.4s ease;
}
.card .info button:hover {
  background: dodgerblue;
  color: white;
}

@media only screen and (min-width: 600px) and (max-width: 768px) {
  .wrapper {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .card {
    height: 270px;
    padding: 1rem;
    width: 250px;
    margin-bottom: 10px;
  }

  .card .info h1 {
    font-size: 1rem;
  }

  .card .info p {
    font-size: 15px;
  }

  .card .info button {
    padding: 0.4rem;
    font-weight: normal;
    font-size: 12px;
  }
}

@media (max-width: 600px) {
  .wrapper {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .card {
    height: 15%;
    padding: 0.3rem;
    width: 180px;
    margin-bottom: 5px;
  }

  .card .info h1 {
    font-size: 1rem;
  }

  .card .info p {
    font-size: 10px;
  }

  .card .info button {
    padding: 0.3rem;
    font-weight: normal;
    font-size: 10px;
  }
}

还有

<div>
          <header className="sec2" id="section2">
            <h1>
              What is in this website?
            </h1>
            <div class="wrapper">
              <div class="card">
                <img src="https://cdn.jdpower.com/JDPA_2022%20Genesis%20G70%20Red%20Front%20Quarter%20View.jpg" />
                <div class="info">
                  <h1>Car Rankings</h1>
                  <p>
                    It contains cars ranked based on body type, done after
                    comprehensive research.
                  </p>
                  <button>Go</button>
                </div>
              </div>
              <div class="card">
                <img src="https://i0.wp.com/autonxt.net/wp-content/uploads/2018/01/autocontentexp.com2018-Lincoln-Navigator5-5b1aebecc618f268352b037fb2253a291d670994-1.jpg?resize=2500%2C1500&ssl=1" />
                <div class="info">
                  <h1>Car Reviews</h1>
                  <p>Contains reviews of selective cars written by us.</p>
                  <button>Read More</button>
                </div>
              </div>
              <div class="card">
                <img src="https://static0.hotcarsimages.com/wordpress/wp-content/uploads/2020/09/AWD-e1600115646672.jpg" />
                <div class="info">
                  <h1>Used Cars/h1>
                  <p> We rank used cars so that you will only get the best out of it.

                   
                  </p>
                  <button>Read More</button>
                </div>
              </div>
            </div>
          </header>
</div>

计算机中的图像:click,以及较小设备中的映像:click

lvmkulzt

lvmkulzt1#

  • 首先将margin: auto添加到.wrapper类中,并将一些margin: 5px;添加到.card类中,因为这会使内容位于中心,并且看起来很好。
.wrapper {
  display: flex;
  width: 90%;
  margin: auto;
  justify-content: space-around;
}
.card {
  /* your code */
  margin: 5px;
}
  • 现在,添加仅更新所有.cardmargin: 15px;,而不是margin-bottom

下面是完整的代码https://codesandbox.io/s/laughing-gwen-4i47v5?file=/src/Home.jsx

相关问题