Is it possible to have a partial border on all the edges of a div starting from each corner? I want to avoid background-image property & use a CSS Only solution to achieve this.
Partial borders are possible using the pseudo :after property but I cant figure out how to have it on all the edges?
The div should look like a focus widget as shown in the attached image.

Thanks.
Sure, try this:
HTML:
<div></div>
CSS:
div {
background: white;
padding: 1.7em;
position: relative;
width: 300px;
height: 300px;
margin: 3em auto;
border: solid 2px #d00;
}
div::before, div::after {
position: absolute;
background: inherit;
content: '';
z-index: 1;
}
div::before {
width: 100px;
left: calc(50% - 50px);
height: calc(100% + 4px);
top: -2px;
}
div::after {
height: 100px;
left: -2px;
width: calc(100% + 4px);
top: calc(50% - 50px);
}
div > * {
position: relative; z-index: 2; /* ensure any child elements sit above pseudos */
}
Fiddle.
You can do this with two extra elements for borders so you don't have to mask backgrounds and you can have transparent background.
.element {
width: 200px;
height: 200px;
margin: 50px;
position: relative;
padding: 10px;
}
.top-border,
.bottom-border {
position: absolute;
display: flex;
justify-content: space-between;
width: 100%;
height: 30%;
left: 0;
}
.top-border {
top: 0;
}
.bottom-border {
bottom: 0;
}
.top-border:after,
.top-border:before,
.bottom-border:after,
.bottom-border:before {
content: '';
width: 30%;
height: 100%;
}
.top-border:after,
.top-border:before {
border-top: 1px solid green;
}
.bottom-border:after,
.bottom-border:before {
border-bottom: 1px solid green;
}
.top-border:before,
.bottom-border:before {
border-left: 1px solid green;
}
.top-border:after,
.bottom-border:after {
border-right: 1px solid green;
}
<div class="element">
<div class="top-border"></div>
<div class="bottom-border"></div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reiciendis, libero!</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With