CSS Inset Borders

前端 未结 12 1643
没有蜡笔的小新
没有蜡笔的小新 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;
    }
    <div id="something"></div>

    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;
    }
    <div id="something"></div>


    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);
    }
    <div id="something">
      <div></div>
    </div>


    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);
    }
    <div id="something"></div>

    0 讨论(0)
  • 2020-12-02 15:37

    To produce a border inset within an element the only solution I've found (and I've tried all the suggestions in this thread to no avail) is to use a pseudo-element such as :before

    E.g.

    .has-inset-border:before {
      content: "foo"; /* you need something or it will be invisible at least on Chrome */
      color: transparent;
      position: absolute;
      left: 10px;
      right: 10px;
      top: 10px;
      bottom: 10px;
      border: 4px dashed red;
    }
    

    The box-sizing property won't work, as the border always ends up outside everything.

    The box-shadow options has the dual disadvantages of not really working and not being supported as widely (and costing more CPU cycles to render, if you care).

    0 讨论(0)
  • 2020-12-02 15:38

    If you want to make sure the border is on the inside of your element, you can use

    box-sizing:border-box;

    this will place the following border on the inside of the element:

    border: 10px solid black;

    (similar result you'd get using the additonal parameter inset on box-shadow, but instead this one is for the real border and you can still use your shadow for something else.)

    Note to another answer above: as soon as you use any inset on box-shadow of a certain element, you are limited to a maximum of 2 box-shadows on that element and would require a wrapper div for further shadowing.

    Both solutions should as well get you rid of the undesired 3D effects. Also note both solutions are stackable (see the example I've added in 2018)

    .example-border {
      width:100px;
      height:100px;
      border:40px solid blue;
      box-sizing:border-box;
      float:left;
    }
    
    .example-shadow {
      width:100px;
      height:100px;
      float:left;
      margin-left:20px;
      box-shadow:0 0 0 40px green inset;
    }
    
    .example-combined {
      width:100px;
      height:100px;
      float:left;
      margin-left:20px;
      border:20px solid orange;
      box-sizing:border-box;
      box-shadow:0 0 0 20px red inset;
    }
    <div class="example-border"></div>
    <div class="example-shadow"></div>
    <div class="example-combined"></div>

    0 讨论(0)
  • 2020-12-02 15:38

    Simple SCSS solution with pseudo-elements

    Live demo: https://codepen.io/vlasterx/pen/xaMgag

    // Change border size here
    $border-width: 5px;
    
    .element-with-border {
    	display: flex;
    	height: 100px;
    	width: 100%;
    	position: relative;
    	background-color: #f2f2f2;
    	box-sizing: border-box;
    	
    	// Use pseudo-element to create inset border
    	&:before {
    		position: absolute;
    		content: ' ';
    		display: flex;
    		border: $border-width solid black;
    		left: 0;
    		right: 0;
    		top: 0;
    		bottom: 0;
    		border: $border-width solid black;
    		// Important: We must deduct border size from width and height
    		width: calc(100% - $border-width); 
    		height: calc(100% - $border-width);
    	}
    }
    <div class="element-with-border">
      Lorem ipsum dolor sit amet
    </div>

    0 讨论(0)
  • 2020-12-02 15:38

    If box-sizing is not an option, another way to do this is just to make it a child of the sized element.

    Demo

    CSS

    .box {
      width: 100px;
      height: 100px;
      display: inline-block;
      margin-right: 5px;
    }
    .border {
      border: 1px solid;
      display: block;
    }
    .medium { border-width: 10px; }
    .large  { border-width: 25px; }
    


    HTML

    <div class="box">
      <div class="border small">A</div>
    </div>
    <div class="box">
      <div class="border medium">B</div>
    </div>
    <div class="box">
      <div class="border large">C</div>
    </div>
    
    0 讨论(0)
  • 2020-12-02 15:42

    You may use background-clip: border-box;

    Example:

    .example {
    padding: 2em;
    border: 10px solid rgba(51,153,0,0.65);
    background-clip: border-box;
    background-color: yellow;
    }
    
    <div class="example">Example with background-clip: border-box;</div>
    
    0 讨论(0)
提交回复
热议问题