What causes the discrepancy in scaling between em values on mobile and desktop displays?

I'm struggling to comprehend how em units function in CSS. I am attempting to make an element, specifically a div, scale proportionally to the font size. After narrowing down my issue, this is what I've come up with.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <style>
      body { font-size: 100%; }
      .EventReg {
        border:solid #000000 .063em;
        background-color:#000000;
        padding:.625em;
        width:13.65em;
      }
    </style>
  </head>
  <body>
    <div style="font-size:2em;">Event Registration</div>
    <div class="EventReg"></div>
  </body>
</html>

Currently, everything scales correctly in IE, FF, and Chrome when I modify body { font-size: 100%; } to a different percentage.

My expectation is that both the text and the div should have equal lengths on a computer browser. However, on my Android smartphone, the text appears wider than the div.

I anticipate that despite scaling, the div below the text should match the same width as the text itself.

If you have any insights into where I may be going astray or if my assumption is incorrect, please share your thoughts.

Answer №1

One possible solution could be related to the -webkit-text-size-adjust property. You can try resolving this issue by including the following CSS style in your code.

* {
  -webkit-text-size-adjust:100%;
}

Some mobile browsers automatically adjust the font size for better readability on websites. By setting this property to 100% (or possibly none), you can prevent this adjustment from occurring.

Answer №2

Expecting the text and an empty div element to be the same width is unreasonable. The width of the text is influenced by the font type and potentially other font rendering characteristics like kerning. On the other hand, the width of an empty div element is determined by using the em unit. The em unit corresponds to the size of the font, not its width or height.

If you want to adjust the width of an element based on the width of text in another element, it requires tools like table formatting or different layout methods. For a simple rectangle under the text, consider using properties like display: inline-block and applying a suitable border-bottom style to the element containing the text.

Answer №3

To overcome this challenge, I converted the text into an image which provided a consistent display across all devices and browsers. While not my preferred solution, it effectively resolved the issue at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating an HTML table using a JavaScript object dynamically

I am in the process of dynamically generating an HTML table using information from a MySQL database table. The data within the table is regularly changing, requiring the HTML table to be updated accordingly when alterations are made to the database table. ...

Avoiding postbacks in an aspx page when submitting an email input data

Currently, I am working on an ASPX page (version 3.5) and attempting to implement HTML5 validation on it. <asp:TextBox ID="AccountantEmail" CssClass="form-control" type="email" runat="server" required /> Upon rendering, the appearance of this contr ...

Attempting to arrange all the images in a horizontal display rather than a vertical one

This is my first ever question on the forum sorry if it is unprofessional hopefully i can learn from my mistakes on this post!! I put the question in the title but if you have any questions feel free to ask below THANK YOU! for the help! My Code ...

What is the best way to choose and deselect a checkbox in Angular?

I'm currently working on an angular Todo list project where I'm tackling the challenge of implementing a select all and unselect all checkbox feature. todo.ts import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core&ap ...

effects of material ui buttons

I have implemented two material ui buttons on my page. <Button variant="contained">Apply</Button> <Button variant="contained">Remove</Button> I am looking to add some functionality where a description of what ea ...

Column overflowing from row in Bootstrap grid on smaller screens

Is there a way to rearrange the display order of my Hello-div-2 and Hello-div-3 on small screens? Currently, the sequence is Hello-div-1, Hello-div-2, Hello-div-3, Hello-div-4. It needs to be Hello-div-1, Hello-div-3, Hello-div-2, Hello-div-4. ...

Locating a button using XPath on Selenium

I have been struggling to find the specific button using Selenium WebDriver: <div class="_1gw6tte"> <div aria-hidden="false"> <div class="_159vfsnv" style="background-color: transparent;"& ...

Arranging columns for optimal display on mobile devices

Check out this website, when I resize to smaller screens, the layout changes: I am trying to replace the second yellow block with the bottom block using Foundation's pull & push options but can't seem to get it right. Do you have any suggestion ...

Retrieve all colors from the style attribute

I'm on the hunt for a method to extract all CSS colors from a website. Currently, I have been able to manage internal and external stylesheets by utilizing document.styleSheets. However, my issue lies in the fact that any styles directly assigned to a ...

The font-weight property appears to be ineffective across all browsers

I'm struggling with the font-weight property in my CSS. Despite trying different values like lighter, bold, and even numerical values like 300, it doesn't seem to have any effect on the font weight in any browser. I am using the following code to ...

What is the best way to modify navbar-default to navbar-inverse when scrolling?

On the header of my webpage, the primary navigation class is called: navbar navbar-default navbar-fixed-top bg I am attempting to create a smoother scroll animation by changing the class when scrolling as follows: navbar navbar-inverse navbar-fixed-top ...

CSS Animations as an Alternative to jQuery's fadeIn(), fadeOut(), and fadeTo() Functions

I currently have the following code snippet: $('#button1').click(function(){ $('#header_bg').fadeTo(15, 0, function() { document.getElementById('header_bg').style.fill = '#FF0000'; }).fadeTo(&ap ...

Is there a way to create tabs in JavaScript without having to edit the <head> section?

I am in need of JavaScript for creating tabs without the necessity of editing the <head> (as it is not possible). My requirement involves having three links and three divs. Whenever a link is clicked, one specific div should be displayed while the o ...

Is there a way to prevent Mac users from using the back and refresh buttons on their browser?

There seems to be a common trend of disabling the back button or refresh button on Windows using JS or Jquery to prevent F5 from working. Can this same method be applied to CMD+R on Mac computers? ...

Modify form layout upon selection of a specific radio button

Hey there! I'm a student currently diving into the world of JavaScript, CSS, and HTML. However, I've encountered a little problem while working on an exercise that involves using Bootstrap. function ShowForm(formId){ document.getElementByI ...

Steps for modifying a cell within a table using a button click

Hello, I am currently working on a project where I want to display a specific div portion when a user clicks on an image icon within a table. However, I am encountering an issue with the JavaScript code as it only shows the div portion in the first row and ...

How can data be transferred between a form and C# code, and then communicated back from C# code?

I have an HTML form that needs to send the user's selection to an aspx.cs file for processing and display the output in a lower textbox. Here is the form: <!-- Form --> <form action="lGen.aspx" method="post" id="lForm" runat="server"> ...

Creating a bold portion of a string

My task involves dynamically creating <p> elements within a div based on the contents of my codeArray, which can vary in size each time. Instead of hard-coding these elements, I have devised the following method: for(i=1;i<codeArray.length;i++) ...

Adjusting box width based on device type: desktop and mobile

I'm currently working on a form that includes several mat-form-fields. I have set the width of these items to 750px, but this does not work well for mobile devices. I am trying to figure out how to make the form or mat-form-field automatically adjust ...

A div containing col-sm class with margin specified on one side

As a newcomer to web design, I've been searching for a solution to this issue with no luck. I am trying to include a fixed vertical navbar on the side of my webpage. To achieve this, I enclosed the entire content in #everything and applied a left mar ...