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

How Can I Create a CSS Border Smaller Than Its Containing Div's Width?

Patricia Arquette
Release: 2024-12-12 11:52:10
Original
114 people have browsed it

How Can I Create a CSS Border Smaller Than Its Containing Div's Width?

Creating a Border Smaller Than the Div Width

In CSS, the width of a border is typically determined by the width of the element it surrounds. If you want your border to be smaller than the width of its container element, you can use pseudoelements. Here's how:


  width: 200px;<br>  height: 50px;   <br>  position: relative;<br>  z-index: 1;<br>  background: #eee;<br>}</p><p>div:before {<br>  content: "";<br>  position: absolute;<br>  left: 0;<br>  bottom: 0;<br>  height: 1px;<br>  width: 50%;  /<em> or 100px </em>/<br>  border-bottom: 1px solid magenta;<br>}

<div>Item 2</div>

In this example, we use the :before pseudoelement to create a border that is only half the width of the container div. We do this by setting the width property of the :before element to 50% or the desired width (e.g., 100px). We also position the :before element absolutely within the container div using the position: absolute property.

The z-index property ensures that the :before element is positioned above the content of the container div. This prevents the border from being hidden behind the content.

By using pseudoelements, you can create borders that have different widths and positions from their container elements. This flexibility allows you to create custom visual effects and enhance the design of your web pages.

The above is the detailed content of How Can I Create a CSS Border Smaller Than Its Containing 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