Card with Image Cutout

CSSCSSBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to create a card with an image cutout using HTML and CSS. This lab will teach you how to add a colored background to a container element, create a card with an image and other content, and use the ::before pseudo-element to add a border around the figure element, creating the illusion of a cutout in the card. By the end of this lab, you will have a solid understanding of how to create visually appealing cards with image cutouts.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("`CSS`")) -.-> css/BasicConceptsGroup(["`Basic Concepts`"]) css(("`CSS`")) -.-> css/BasicStylingGroup(["`Basic Styling`"]) css(("`CSS`")) -.-> css/CoreLayoutGroup(["`Core Layout`"]) css(("`CSS`")) -.-> css/AdvancedLayoutGroup(["`Advanced Layout`"]) css(("`CSS`")) -.-> css/IntermediateStylingGroup(["`Intermediate Styling`"]) css(("`CSS`")) -.-> css/DynamicStylingGroup(["`Dynamic Styling`"]) css/BasicConceptsGroup -.-> css/selectors("`Selectors`") css/BasicStylingGroup -.-> css/colors("`Colors`") css/BasicStylingGroup -.-> css/text_styling("`Text Styling`") css/CoreLayoutGroup -.-> css/box_model("`Box Model`") css/CoreLayoutGroup -.-> css/margin_and_padding("`Margin and Padding`") css/CoreLayoutGroup -.-> css/borders("`Borders`") css/CoreLayoutGroup -.-> css/width_and_height("`Width and Height`") css/CoreLayoutGroup -.-> css/display_property("`Display Property`") css/CoreLayoutGroup -.-> css/positioning("`Positioning`") css/AdvancedLayoutGroup -.-> css/flexbox("`Flexbox`") css/IntermediateStylingGroup -.-> css/backgrounds("`Backgrounds`") css/DynamicStylingGroup -.-> css/transformations("`Transformations`") css/IntermediateStylingGroup -.-> css/pseudo_elements("`Pseudo-elements`") subgraph Lab Skills css/selectors -.-> lab-35179{{"`Card with Image Cutout`"}} css/colors -.-> lab-35179{{"`Card with Image Cutout`"}} css/text_styling -.-> lab-35179{{"`Card with Image Cutout`"}} css/box_model -.-> lab-35179{{"`Card with Image Cutout`"}} css/margin_and_padding -.-> lab-35179{{"`Card with Image Cutout`"}} css/borders -.-> lab-35179{{"`Card with Image Cutout`"}} css/width_and_height -.-> lab-35179{{"`Card with Image Cutout`"}} css/display_property -.-> lab-35179{{"`Card with Image Cutout`"}} css/positioning -.-> lab-35179{{"`Card with Image Cutout`"}} css/flexbox -.-> lab-35179{{"`Card with Image Cutout`"}} css/backgrounds -.-> lab-35179{{"`Card with Image Cutout`"}} css/transformations -.-> lab-35179{{"`Card with Image Cutout`"}} css/pseudo_elements -.-> lab-35179{{"`Card with Image Cutout`"}} end

Card With Image Cutout

index.html and style.css have already been provided in the VM.

To create a card with an image cutout, follow these steps:

  1. Add a colored background to a .container element using the background property.
  2. Create a .card element and add a figure element inside it with the desired image and any other content.
  3. Use the ::before pseudo-element to add a border around the figure element. Set the border color to match the .container element's background color to create the illusion of a cutout in the .card.

Here is an example HTML code for the card:

<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>

And here is the corresponding CSS code:

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

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

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

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

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

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

Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

Summary

Congratulations! You have completed the Card With Image Cutout lab. You can practice more labs in LabEx to improve your skills.

Other CSS Tutorials you may like