Home > Web Front-end > CSS Tutorial > What's the Best Alternative to CSS's calc() Function for Browser Compatibility?

What's the Best Alternative to CSS's calc() Function for Browser Compatibility?

Mary-Kate Olsen
Release: 2024-11-13 02:19:02
Original
947 people have browsed it

What's the Best Alternative to CSS's calc() Function for Browser Compatibility?

Calc Function Alternative for CSS

The calc() function offers a convenient way to dynamically calculate values in CSS. However, it may not be supported in older browsers like IE 5.5 and certain mobile browsers. Here's an alternative approach that works better across a wider range of browsers:

Box-sizing: border-box

Almost every instance of calc(100% - ) can be replaced by setting box-sizing: border-box; on one element and padding or margin on the other. For instance, to dynamically set the width of a div element:

Original Approach (with calc()):

width: calc(100% - 500px);
Copy after login

Alternative Approach (without calc()):

.sidebar {
  position: absolute;
  top: 0;
  left: 0;
  width: 300px;
  background: orange;
}

.content {
  padding-left: 300px;
  width: 100%;
  box-sizing: border-box;
  background: wheat;
}
Copy after login

Compatibility:

This alternative approach supports most major browsers, including IE 5.5 and above, Opera, and Android browsers.

Note: This solution relies on a precise value for the element's dimensions. If the element's dimensions are fluid or cannot be determined in advance, calc() may be the only viable option.

The above is the detailed content of What's the Best Alternative to CSS's calc() Function for Browser Compatibility?. 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