简体   繁体   English

如何显示背景图像的剪辑内容并且只显示边框?

[英]How to show clip content of the background image and only show border?

This codepen https://codepen.io/Chester/pen/QPoyjN here shows how to do border animation.这个codepen https://codepen.io/Chester/pen/QPoyjN这里展示了如何做边框animation。 It works well, however they are using white background and I want to make background of the box dynamic.它工作得很好,但是他们使用white背景,我想让盒子的背景动态化。

<div class="rainbow">
    Rainbow border
</div>

Suppose the div already has some class which sets it's background.假设 div 已经有一些 class 设置它的背景。 How do I make the ::after background use already existing background?如何使::after背景使用已经存在的背景?

Is there any solution to clip background from content and only show ::before background to borders?是否有任何解决方案可以从内容中剪辑背景并且只显示::before背景到边框?

Here's the magic scss code:这是神奇的scss代码:

body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

*, *::before, *::after {
    box-sizing: border-box;
}

@keyframes rotate {
    100% {
        transform: rotate(1turn);
    }
}

.rainbow {
    position: relative;
    z-index: 0;
    width: 400px;
    height: 300px;
    border-radius: 10px;
    overflow: hidden;
    padding: 2rem;
    
    &::before {
        content: '';
        position: absolute;
        z-index: -2;
        left: -50%;
        top: -50%;
        width: 200%;
        height: 200%;
        background-color: #399953;
        background-repeat: no-repeat;
        background-size: 50% 50%, 50% 50%;
        background-position: 0 0, 100% 0, 100% 100%, 0 100%;
        background-image: linear-gradient(#399953, #399953), linear-gradient(#fbb300, #fbb300), linear-gradient(#d53e33, #d53e33), linear-gradient(#377af5, #377af5);
        animation: rotate 4s linear infinite;
    }
    
    &::after {
        content: '';
        position: absolute;
        z-index: -1;
        left: 6px;
        top: 6px;
        width: calc(100% - 12px);
        height: calc(100% - 12px);
        background: white;
        border-radius: 5px;
    }
}

You can use background: inherit;您可以使用background: inherit;

 body { display: flex; justify-content: center; align-items: center; height: 100vh; } *, *::before, *::after { box-sizing: border-box; } @-webkit-keyframes rotate { 100% { transform: rotate(1turn); } } @keyframes rotate { 100% { transform: rotate(1turn); } }.rainbow { position: relative; z-index: 0; width: 400px; height: 300px; border-radius: 10px; overflow: hidden; padding: 2rem; background: pink; }.rainbow::before { content: ""; position: absolute; z-index: -2; left: -50%; top: -50%; width: 200%; height: 200%; background-color: #399953; background-repeat: no-repeat; background-size: 50% 50%, 50% 50%; background-position: 0 0, 100% 0, 100% 100%, 0 100%; background-image: linear-gradient(#399953, #399953), linear-gradient(#fbb300, #fbb300), linear-gradient(#d53e33, #d53e33), linear-gradient(#377af5, #377af5); -webkit-animation: rotate 4s linear infinite; animation: rotate 4s linear infinite; }.rainbow::after { content: ""; position: absolute; z-index: -1; left: 6px; top: 6px; width: calc(100% - 12px); height: calc(100% - 12px); background: inherit; border-radius: 5px; }
 <div class="rainbow"> Rainbow border </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM