CSS Inset Borders

前端 未结 12 1693
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 15:26

I need to create a solid color inset border. This is the bit of CSS I\'m using:

border: 10px inset rgba(51,153,0,0.65);

Unfortunately that cr

12条回答
  •  误落风尘
    2020-12-02 15:36

    You could use box-shadow, possibly:

    #something {
        background: transparent url(https://i.stack.imgur.com/RL5UH.png) 50% 50% no-repeat;
        min-width: 300px;
        min-height: 300px;
        box-shadow: inset 0 0 10px #0f0;
    }
    

    #something {
      background: transparent url(https://i.stack.imgur.com/RL5UH.png) 50% 50% no-repeat;
      min-width: 300px;
      min-height: 300px;
      box-shadow: inset 0 0 10px #0f0;
    }

    This has the advantage that it will overlay the background-image of the div, but it is, of course, blurred (as you'd expect from the box-shadow property). To build up the density of the shadow you can add additional shadows of course:

    #something {
        background: transparent url(https://i.stack.imgur.com/RL5UH.png) 50% 50% no-repeat;
        min-width: 300px;
        min-height: 300px;
        box-shadow: inset 0 0 20px #0f0, inset 0 0 20px #0f0, inset 0 0 20px #0f0;
    }
    

    #something {
      background: transparent url(https://i.stack.imgur.com/RL5UH.png) 50% 50% no-repeat;
      min-width: 300px;
      min-height: 300px;
      box-shadow: inset 0 0 20px #0f0, inset 0 0 20px #0f0, inset 0 0 20px #0f0;
    }


    Edited because I realised that I'm an idiot, and forgot to offer the simplest solution first, which is using an otherwise-empty child element to apply the borders over the background:

    #something {
      background: transparent url(https://i.stack.imgur.com/RL5UH.png) 50% 50% no-repeat;
      min-width: 300px;
      min-height: 300px;
      padding: 0;
      position: relative;
    }
    #something div {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      border: 10px solid rgba(0, 255, 0, 0.6);
    }


    Edited after @CoryDanielson's comment, below:

    jsfiddle.net/dPcDu/2 you can add a 4th px parameter for the box-shadow that does the spread and will more easily reflect his images.

    #something {
      background: transparent url(https://i.stack.imgur.com/RL5UH.png) 50% 50% no-repeat;
      min-width: 300px;
      min-height: 300px;
      box-shadow: inset 0 0 0 10px rgba(0, 255, 0, 0.5);
    }

提交回复
热议问题