I am aware of the CSS 3 units vw
, vh
and vm
, which seem to be useful for making elements that have their box sizes and text sizes relative to the browser's viewport size. However, sadly, these are not well-supported with the current major browsers; only Internet Explorer 9+ does.
What other methods can I use to do things like CSS font-size
properties that scale with the vie开发者_Go百科wport? I would like to avoid JavaScript and/or jQuery solutions if possible.
Doing a 100% scalable website is possible. As Rev said, you can do this by using percentage values, but it is tricky.
The better option is to utilize @media queries
. These allow you to apply CSS rules to a page only under certain conditions. By using media queries to detect the device width and/or the page width, you can apply fine tune control over how your site looks AT different viewport sizes. For instance:
@media screen and (max-device-width: 960px) {
font-size:14px;
}
@media screen and (max-device-width: 480px) {
font-size:13px;
}
Now, the above example is rather trivial and contrived. For a deeper understanding of what you can accomplish with media queries, I recommend you view the W3C spec page. Some of the most powerful are the width
, min-device-width
, max-device-width
, portrait|landscape
, and print
queries.
As a side note, make sure to include these styles at the bottom of your CSS, so that they dont get overwritten by default styles.
精彩评论