30秒学会 CSS 片段 · 2023年12月20日

30秒学会 CSS 片段 – Card with image cutout

Creates a card with an image cutout.

  • Use background to add a colored background to a .container element.
  • Create a .card containing a figure with the appropriate image for the cutout and any other content you want.
  • Use the ::before pseudo-element to add a border around the figure element, matching the .container element’s background and creating the illusion of a cutout in the .card.

预览


Lorem ipsum dolor sit amet consectetur adipisicing elit.



HTML

<div class="container">
  <div class="card">
    <figure>
      <img alt="" src="https://picsum.photos/id/447/400/400"/>
    </figure>
    <p class="content">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
  </div>
</div>

CSS

.container {
  display: flex;
  padding: 96px 24px 48px;
  justify-content: center;
  align-items: center;
  background: #f3f1fe;
}

.card {
  width: 350px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #fff;
  border-radius: 10px;
  margin: 8px;
  box-shadow: 0 0 5px -2px rgba(0, 0, 0, 0.1);
}

.card figure {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin-top: -60px;
  position: relative;
}

.card figure::before {
  content: "";
  border-radius: inherit;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  transform: translate(-50%, -50%);
  border: 1rem solid #f3f1fe;
  box-shadow: 0 1px rgba(0, 0, 0, 0.1);
}

.card figure img {
  border-radius: inherit;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card .content {
  text-align: center;
  margin: 2rem;
  line-height: 1.5;
  color: #101010;
}

翻译自:https://www.30secondsofcode.org/css/s/card-image-cutout