css - 添加边框到填充

标签 css border padding

我有一个 div,左右边距都设置为 50px。

Padding border?

是否可以添加紫色边框?

我无法向 HTML 添加代码,这应该尽可能使用纯 css 来完成。我试图用 border-image 来欺骗它并添加渐变,但我只能这样添加:

div {
  width: 150px;
  height: 150px;
  background-color: grey;
  padding-left: 50px;
  padding-right: 50px;

  border-top: 5px solid black;
  border-image: linear-gradient(to right, white 50px, purple 0%);
  border-image-slice: 1;
}

<div>Content</div>

https://jsfiddle.net/u7zq0amc/1/

最佳答案

改用伪元素:

div {
  width: 150px;
  height: 150px;
  background-color: grey;
  padding-left: 50px;
  padding-right: 50px;
  position:relative;
  margin-top:20px;
}
div:before {
  content:"";
  position:absolute;
  height:5px;
  top:-5px;
  right:50px;
  left:50px;
  background:red;
}
<div>Content</div>

关于css - 添加边框到填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48291828/

相关文章:

html - 如何在页面中移动元素

html - 相同的样式为表格边框返回不同的输出

html - 使用 ProfitBuilder WP 在我的网站上添加额外填充

css - 使用 twitter bootstrap 对不同页面进行不同的填充

html - Div 没有出现在父 div 容器中

jquery 移动水平控制组无法在移动设备上正确显示

html - 为什么箭头不能在IE7中显示?

html - 圆形按钮上的斜 Angular

css - CSS中边框底部的曲线

html - 为什么这些 inline-block div 元素之间存在无法解释的差距?