HTML/CSS: table font size different in mobile device
Asked Answered
O

3

23

I would like to have a simple website that works both on desktop and mobile browser, and encountered a weird (rookie) problem: when I have a table whose column texts are of different length, the font sizes rendered in mobile device are dramatically different. Any idea why this is happening and what is a quick and clean fix? Thanks in advance for your help!

The HTML code:

<!DOCTYPE html>
<html>
  <head>
    <style>
     body {
       font-family: Verdana, Geneva, Arial, sans-serif;
       font-size: medium;
     }
     table, td {
       border: 1px solid black;
     }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <td>Short text. Short text</td>
        <td>Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. </td>
      </tr>
    </table>
  </body>
</html>

Renders okay on desktop browser

desktop

Weird font size on mobile browser (chrome emulator)

mobile

Oogonium answered 22/11, 2014 at 5:3 Comment(3)
What emulator are you referring to? This seems to be a bug in the emulator rather than anything else.Detonation
I was using Google Chrome mobile emulator, and nope, it's not a bug --- I saw the same rendering effect on my smart phone (and that's why I set out to fix it).Oogonium
You’re correct, this oddity indeed happens in Chrome on Android (and the meta tag suggested in an answer fixes this).Detonation
G
17

You need to consider setting the viewport of your application. To make a mobile-friendly web application, you will need to set this in your headers. You can do so by adding a <meta> tag between your <head> tags. Here is an example:

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

For a full description of what a viewport is and how it is used, you can visit the article Configuring the Viewport from Apple Developer as it was first introduced for Safari Mobile.

Gaylord answered 22/11, 2014 at 5:23 Comment(6)
Thanks @Abel, but changing font-size does not solve my problem --- the emulator still has different font size on two columns.Oogonium
@YingXiong take a look at my edited answer if it works for youGaylord
Yep, the <meta> tag corrects the rendering. If you could kindly add some more explanation on that tag, I'd really appreciate it!Oogonium
The font size settings are not relevant to the question.Detonation
I have just edited the answer and removed the unnecessary parts of it. Now it's just the plain solution.Gaylord
This is MAGIC! 🦄Reece
N
11

Eventually you might also need

text-size-adjust: none;
-webkit-text-size-adjust: none;

to make sure that text is not renderred bigger or smaller depending on the size of table cells. Especially in Safari on iOS I found this to be relevant.

Nationalism answered 15/10, 2020 at 10:11 Comment(2)
The accepted answer didn't work for me, but this did. Thank you.Waterhouse
Same here. The accepted answer didn't work for me, but this did. +1. In particular, it seems it is "-webkit-text-size-adjust: none;" the key. Pay attention to the "-", it is really "-webkit" not "webkit" ! Thank you Tom !Eidetic
D
-2

try this

 body {
  font-family: Verdana, Geneva, Arial, sans-serif;
  font-size: 15px;
}
Dipole answered 22/11, 2014 at 7:34 Comment(1)
Thanks, @Reza, but setting the font-size does not solve my problem.Oogonium

© 2022 - 2025 — McMap. All rights reserved.