You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅使用CSS创建附图指定格式的div元素

实现方案

你可以通过以下两种纯CSS方案实现对角斜切的卡片样式,可根据兼容性需求选择:

方案1:clip-path实现(写法最简单,现代浏览器兼容性好)

HTML结构

<div class="cut-card">卡片内容</div>

CSS代码

.cut-card {
  width: 300px;
  padding: 20px;
  background: #2d3748;
  color: #fff;
  /* 调整下方的12px即可修改斜切的大小 */
  clip-path: polygon(12px 0, 100% 0, 100% calc(100% - 12px), calc(100% - 12px) 100%, 0 100%, 0 12px);
}

方案2:渐变实现(兼容旧版本浏览器,支持添加边框效果)

基础无边框样式

.cut-card {
  width: 300px;
  padding: 20px;
  color: #fff;
  background:
    linear-gradient(135deg, transparent 12px, #2d3748 0) top left,
    linear-gradient(-45deg, transparent 12px, #2d3748 0) bottom right;
  background-size: 100% 50%;
  background-repeat: no-repeat;
}

带边框样式

.cut-card {
  width: 300px;
  padding: 20px;
  color: #2d3748;
  border: 2px solid #3182ce;
  border-top: none;
  border-bottom: none;
  background:
    /* 边框色渐变 */
    linear-gradient(135deg, transparent 12px, #3182ce 0, #3182ce calc(12px + 2px), #fff 0) top left,
    linear-gradient(-45deg, transparent 12px, #3182ce 0, #3182ce calc(12px + 2px), #fff 0) bottom right;
  background-size: 100% 50%;
  background-repeat: no-repeat;
}

所有代码中的斜切尺寸、颜色、宽高、内边距都可以根据你的实际需求自行调整。

内容的提问来源于stack exchange,提问作者v1t0rs0uz4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 09:15:04