Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Borders: Distance from Object Edge?

Quick question. I was writing out some code and was curious if there is a way to add a border on a div that is 5px within the object - as in not on the actual edge of the div. I checked out WC3 and didn't see any specs - but I may have missed it.

In my case I'd be using a dashed border 5px inside the div, to create an effect like the div had been sewn to the rest of the site. I can do it fairly easily with background-image but why add KB when a line or two of css could do it.

I would assume it would be something like "border-position" or "border-distance".

Thanks in advance.

like image 648
Will D. White Avatar asked Feb 21 '11 16:02

Will D. White


People also ask

How do you cut the edges of a border in CSS?

Style the cut corner with the ::before pseudo-element and use the content property required while using the :: before pseudo-element to generate and insert content. Set the position to "absolute" and add the top and right, border-top and border-right properties.

How do you put space between border and content?

To create extra space between elements on the page CSS uses two properties, the padding and margin properties. The picture below shows what padding and margin do. Padding is the distance between the element's content area and any border that might be applied to the element. Margin is extra space around the element.

How do you put a space between borders in CSS?

The border-spacing CSS property sets the distance between the borders of adjacent cells in a <table> . This property applies only when border-collapse is separate .

What is border-radius in CSS?

The border-radius CSS property rounds the corners of an element's outer border edge. You can set a single radius to make circular corners, or two radii to make elliptical corners.


2 Answers

I've never come across any property that resembles this, so I'd have to say, simply, 'no.'

But then I'd feel bad for that, so all I could really suggest is wrapping the div you wish to 'sew on' within another div and styling the parent with the same background-color to emulate the look you're after. Here's some css for a possible take:

.wrap {
    border-width: 0;
    background-color: #ffa;
    width: 50%;
    padding: 0.5em;
}

.wrap #panel {
    background-color: inherit;
    height: 6em;
    border: 5px dashed #f90;
    text-align: center;
}

And some html:

<div class="wrap">
    <div id="panel">
        <p>This panel should look kinda sewn-on.</p>
    </div>
</div>

And, finally, A JS Fiddle demo

Okay, having just rediscovered this answer (thanks to the up-voter!), I can, now, provide an actual CSS-only no-extraneous-elements solution, using box-shadow:

#panel {
    background-color: #ffa;
    height: 6em;
    border: 5px dashed #f90;
    text-align: center;
    width: 50%;
    margin: 30px auto 0 auto;
    box-shadow: 0 0 0 15px #ffa;
}​

JS Fiddle demo.

The fourth parameter is the key, it defines the, uh, 'spread' of the shadow (the third parameter defines the 'fuzziness'/'diffusion' which in this case is 0), using the same background-color as the element itself gives the illusion that the border is within the element, while it's actually a shadow of the element extending out from the element.

like image 87
David Thomas Avatar answered Nov 06 '22 11:11

David Thomas


Thats what IE used to do in quirks mode. With CSS3 box-sizing you can switch between the two modes, but I'm not sure how the support is at the moment See http://www.quirksmode.org/css/box.html for more infos.

like image 31
Mene Avatar answered Nov 06 '22 12:11

Mene