需要背景图时如何用CSS切角?

How to cut a corner with CSS when background image is necessary?

以上是我正在处理的项目的图像。这是我得到的结果:

创建盒子非常简单;然而,现在我不知道如何在左下角创建这个切角。我已经尝试了很多东西,如果背景不是透明的而是一块颜色,大多数事情都会起作用。由于背景需要是这张图片,如果不让一面显示某种颜色,我就无法进行切角。这是我的代码:

<div class="profile">
    // HTML content
</div>

<style>
   profile {
   border: 2px solid #fff;
   color: #fff;
   height: 100%;
   padding: 10px;
   width: 250px;
</style>

我已经尝试了很多东西,比如这里的这个(不是我使用的确切代码,但我遵循了这个例子):

.cut {
  border: none;
  position: relative;
}

.cut:before {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  border-bottom: 20px solid lightgrey;
  border-left: 20px solid #e67e22;
  width: 0;
}

这创建了一个切角,但有一块纯色,我需要显示图像,而不是颜色。

有人知道怎么做吗?非常感谢您的建议。谢谢!

您可以使用 before/after 元素制作底部,如下所示:

.profile {
position:relative;
display:inline-block;
margin:50px;
border:1px solid #000;
border-bottom:none;
width:100px;
height:200px;
background:#ccc;
}
.profile:after {
content:" ";
position:absolute;
border:1px solid #000;
height:20px;
width:80px;
bottom:-20px;
right:-1px;
border-top:0;
border-left:0;
background:#ccc;
}
.profile:before {
content:" ";
position:absolute;
border-bottom:1px solid #000;
height:29px;
width:29px;
transform:rotate(45deg);
bottom:-15px;
left:6px;
background:#ccc;
}
<div class="profile"></div>

底部分为两部分:只有两个边框的矩形+一个边框旋转45°的正方形

希望对您有所帮助

注意:更改尺寸时要小心

.profile {
  position: relative;
  display: inline-block;
  width: 200px;
  height: 200px;
  border-top: 2px solid #000;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  padding: 20px;
  box-sizing: border-box;
  font-family: Arial, sans-serif;
}

.profile h2 {
  margin: 0 0 10px;
}

.profile p {
  font-size: 14px;
}

.profile .bottom {
  position: absolute;
  bottom: -30px;
  right: -2px;
  width: 180px;
  height: 30px;
  border-bottom: 2px solid #000;
  box-sizing: border-box;
  border-right: 2px solid #000;
}

.profile .bottom::before {
  content: '';
  position: absolute;
  left: -10px;
  bottom: -4px;
  width: 2px;
  height: 35px;
  background-color: #000;
  transform: rotate(-35deg);
}
<div class="profile">
  <h2>Name</h2>
  <p>Description</p>
  <div class="bottom"></div>
</div>

我认为您正在尝试剪切图像的一角而不是 div,因此您可以这样做:

body {
    background: url('https://www.lunapic.com/editor/premade/o-paint-bucket.gif');
}

.container {
    display: inline-block;
    width: 200px;
    height: 300px;
    overflow: hidden;
}

.container .image_container {
    width: 320px;
    height: 550px;
    overflow: hidden;
    display: block;
    position: relative;
    transform: rotate(45deg);
    margin-left: calc(260px - 360px);
    margin-top: -40px;
}

.container .image_container .image {
    transform: rotate(-45deg);
    margin-top: 10px;
    margin-left: -10px;
}
<div class="container">
  <div class="image_container">
    <div class="image">
      <img src="https://www.w3schools.com/css/img_fjords.jpg" />
    </div>
  </div>
</div>