Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;, 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!

like image 357
Sonhja Avatar asked Jun 13 '12 10:06

Sonhja


People also ask

Does font size change with resolution?

However, because they are a fixed size, pixels do not size up or down. That means that fonts will be rendered smaller on a screen with higher resolution than on a lower resolution screen.

How do I change my screen resolution font and text size on my monitor?

To change your display in Windows, select Start > Settings > Ease of Access > Display. To make only the text on your screen larger, adjust the slider under Make text bigger. To make everything larger, including images and apps, choose an option from the drop-down menu under Make everything bigger.

Why is high resolution text smaller?

Modern computer monitors / screens leave the factory with the screen resolution set at 1024 x 768 pixels or even higher. This is to give the maximum effect for graphics, particularly for playing computer games. However this has the effect of making the text size, and icon size smaller in general use.

How do you change the font size based on container width?

The font size can be set with vw (viewport) unit, which means the viewport width. The viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.

How do I change font size for screen size?

Select 'Display'. Select the 'Change display settings' link towards the top of the window on the left-hand side. In the 'Resolution' section, select the pull-down bar, and a slider bar should appear. Move the slider bar down to make the text larger, or move it up to make the text smaller.


2 Answers

Try to use this concept proof 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

like image 143
Vladimir Starkov Avatar answered Nov 13 '22 04:11

Vladimir Starkov


You want to use media queries rather than JS. Alternatively, use JS to add a class to the body then use that to target what you want.

like image 31
Rich Bradshaw Avatar answered Nov 13 '22 03:11

Rich Bradshaw