效果以下 css
padding: 16px;
box-shadow: 0 0 0 8px #655;
border-radius: 5px;
复制代码
若是采用上面的方式,会产生一个与上面效果很像的效果:内外都是圆角。下过以下html
其关键就是outline和box-shadow属性:元素的圆角没法规定描边的方式浏览器
那么你可能会想到这样的实现方式ui
padding: 16px;
outline: 8px solid #655
border-radius: 5px;
复制代码
而后当你满怀自信去查看效果时发现,竟然又不是你想要的效果,你会发现内外之间会有一小点空白。spa
那怎么办,是否是bug?虽然元素的圆角没法规定描边的角的样式,但能够规定box-shadow的角的样式,因而解决方式诞生了:让box-shadow与outline重合,outline将box-shadow的圆角填充成直角,box-shadow填充outline与内层之间的空白code
padding: 16px;
box-shadow: 0 0 0 8px #655;
border-radius: 5px;
outline: 8px solid #655;
复制代码
这时再去刷新浏览器就会发现会是咱们想要的效果了cdn
附一个完整的例子htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>css learning</title>
<style> .section { display: inline-block; width: 200px; background: gray; padding: 16px; box-shadow: 0 0 0 8px #655; border-radius: 5px; outline: 8px solid #655; } </style>
</head>
<body>
<div class="container">
<span class="section">
Suspendisse et arcu felis, ac gravida turpis.
Suspendisse potenti. Ut porta rhoncus ligula,
sed fringilla felis feugiat eget.
</span>
</div>
</body>
</html>
复制代码