Home Web Front-end HTML Tutorial Best font size settings for mobile

Best font size settings for mobile

May 15, 2018 am 11:01 AM
html

Hey, let me tell you how to use some mobile CSS units. px is definitely not possible. Mobile phones have so many sizes, so how much @media should be written. There are two most commonly used units:
1 and rem

rem is set relative to the size of the root element. The root element in the web page refers to It is html. We can control the size of rem by setting the font size of html. For example:

<!DOCTYPE html>
<html>
<head>    
<meta charset="utf-8" />    
<title></title>    
<style type="text/css">        
html{            
font-size:20px;        }        
p{            
width: 600px;            
overflow: hidden;            
line-height: 1;            
font-size: 1rem;        }    
</style></head>    <body>    
<p>        是啊啊啊    
</p></body></html>
Copy after login
When you set font-size:20px; in the html root element, 1rem = 20px; similarly, if you set 10px, then 1 rem = 10px; rem can not only be used for font size, It can also be used for length units such as width. At this time you may ask, how to achieve responsiveness like this? Therefore, we need to combine @media to set the size of the html root element, so that all corresponding rem units in the page will follow the root element, for example:
  • html {
        font-size : 20px;
    }
    @media only screen and (min-width: 401px){
        html {
            font-size: 25px !important;
        }
    }
    @media only screen and (min-width: 428px){
        html {
            font-size: 26.75px !important;
        }
    }
    @media only screen and (min-width: 481px){
        html {
            font-size: 30px !important; 
        }
    }
    @media only screen and (min-width: 569px){
        html {
            font-size: 35px !important; 
        }
    }
    @media only screen and (min-width: 641px){
        html {
            font-size: 40px !important; 
        }
    }
    Copy after login

If you don't When setting the font size of HTML, 16px will be used as the base. The advantage of this over px is obvious. You don't need to apply @media to each element. You only need to set different sizes for the html root element. The compatibility is also very good.
2. vw, vh
Although the rem unit is still very simple, it is still not simple and crude enough. There is another simpler unit, which is the vw and vh unit.

Relative to the width of the viewport. The viewport is divided into 100 units of vw. For example, 1vw is equivalent to 1% of the screen width. Go directly to the example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        p{
            width: 20vh;
            height: 30vw;
            line-height: 1;
            font-size: 1vw;
            background: #FC8B53;
            color: #fff;
        }
    </style>
</head> 
<body>
    <p>
        是啊啊啊
    </p>
</body>
</html>
Copy after login
After setting it like this, the width of p changes with the change of the screen height, its height changes with the change of the screen width, and the font size also changes with the screen. I did this to more intuitively express the flexibility of the vw unit. No more writing a bunch of messy @media stuff.
What kind of flexible method? For example, if you set the width of a p to 50% of the screen, then the width is 50vw. At this time, you need to set its height to 50% of its width. What if you don't use vw? You can only use js to calculate. But when using vw, just set it to 25vw and it will be fine.
However, there are some compatibility issues. There is no problem with ios. The built-in browser of Android phone requires Android system 4.4 or above. Other browsers are OK, only the built-in browser has problems. But who still uses Android’s built-in browser to play with it now? It can’t be found anywhere. So, just use vw and vh.

The above is the detailed content of Best font size settings for mobile. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles