Home > Web Front-end > CSS Tutorial > How Can I Create a Shorter Border Than My Div's Width?

How Can I Create a Shorter Border Than My Div's Width?

Barbara Streisand
Release: 2024-12-04 20:31:10
Original
898 people have browsed it

How Can I Create a Shorter Border Than My Div's Width?

Border Length Shallower Than Div Width

Your code defines a div element with a width of 200px and a solid 1px magenta border along its bottom edge. However, you seek a method to limit this border's length to just 100px without altering the div's width.

Pseudoelements provide a solution. Pseudoelements are elements added to your DOM tree, allowing you to style portions of an existing element. In this instance, you can create a pseudoelement that acts as the border:

div:before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  height: 1px;
  width: 50%;  /* or 100px */
  border-bottom:1px solid magenta;
}
Copy after login

This pseudoelement, which positions itself absolutely at the bottom left of the div, replicates the desired border length of 100px. By adjusting its width property, you can easily control the border's length without affecting the div's own width.

The above is the detailed content of How Can I Create a Shorter Border Than My Div's Width?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template