Transparent Border showing background image

后端 未结 2 2017
心在旅途
心在旅途 2020-12-11 23:37

I have this:

\"Solid

I want to achieve this:

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 00:14

    You can achieve the transparent border showing background image using a pseudo element.

    The red background is the border of the pseudo element and the transparent border is created by the gap between the element's background and the pseudo element's border:

    DEMO :

    body{
        background:url('https://farm4.staticflickr.com/3771/13199704015_72aa535bd7.jpg');
        background-size:cover;
    }
    .big{
        margin:50px;
        padding:50px;
        min-height:500px;
        overflow:hidden;
    }
    .big p{
        position:relative;
        z-index:1;
    }
    .small{
        position:relative;
        background:teal;
        width:150px;height:150px;
        margin:25px;
        z-index:0;
    }
    .small:before{
        content:'';
        position:absolute;
        top:-5025px; left:-5025px;
        width:200px; height:200px;
        border:5000px solid rgba(255,255,255,0.8);
        background:none;
    }

    content here

    content here

    output:

    You can also use box-shadow instead of border so you don't have to use negative values for the top/left positioning of the pseudo element. Browser support isn't as good as border though :

    body{
        background:url('https://farm4.staticflickr.com/3771/13199704015_72aa535bd7.jpg');
        background-size:cover;
    }
    .big{
        margin:50px;
        padding:50px;
        min-height:500px;
        overflow:hidden;
    }
    .big p{
        position:relative;
        z-index:1;
    }
    .small{
        position:relative;
        background:teal;
        width:150px;height:150px;
        margin:25px;
        z-index:0;
    }
    .small:before{
        content:'';
        position:absolute;
        top:-25px; left:-25px;
        width:200px; height:200px;
        box-shadow: 0px 0px 0px 5000px rgba(255,255,255,0.8);
        background:none;
    }

    content here

    content here

提交回复
热议问题