Home >Web Front-end >CSS Tutorial >Can You Use Mathematical Expressions within the `max-width` Property in CSS?
Is it possible to utilize mathematical expressions within the max-width property in CSS, such as:
max-width: calc(max(500px, 100% - 80px))
or
max-width: max(500px, calc(100% - 80px)))
Answer:
While direct calculation within the max-width property was previously not possible, modern CSS introduces a workaround using media queries:
<code class="css">.yourselector { max-width: calc(100% - 80px); } @media screen and (max-width: 500px) { .yourselector { max-width: 500px; } }</code>
This approach sets the max-width to be relative to the viewport width with a minimum of 500px, ensuring that the element does not exceed that width even when the parent container is wider than 500px.
The above is the detailed content of Can You Use Mathematical Expressions within the `max-width` Property in CSS?. For more information, please follow other related articles on the PHP Chinese website!