.item {
display: inline-block;
padding: 0.2em 0.3em;
background: #f8f8f8;
color: #454545;
/* Set border widths with variables */
--top-border-width: 4px;
--bottom-border-width: var(--top-border-width);
--left-border-width: 16px;
--right-border-width: var(--top-border-width);
/* Set border styles for each side */
border-top: var(--top-border-width) solid #e4e4e4;
border-bottom: var(--bottom-border-width) solid #e4e4e4;
border-right: var(--right-border-width) solid #e4e4e4;
/* Remove the left border and add blank space where the border should be placed */
border-left: 0;
margin-left: var(--left-border-width);
/* Contain the ::before */
position: relative;
}
.item::before {
/* Give the pseudo element substance */
display: block;
content: "";
/* Add a left border with a straight edge */
border-left: var(--left-border-width) solid #f84995;
/* Position pseudo element's border where the normal border would have been placed */
position: absolute;
top: calc(0px - var(--top-border-width));
bottom: calc(0px - var(--bottom-border-width));
left: calc(0px - var(--left-border-width));
}
div {
/* downside of using box-shadow, you need to add the */
/* inner border size to the padding on top of any */
/* additional padding you might want */
padding: 20px;
/* by changing the order of your box-shadows, you */
/* can modify which borders overlap each other */
box-shadow:
/* left "inner border" */
inset 20px 0 0 0 red,
/* right "inner border" */
inset -20px 0 0 0 grey,
/* top "inner border" */
inset 0 20px 0 0 grey,
/* bottom "inner border" */
inset 0 -20px 0 0 grey;
}
div {
border: 20px solid grey;
}
div::before {
position: absolute;
background-color: red;
content: "";
width: 20px;
/* we need to add the height of the top and bottom */
/* border to the pseudo-elements' height as the */
/* inset border is not included in the height of the */
/* div even when "box-sizing: border-box" is set. */
height: calc(100% + 20px + 20px);
top: -20px;
left: -20px;
}
div {
/* downside of using multiple linear-gradients, you */
/* need to add the inner border size to the padding */
/* on top of any additional padding you might want */
padding: calc(20px + 10px);
background-image:
/* left "inner border" */
linear-gradient(to right, red 20px, transparent 20px),
/* right "inner border" */
linear-gradient(to left, grey 20px, transparent 20px),
/* top "inner border" */
linear-gradient(grey 20px, transparent 20px),
/* bottom "inner border" */
linear-gradient(to top, grey 20px, transparent 20px);
}
4条答案
按热度按时间twh00eeo1#
.item::before
是正确的方法,但是除了border-left
属性之外,还需要做一些工作。您需要使伪元素可见(display: block; content: "";
),将伪元素定位在.item
的左侧,并将其拉伸到与顶部和底部边框正确对齐。虽然这可以手动完成,但我强烈推荐使用CSS Variables(或预处理器中的变量),因为它使更新边框宽度更不容易出错和痛苦。
klr1opcd2#
如果你想使用
:before
伪选择器,你还需要设置一些内容,例如this jsfiddle和下面的代码:CSS:
显示为:
嗯,虽然这应该严格地回答了问题,但当我试图将我的解决方案调整到问题的小提琴上时,我发现这并不能很好地处理填充。欢迎提出建议/编辑/其他答案,可以处理这一点:(...
vnjpjtjt3#
这应该工作,但需要额外的标记:
以及
wfveoks04#
背景
默认情况下,CSS使用miter joints(45°角)作为所有边界的关节。因此,要实现任何边界的方形关节(90°角),您可以使用(1)内部box-shadow,(2)pseudo-elements或(3)background-image和多个linear-gradients。
假设您有以下要设置样式的元素:
选项1:方形接头使用
box-shadow
box-shadow
的浏览器支持选项2:方形接头
pseudo-elements
pseudo-elements
的浏览器支持选项3:使用
background-image
和多个linear-gradients
的方形接头background-images()
的浏览器支持linear-gradient()
的浏览器支持