Home > Article > Web Front-end > How Can I Calculate Maximum Width in CSS Without Using the `max` Function?
Calculating Maximum Width in CSS
In CSS, it's currently not feasible to directly calculate the maximum width as a function of other values using the max function. Attempting to do so with constructs like max-width: calc(max(500px, 100% - 80px)) or max-width: max(500px, calc(100% - 80px))) will not yield the desired result.
A CSS Solution with Media Queries
However, it's possible to achieve a similar effect using media queries:
<code class="css">.yourselector { max-width: calc(100% - 80px); } @media screen and (max-width: 500px) { .yourselector { max-width: 500px; } }</code>
In this example, the .yourselector element will have a maximum width based on the viewport's size. When the viewport is wider than 500px, the maximum width will be set as the difference between the viewport width and 80px. However, when the viewport is at 500px or narrower, the maximum width will be explicitly set to 500px.
This approach effectively uses media queries to calculate the maximum width dynamically based on screen size, thereby mimicking the desired functionality of the max function in a restricted manner.
The above is the detailed content of How Can I Calculate Maximum Width in CSS Without Using the `max` Function?. For more information, please follow other related articles on the PHP Chinese website!