Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Tạo testimonial (nhận xét khách hàng) responsive với CSS


Testimonial responsive: avatar tròn, tên, nội dung. Trên mobile: xếp dọc, căn giữa; Desktop: avatar float left.

Ví dụ

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <style>
    body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
    }
    .testimonialContainer {
      border: 2px solid rgba(0, 0, 0, 0.363);
      background-color: rgb(207, 235, 218);
      border-radius: 5px;
      padding: 16px;
      margin: 16px 0;
    }
    .profilePic {
      width: 100px;
      height: 100px;
      object-fit: cover;
    }
    .testimonialContainer::after {
      content: "";
      clear: both;
      display: table;
    }
    .testimonialContainer img {
      float: left;
      margin-right: 20px;
      border-radius: 50%;
    }
    .testimonialContainer span {
      font-size: 24px;
      font-weight: 500;
      margin-right: 15px;
      color: purple;
    }
    .testimonialContainer p {
      font-size: 18px;
      font-style: oblique;
    }
    @media (max-width: 500px) {
      .testimonialContainer { text-align: center; }
      .testimonialContainer img {
        margin: 0 auto 15px;
        float: none;
        display: block;
      }
    }
  </style>
</head>
<body>
  <h1>Responsive Testimonials Example</h1>
  <div class="testimonialContainer">
    <img class="profilePic" src="https://images.pexels.com/photos/614810/pexels-photo614810.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/>
    <span>James Anderson</span>
    <p>Shawn is a hard working and committed individual</p>
  </div>
  <div class="testimonialContainer">
    <img class="profilePic" src="https://images.pexels.com/photos/2128807/pexels-photo2128807.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/>
    <span>Steve Boulder</span>
    <p>Shawn is great at managing people</p>
  </div>
</body>
</html>

Kết quả

  • Desktop: Avatar bên trái, tên/nội dung bên phải
  • Mobile (≤500px): Avatar ở trên căn giữa, tên/nội dung dưới đó

Kỹ thuật chính

  • ::after { clear: both } clear float
  • @media (max-width: 500px) breakpoint mobile
  • float: none; display: block; margin: auto căn giữa ảnh trên mobile
  • object-fit: cover giữ tỷ lệ avatar