简体   繁体   English

根据分辨率更改字体大小

[英]Change font size depending on resolution

I'm developing a Web page that uses different sizes for its different paragraphs, h... and so on. 我正在开发一个网页,它为不同的段落使用不同的大小,等等......等等。 I'm using em sizes: font-size: 2em; 我正在使用em尺寸: font-size:2em; , as an example. , 举个例子。 But when I change my screen resolution to a lower one, I want that size to a smaller one. 但是,当我将屏幕分辨率更改为较低的分辨率时,我希望将该分辨率调整为较小的分辨率。

For that purpose, I tried this code: 为此,我尝试了这段代码:

<script>
        if ( screen.width > 1600)
        {
            document.write('<style>#centered h1 { font-size: 2em; } </style>');
        }
        else if (screen.width <= 1600 && screen.width >= 1360)
        {
            document.write('<style>#centered h1 { font-size: 1.7em; } </style>');
        }
        else if (screen.width < 1360 && >= 1024)
        {
            document.write('<style>#centered h1 { font-size: 1.4em; } </style>');
        }
        else
        {
            document.write('<style>#centered h1 { font-size: 0.8em; } </style>');
        }
    </script>

First: it doesn't work... 第一:它不起作用......
Second: I think there should be cleaner way to do so... 第二:我认为应该采取更清洁的方式......

So, the question is: how to use different sizes to my fonts or how to make them adjust for different resolutions? 所以,问题是:如何对我的字体使用不同的大小或如何使它们针对不同的分辨率进行调整?

Can anyone help please? 有人可以帮忙吗? Thanks! 谢谢!

Try to use this concept proof CSS: 尝试使用这个概念证明CSS:

html { font-size: 62.5%; }
body { font-size: 1em;}

@media (max-width: 300px) {
    html { font-size: 70%; }
}

@media (min-width: 500px) {
    html { font-size: 80%; }
}

@media (min-width: 700px) {
    html { font-size: 120%; }
}

@media (min-width: 1200px) {
    html { font-size: 200%; }
}

Working demo on jsFiddle 关于jsFiddle的工作演示

You want to use media queries rather than JS. 您想使用媒体查询而不是JS。 Alternatively, use JS to add a class to the body then use that to target what you want. 或者,使用JS向主体添加类,然后使用它来定位您想要的内容。

You can either use font sizes (em or pt) relative to the screen resolution as lanzz pointed out. 您可以使用相对于屏幕分辨率的字体大小(em或pt),如lanzz所指出的那样。 Or, you can also use media queries in your css file as per "http://www.w3.org/TR/css3-mediaqueries/#width" 或者,您也可以根据“http://www.w3.org/TR/css3-mediaqueries/#width”在css文件中使用媒体查询

@media screen and (min-width: 400px) and (max-width: 700px) { … }

you can set any min and max width. 您可以设置任何最小和最大宽度。

The best solution is Viewport Sized Typography 最佳解决方案是Viewport Sized Typography

There are many reasons. 原因很多。 Here are two: 这是两个:

  1. There is a such thing as a comfortable line length for reading text on screens. 在屏幕上阅读文本有一个舒适的线长。 I don't want to kick a hornet's nest, but let's say its around 80 characters. 我不想踢大黄蜂的巢,但让我们说它大约80个字符。 These units allow you to get it feeling perfect and then have that experience scale to any size screen. 这些单元让您感觉完美,然后将体验扩展到任何尺寸的屏幕。
  2. They allow you to tightly couple the size relationship of, say, a typographic header and the content it goes with. 它们允许您紧密地结合印刷标题的大小关系以及它所带来的内容。

How they work 他们如何工作

One unit on any of the three values is 1% of the viewport axis. 三个值中的任何一个上的一个单位是视口轴的1%。 "Viewport" == browser window size == window object. “视口”==浏览器窗口大小==窗口对象。 If the viewport is 40cm wide, 1vw == 0.4cm. 如果视口宽40厘米,1vw == 0.4厘米。

For use with font-size, I guess it's one "letter" that takes on that size, but as we know, in non-mono-spaced fonts the width of a letter is rather arbitrary. 为了与font-size一起使用,我猜这是一个“字母”,它采用了这个尺寸,但正如我们所知,在非单倍间距字体中,字母的宽度是相当随意的。 I find you just need to tweak around with the values to get it how you want it. 我发现你只需要调整值就可以得到你想要的效果。 Which is basically what we do anyway, right? 这基本上就是我们做的事,对吧?

  • 1vw = 1% of viewport width 1vw =视口宽度的1%
  • 1vh = 1% of viewport height 1vh =视口高度的1%
  • 1vmin = 1vw or 1vh, whichever is smaller 1vmin = 1vw或1vh,取较小者
  • 1vmax = 1vw or 1vh, whichever is larger 1vmax = 1vw或1vh,以较大者为准
 h1 { font-size: 5.9vw; } h2 { font-size: 3.0vh; } p { font-size: 2vmin; } 

em sizes are relative to the size of the parent element. em大小相对于父元素的大小。 You probably want to set your font sizes in pt units (1pt = 1/72 inch), which are resolution-independent and designed to look the same apparent size in any resolution. 您可能希望以pt单位(1pt = 1/72英寸)设置字体大小,这些单位是与分辨率无关的,并且设计为在任何分辨率下看起来都具有相同的表观大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM