How to Achieve Dynamic Width Adjustments Without CSS Calc() for Wider Browser Compatibility?

Patricia Arquette
Release: 2024-11-12 20:31:02
Original
165 people have browsed it

How to Achieve Dynamic Width Adjustments Without CSS Calc() for Wider Browser Compatibility?

Alternative to CSS Calc

The CSS calc() function conveniently allows for dynamic width adjustments of elements. While supported by modern browsers, it lacks compatibility with older versions such as IE 5.5 and below.

To resolve this and extend support to Opera and the Android browser, consider using box-sizing: border-box instead.

For instance, assume a div with the class "sideBar" with an assumed width of 300px. To dynamically adjust the width of the "content" div based on the sidebar width, avoid using:

.content {
  width: calc(100% - 300px);
}
Copy after login

Instead, apply the following styles:

.sideBar {
  position: absolute;
  top: 0;
  left: 0;
  width: 300px;
}
.content {
  padding-left: 300px;
  width: 100%;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
Copy after login

By defining a fixed width for the sidebar and applying box-sizing: border-box to the content div, the content's width adjusts automatically based on the sidebar's width, eliminating the need for calc(). This approach ensures compatibility across a wider range of browsers, including older versions of IE, Opera, and the Android browser.

The above is the detailed content of How to Achieve Dynamic Width Adjustments Without CSS Calc() for Wider 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