Home > Web Front-end > CSS Tutorial > Can I Use Percentages for Border Width in CSS?

Can I Use Percentages for Border Width in CSS?

DDD
Release: 2024-12-09 06:56:11
Original
960 people have browsed it

Can I Use Percentages for Border Width in CSS?

Border Thickness in Percentages

Your endeavor to set border width in percentages has encountered a roadblock, as the CSS specification explicitly states that percentages are not supported for border widths.

However, a workaround exists that allows you to achieve the illusion of percentage borders. This involves creating a wrapper element with the desired border color as its background and setting its padding in percentages. Simultaneously, set the background of the element you want to border to white (or another suitable color).

Consider this example:

CSS:

.faux-borders {
    background-color: #f00;
    padding: 1px 25%; /* simulate border with padding */
}

.content {
    background-color: #fff;
}
Copy after login

HTML:

<div>
Copy after login

This non-scripted solution provides a method to simulate percentage borders without JavaScript.

The above is the detailed content of Can I Use Percentages for Border Width in CSS?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template