Home >Web Front-end >CSS Tutorial >How Can I Create Partial Borders in CSS?

How Can I Create Partial Borders in CSS?

Susan Sarandon
Susan SarandonOriginal
2024-12-26 22:50:10234browse

How Can I Create Partial Borders in CSS?

Partial Borders in CSS: A Creative Illusion

It can be desirable to create boxes with borders that appear only on certain sides or extend only partially across an edge. Despite the absence of direct support in CSS, this effect can be elegantly achieved through a simple and flexible method.

Consider, for instance, a box of 350px width with a bottom border that extends only 60px from the left. This can be replicated with the following CSS code:

div {
  width: 350px;
  height: 100px;
  background: lightgray;
  position: relative;
  margin: 20px;
}

div:after {
  content: '';
  width: 60px;
  height: 4px;
  background: gray;
  position: absolute;
  bottom: -4px;
}

The magic lies in the div:after pseudo-element. It creates an empty box positioned absolutely at the bottom of the main box. By adjusting the width and bottom properties, we can control the size and position of the partial border.

This approach is versatile and works well in all modern browsers. It does not require any additional markup, leading to clean and maintainable code. It also degrades gracefully in browsers that do not support position: absolute.

The above is the detailed content of How Can I Create Partial Borders in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn