The vertical size of the "input" element is larger in Firefox Android when using "white-space:pre-wrap" compared to Chrome on Android

Check out this simplified example of a bug:

.wrap {
  white-space: pre-wrap;
}
<div>
<input type="date" class="wrap">
</div>

If you view this code on Firefox Android (latest version), you'll notice that the height appears larger than in Chrome.

https://i.sstatic.net/el9D0.jpg

https://i.sstatic.net/mm9MM.jpg

The issue is related to the "white-space: pre-wrap" property. The reason behind this height discrepancy is unclear. Can someone provide an explanation and suggest a possible solution or alternative? Note that I prefer not to set a fixed height for my input field.

Answer №1

It appears that you have discovered an intriguing issue within Firefox.

A. Here is a suggested solution

SIMPLY: Avoid using whitespace: pre-wrap with

<input type="date">
or
<input type="time">
. There is no need for it... and evidently, the browser developers did not anticipate anyone doing so, as this behavior has not been identified by the developers yet.

It is truly difficult to fathom any reason or scenario where the property whitespace: pre-wrap would be necessary for that element. The date does not automatically go to the next line on its own.

B. The rationale behind your inquiry

Elements with integrated functionalities like date-/time-picker contain additional hidden internal elements. For <input type="date", the typically unseen structure is as follows:

<div id="input-box-wrapper" class="datetime-input-box-wrapper">
    <span id="edit-wrapper" class="datetime-input-edit-wrapper"></span>
    <button id="reset-button" class="datetime-reset-button"></button>
</div>

// You can view this normally hidden structure in Firefox using the developer tools
// when 'white-space: pre-wrap' is applied to the element ...

The CSS for these elements is located within the browser's integrated styles, which are typically inaccessible for modification via css/js. Particularly in Firefox, there is no intended method by the developers to remove the date/time behavior in Firefox entirely.

If you prefer not to utilize the browser's integrated mechanism in Firefox, consider using a text field and incorporating a date picker extension such as jQuery. (This could serve as a workaround for you as well!?)

Additionally, here is the browser's integrated CSS for the concealed elements:

// file: datetimebox.css
// These can also be viewed in Firefox dev tools

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@namespace url("http://www.w3.org/1999/xhtml");
@namespace svg url("http://www.w3.org/2000/svg");

.datetimebox {
  display: flex;
  /* TODO: Enable selection once bug 1455893 is fixed */
  user-select: none;
}

.datetime-input-box-wrapper {
  display: inline-flex;
  flex: 1;
  background-color: inherit;
  min-width: 0;
  justify-content: space-between;
  align-items: center;
}

.datetime-input-edit-wrapper {
  overflow: hidden;
  white-space: nowrap;
  flex-grow: 1;
}

.datetime-edit-field {
  display: inline;
  text-align: center;
  padding: 1px 3px;
  border: 0;
  margin: 0;
  ime-mode: disabled;
  outline: none;
}

.datetime-edit-field:not([disabled="true"]):focus {
  background-color: Highlight;
  color: HighlightText;
  outline: none;
}

.datetime-edit-field[disabled="true"],
.datetime-edit-field[readonly="true"] {
  user-select: none;
}

.datetime-reset-button {
  color: inherit;
  fill: currentColor;
  opacity: .5;
  background-color: transparent;
  border: none;
  flex: none;
  padding-inline: 2px;
}

svg|svg.datetime-reset-button-svg {
  pointer-events: none;
}

Bringing it all together:

The underlying issue lies in the integrated element .datetime-reset-button. Prove it for yourself: applying white-space: pre-wrap to the input causes the reset button wrapper element to expand in height. By directly adding a display: none (in Firefox) to that element in the browser's integrated style, the behavior of the input element is altered, restoring it to its normal size.

Therefore, - white-space: pre-wrap alters the behavior of the field and reserves space for sequences of whitespace (for more details: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space). Typically, the display: inline-flex property from the parent element of the reset-button-container allows the container to flow inline and move to the next line. With wrap set, the button appears on the following line beneath the input field. (This observation also appears to be a bug.) However, with pre-wrap, this flow is disrupted, causing the button to be compressed within the field... leading to its increased height.

Answer №2

When utilizing the CSS property white-space: pre-wrap, Safari will maintain sequences of white space.

A straightforward solution would be:

.wrap {
  white-space: none;
  white-space: -moz-none;
}

I suggest taking a look at CSS-Tricks for more information.

Answer №3

<div> <--linebreak
>whitespace<.innerHTML <-- linebreak
</div>

The inclusion of line breaks and whitespace in your element's .innerHTML can cause it to stretch due to the {white-space: pre;} property being preserved.

<div>.innerHTML</div>
-or-
elem.innerHTML = elem.innerHTML.trim()

This resolves the issue for elements with a closing tag.

However,

<input type="date" id="wrap" value='2021-12-06'>

does not have .innerHTML or a closing tag, and its value lacks whitespace.

https://i.sstatic.net/8xRUL.png

The element's displayed contents may be clipped by the {max-height: 20px} rule without reflection in the inspector.

I have searched for a method to retrieve the displayed contents of this tag but have not found one. I recommend avoiding default input tags and opting for a JavaScript solution instead. Default input fields can be challenging to style consistently across different devices.

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

What is the best way to shrink or enlarge individual sections of the accordion?

Exploring AngularJs Accordions I'm struggling to understand how to control accordions in AngularJS. I have a step-by-step module set up as an accordion and I want to collapse one part while expanding another based on completion. Here is the accordion ...

utilizing jQuery to deliver a $.post request to a .aspx file

Recently, I started following a jQuery tutorial on the phpAcademy channel hosted on thenewboston. In this tutorial, they demonstrate how to create an email validation form using ASP.net instead of PHP. However, despite following all the steps in the tutor ...

Using ES6 without the need for jQuery, populate a select element with JSON data using Javascript

I have a json-formatted dataset that I want to incorporate into my select options. Here is the data: { "timezones": { "country": "Africa", "tz": "Africa/Abidjan" }, { "country": "America", "tz": "America/ ...

Create data structures in python, php, c#, go, c++, and java using a JavaScript/JSON object

I am looking for a way to reverse the output of the JSON.stringify function specifically for node.js. My goal is to generate code snippets for various programming languages by converting JavaScript structures into their native counterparts in python, PHP, ...

Customize YouTube iframe styles in Angular 4+ with TypeScript

Has anyone been successful in overriding the style of an embedded YouTube iframe using Angular 4+ with TypeScript? I've attempted to override a CSS class of the embed iframe, but have not had any luck. Here is the URL to YouTube's stylesheet: ...

Obtain the location of the image source from a text file within an HTML document

I need to create a slideshow displaying a sequence of images. The path to these images will be stored in a text file. How can I read the image paths from the text file? Currently, I have hardcoded code like the example below: <div class="mySlides fade" ...

What methods is Facebook using to manage their onbeforeunload confirmation dialog?

While using Facebook on the latest Chrome browser on my Mac, I observed an interesting behavior. When I began typing in a comment box and then clicked on another link or pressed the back button, a confirmation window popped up asking if I wanted to leave: ...

Having trouble displaying child nodes in MatTreeView with Angular 14?

In an Angular project, I am attempting to display a private group's data from GitLab (utilizing a public one for testing purposes). To achieve this, I have implemented the NestedTreeView component. While the parent nodes are displaying correctly, I am ...

Encountering issues with the routing in my live Node.js project: ERROR

I am encountering issues with my project in production. I suspect it could be due to a misconfiguration or something similar. Could you please review the provided code snippets and see if you notice any potential issues? The project works fine locally, bu ...

Leveraging Emotion API in Video Content (JavaScript or Ruby)

Currently, I'm in the process of uploading a video to the Emotion API for videos, but unfortunately, I have not received any response yet. I managed to successfully upload it using the Microsoft online console. However, my attempts to integrate it in ...

What is the method used by Vue.js to establish observers in computed properties?

Consider this scenario: computed: { greeting() { const state = this.$store.state; if (state.name === 'Joe') { return 'Hey, Joe'; } else { return 'Hello, ' + state.name; } } } Which object(s) w ...

Adjust the x and y coordinates of the canvas renderer

Manipulating the dimensions of the canvas renderer in Three.js is straightforward: renderer = new THREE.CanvasRenderer(); renderer.setSize( 500, 300 ); However, adjusting the position of the object along the x and y axes seems more challenging. I've ...

The issue of video tags not displaying previews on iPhone across all browsers is also accompanied by problems with controls not functioning correctly

As I delve into the world of HTML5 video tags, I encountered an issue where the video wouldn't show a preview frame initially. Instead, only the Resume button would appear. Furthermore, after playing the video with the Resume button, it wouldn't ...

How can I easily tailor Google Map designs for embedding on websites using iframes?

How do I customize the color of Map attributes within an iframe? <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3929.7682222383455!2d78.13052821529986!3d9.95323247656748!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3b00c5e0 ...

Develop content specific to different geographic locations for a web application using PHP

I have been working on a PHP-based online shopping website but have not had much success with it. example.com After trying multiple approaches without success, I have decided to implement an admin feature for adding locations. For instance, if I add "Mad ...

Incorporating a slider into an HTML website

I'm currently working on designing a web page and I want to include a sliding input feature, although I am not entirely sure if it is referred to as a slider. ...

Block entry to HTML files unless the user has logged in utilizing PHP sessions

I have been working on implementing a verification process to check if a user is logged in when accessing a specific HTML file. When a user tries to access something.html without being logged in, I would like to redirect them to index.php and restrict acc ...

showcasing JSON data in an HTML table with pure JavaScript

I am currently utilizing vanilla JS to send a client request to a REST web service that I have designed. The GET method I have implemented returns a list of books (List) in JSON format: 0: {name: "To Pimp a Butterfly", isbn: "ACBCDDSA", availableCopies: 1 ...

Guide on accessing and displaying data table values in console.log using Vue.js and vuetify

Hello, I am new to Vue.js and I am trying to work with data tables. I have copied some code from the Vuetify website and I am attempting to call the desserts' names from a sample table using console.log. Below is the code snippet: <template> ...

Having trouble targeting a div with jQuery

Is it possible to target a specific div within an unordered list and list items? I'm having trouble with it. Here is the HTML code: <ul class="grid"> <div id='categoria' cat='web'></div> <li id=' ...