css - 有没有办法让左边框填充所有空间(没有内部间隙)?

标签 css border

我正在尝试使用以下代码使用 CSS 边框属性设置页面标题的样式:

h2 {
    display: inline-block;
    margin: 5px 0 5px 0;
    padding: 0 0 0 5px;
    background-color: #eee;
    border-color: #aaa;
    color: #000;
    border-style: dotted dotted dotted solid;
    border-width: 1px 1px 1px 5px;
}
结果是
enter image description here ,
没关系,但左边框有“尖尖”的提示,有间隙(看起来像是一种“规定”,在这种情况下,不存在类似的边框),如下图所示
left border with pointy tips, showing gap up-close
有没有办法获得左边框的“方形”提示?

最佳答案

不,您不能使用本地边界。但是你可以使用 :before 来伪造它元素:

h2 {
  position: relative; /* make title relative */
  display: inline-block;
  margin: 5px 0 5px 0;
  padding: 0 0 0 5px;
  background-color: #eee;
  border-color: #aaa;
  color: #000;
  border-style: dotted dotted dotted solid;
  border-width: 1px 1px 1px 5px;
}

h2:before {
  content: "";
  position: absolute;
  height: calc(100% + 2px); /* 2px is the addition of the border-bottom (1px) and the border-top (1px) */
  width: 5px; /* same size than the border-left */
  background-color: #aaa;
  left: -5px;
  top: -1px; /* size of the border-top */
}
<h2>A title</h2>

关于css - 有没有办法让左边框填充所有空间(没有内部间隙)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64066775/

相关文章:

css - 外部样式渗入我的 Polymer 组件的本地 DOM

css - Bootstrap 字幕位置

css - 带圆 Angular 和缩进曲线边框的正方形

css边框问题

iframe - 如何在其他元素(例如 div 或 iframe)上模拟事件输入或文本区域周围的 Chrome/Safari 边框?

html - z-index 和汉堡菜单

javascript - 如何解决Bootstrap中的 "style sheet could not be loaded"

html - css 'tab' 修复 > 选项卡的底部边框

css - Safari 边框错误

jQuery(或 CSS3)在滚动的内容部分之间切换