欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
答:使用CSSbox-shadow属性
如果要在矩形框内放置或绘制边框,则有一个非常简单的解决方案-只需使用CSS outline属性而不是border,并使用outline-offset具有负值的CSS3属性将其移动到元素框内即可。
 
但是,此解决方案不适用于圆角元素。但是,您仍然可以使用该box-shadow属性通过一些技巧在圆形框或带有圆角的元素内绘制边框。
 
让我们看下面的示例,以了解其基本工作原理:
 
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Border inside DIV Element</title>
<style>
    .box {
        width: 180px;
        height: 180px;
        background: black;
        margin: 20px 50px;
    }
    .circle {
        border-radius: 50%;
    }
    .inner-border {
        border: 20px solid black;
        box-shadow: inset 0px 0px 0px 10px red;
        box-sizing: border-box; /* Include padding and border in element's width and height */
    }
    /* CSS3 solution only for rectangular shape */
    .inner-outline {
        outline: 10px solid red;
        outline-offset: -30px;
    }
</style>
</head> 
<body>
    <h2>Border inside Rectangular and Circular Shape</h2>
    <div class="box circle inner-border"></div>
    <div class="box inner-border"></div>
    <hr>
    <h2>Outline Inside Rectangular Shape</h2>
    <div class="box inner-outline"></div>
</body>
</html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h61125.shtml