Home >Web Front-end >CSS Tutorial >How Can I Set an Element's Height or Width as a Percentage Minus a Fixed Pixel Value in CSS?
One common challenge in CSS is specifying an element's height or width as a percentage of its container minus a fixed value. Consider the following scenario:
You have a container
The solution lies in utilizing the calc() function:
height: calc(100% - 18px);
This expression calculates the height by subtracting the known fixed value (18px), allowing the list to expand to fill the remaining space in the container.
While calc() is widely supported, some legacy browsers require vendor-specific versions:
/* Firefox */ height: -moz-calc(100% - 18px); /* WebKit */ height: -webkit-calc(100% - 18px); /* Opera */ height: -o-calc(100% - 18px);
For complete cross-browser compatibility, consider using all the versions together with the standard syntax at the end:
/* Firefox */ height: -moz-calc(100% - 18px); /* WebKit */ height: -webkit-calc(100% - 18px); /* Opera */ height: -o-calc(100% - 18px); /* Standard */ height: calc(100% - 18px);
The above is the detailed content of How Can I Set an Element's Height or Width as a Percentage Minus a Fixed Pixel Value in CSS?. For more information, please follow other related articles on the PHP Chinese website!