【问题标题】:CSS diagonal line separating 2 imagesCSS对角线分隔2个图像
【发布时间】:2021-03-11 11:37:39
【问题描述】:

我的任务是用 html 和 css 重新创建这个模型。在过去的几个小时里,我一直在摆弄试图使用剪辑路径和边框,但我无法实现模型的结果。

给了我2个图片资源,图片不包含文字和按钮。

#index-custom-content {
    height: 40rem;
    padding-bottom: -20%;
}

#index-custom-content div:nth-of-type(1){
    position: relative;
    width: 100%;
}

#index-custom-content div:nth-of-type(2) {
    clip-path: polygon(0 0, 100% 90%, 100% 100%, 0 10%);
    background-color: black;
    z-index: 1000;
    position: relative;
    top: -2em;
}

#index-custom-content div:nth-of-type(3){
    position: relative;
    top: -20%;
    width: 100%;
    z-index: -100;
}
<div id="content">
        <div id="index-custom-content">
            <div><img src="bare-rolig.jpg"></div>
            <div>asd</div>
            <div><img src="bestseller.gif"> </div>
        </div>
    </div>

我的问题是,当给定 2 个大小不等的图像(宽度和高度)时,如何实现分隔 2 个图像的对角线。也可以调整大小,比例正确,线条宽度也保持不变。

非常感谢任何建议或正确方向的观点!提前谢谢!

【问题讨论】:

    标签: html css


    【解决方案1】:

    具有负边距的剪辑路径:

    .first,
    .second{
      --s:100px;
      height:300px;
    }
    
    .first {
      background:red;
      clip-path:polygon(0 0,100% 0,100% 100%,0 calc(100% - var(--s)));
    }
    
    .second {
      background:green;
      /* a little smaller than --s to create a gap */
      margin-top: calc(var(--s) * -1  + 10px);
      clip-path:polygon(0 0,100% var(--s),100% 100%,0 100%);
    }
    <div class="first"></div>
    <div class="second"></div>

    【讨论】:

    • 非常感谢您,这非常有效!但是,您能向我解释一下 --s 是怎么回事吗?
    • @JosephCarpenter 它是一个 CSS 变量,用于避免重复相同的值两次,实际上并不需要。可以手动写入值
    • 由于 margin-top 在某种程度上取决于变量 --s 我建议 margin-top: calc(var(--s) * -1 + 10px);
    • @FabrizioCalderanlovestrees 随意编辑,这是一个 wiki 答案;)
    猜你喜欢
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多