简体   繁体   English

响应字体大小

[英]Responsive font-size

hi i'm new to webdev and i was tring to make my first full-responsive webpage but i had a problem that the text is extra large for the mobile version so i did set the width to #vw as i though that will fix it but it didn't go as i wanted as the text get alot smaller being unreadable + the page looks ugly with it can you guys help me please?嗨,我是 webdev 的新手,我想制作我的第一个完全响应式网页,但我遇到了一个问题,即移动版本的文本特别大,所以我确实将宽度设置为 #vw,因为我虽然这样可以解决它但它并没有像我想要的那样 go,因为文本变小了很多,无法阅读 + 页面看起来很难看,你们能帮帮我吗?

`` ``

 .#### { font-size: 2.5vw; font-weight: 700; text-shadow: 0px 0px 2px rgba(0, 0, 0, 0.2); }

`` and i expected it to make the font size change with the width and it did but alittle bit over what i wanted and now with the mobile version the text is unreadable `` 我希望它能使字体大小随着宽度的变化而变化,但它确实比我想要的要小一点,现在移动版本的文本是不可读的

It sounds like you are using the vw (viewport width) unit to set the font size of your text, which causes the text to become smaller as the viewport width becomes smaller (eg on mobile devices).听起来您正在使用 vw(视口宽度)单位来设置文本的字体大小,这会导致文本随着视口宽度变小而变小(例如在移动设备上)。 One solution to this problem is to use media queries to apply different styles based on the width of the viewport.这个问题的一种解决方案是使用媒体查询根据视口的宽度应用不同的 styles。

For example, you could use a media query to set the font size to a larger/smaller value when the viewport width is below a certain threshold:例如,当视口宽度低于特定阈值时,您可以使用媒体查询将字体大小设置为更大/更小的值:

.#### {
  font-size: 2.5vw;
  font-weight: 700;
  text-shadow: 0px 0px 2px rgba(0, 0, 0, 0.2);
}

@media (max-width: 600px) {
  .#### {
    font-size: 2vw;
  }
}

This will cause the font size to decrease to 2vw when the viewport width is 600px or less.当视口宽度为 600px 或更小时,这将导致字体大小减小到 2vw。 You can adjust the width threshold and the font size to find the values that work best for your design.您可以调整宽度阈值和字体大小以找到最适合您的设计的值。

for more detail you can go https://www.w3schools.com/css/css_rwd_mediaqueries.asp有关更多详细信息,您可以 go https://www.w3schools.com/css/css_rwd_mediaqueries.asp

You can use media queries.您可以使用媒体查询。
Media query is a CSS technique introduced in CSS3.媒体查询是CSS3中引入的CSS技术。
It uses the @media rule to include a block of CSS properties only if a certain condition is true.它使用@media规则仅在特定条件为真时包含 CSS 属性块。

So here you can use a condition that if device is mobile then use these css (ie css for smaller font)所以在这里你可以使用一个条件,如果设备是移动的,那么使用这些 css(即 css 用于较小的字体)

 #para { font-size: 40px; } // This CSS will run for mobile devices @media only screen and (max-width: 300px) { #para { font-size: 10px; } }
 <p id="para"> arches to delightful measures. Grim-visaged war hath smooth'd his wrinkled front; And now, instead of mounting barded steeds To fright the souls of fearful adversaries, He capers nimbly in a lady's chamber To the lascivious pleasing of a lute. But I, that am not shaped for sportive tricks, Nor made to court an amorous looking-glass; I, that am rudely stamp'd, and want love's majesty To strut before a wanton ambling nymph; I, that am rudely stamp'd, and want love's majesty To strut before a wanton ambling nymph; I, that am not shaped for sportive tricks, Nor made to court an amorous looking-glass; I, that am curtail'd of this fair proportion, of the ocean buried. Now are our brows bound with victorious wreaths; Our bruised arms hung up for monuments; Our stern alarums changed to merry meetings, Our dreadful marches to delightful measures. Grim-visaged war hath smooth'd his wrinkled front; And now, instead of mounting barded steeds To fright the souls of fearful adversarie </p>



You can even add breakpoints for multiple screen widths, like say for device width >1000px use font 20px ,then from width 500 to 1000px use font 15px , then for width <500px use font 10px , etc, anything you may like.您甚至可以为多个屏幕宽度添加断点,比如device width >1000px使用字体20px ,然后从width 5001000px使用字体15px ,然后width <500px使用字体10px ,等等,任何你可能喜欢的。

Also I am providing you some breakpoints which I use, you can search for better maybe:我还为您提供了一些我使用的断点,您可以搜索更好的也许:

@media (min-width:320px)  { /* smartphones, iPhone, portrait 480x320 phones */ }
@media (min-width:481px)  { /* portrait e-readers (Nook/Kindle), smaller tablets @ 600 or @ 640 wide. */ }
@media (min-width:641px)  { /* portrait tablets, portrait iPad, landscape e-readers, landscape 800x480 or 854x480 phones */ }
@media (min-width:961px)  { /* tablet, landscape iPad, lo-res laptops ands desktops */ }
@media (min-width:1025px) { /* big landscape tablets, laptops, and desktops */ }
@media (min-width:1281px) { /* hi-res laptops and desktops */ }

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

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