Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change the width of a CSS border independently from it's parent?

I am looking to apply a custom border on a div. I’d like that border to have a length/width (unsure on how I should name this) that’s independent from the div width.

I know a similar question has been asked here but isn’t there a new solution that doesn’t rely on adding some extra HTML ?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#foo {
    height: 100px;
    width: 100px;
    background-color: red;

    border-top: 10px solid blue;
}
<div id="foo"></div>

>Solution :

You can try using ::before and ::after

#foo {
    height: 100px;
    width: 100px;
    background-color: red;
}

#foo::before {
  border-top: 5px solid blue;
  width: 80px;
  height: 5px;
  position: absolute;
  content: '';
  margin-left: calc((100px - 80px)/2);
  top: 3px;
}
<div id="foo"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading