Hui Yee Tan
Hui Yee Tan

Reputation: 63

CSS styling for input[type='date'] for browsers other than Chrome

Is there equivalent css code of

::-webkit-datetime-edit
::-webkit-datetime-edit-fields-wrapper
::-webkit-datetime-edit-text
::-webkit-datetime-edit-month-field
::-webkit-datetime-edit-day-field
::-webkit-datetime-edit-year-field
::-webkit-inner-spin-button
::-webkit-calendar-picker-indicator

for firefox and microsoft edge? So far I cannot find any documentations / resources regarding to the styling of placeholder for <input type='date'>. Any recommendations / answers is appreciated.

Tried the ::placeholder, ::-ms-input-placeholder and ::-moz-placeholder but they don't work when the input type is date.

Alternatively I am happy to accept the answer if someone can tell me how to hide the default placeholder.

Upvotes: 6

Views: 8555

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21546

By using F12 developer tools to check the HTML and CSS, we can see that Chrome browser using user agent sytelsheet and these pseudo-elements (::-webkit) apply to chrome browser, but in Microsoft Edge browser, it's not using user agent sytelsheet, and these pseudo-elements aren't applied to the input date textbox. So, the code not working in Microsoft Edge.

So, I think you could try to use Microsoft Edge Dev version (chromium based), the code works well on it.

Otherwise, as a workaround, I suggest you could refer to the following code to use text box and datepicker plugin to display the date.

<style>
    .input-field {
        position: relative;
        display: inline-block;
    }

        .input-field > label {
            position: absolute;
            left: 0.5em;
            top: 50%;
            margin-top: -0.5em;
            opacity: 0.5;
        }

        .input-field > input[type=text]:focus + label {
            display: none;
        }

        .input-field > label > span {
            letter-spacing: -2px;
        }

    .month {
        color: cornflowerblue;
    }

    .day {
        color: aqua;
    }

    .year {
        color:darkmagenta
    }

    .separate-letter {
        color: red
    }
</style>
<link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<div class="input-field">
    <input id="input-text-field" type="text" class="date" data-selecteddate="" value="" />
    <label for="input-text-field">
        <span id="span_month" class="month">MM</span>
        <span class="separate-letter">/</span>
        <span  id="span_day" class="day">DD</span>
        <span class="separate-letter">/</span>
        <span id="span_year"  class="year">YYYY</span>
    </label>
</div>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
    $(function ($) {
        $(".date").datepicker({
            onSelect: function (dateText) {
                //display("Selected date: " + dateText + "; input's current value: " + this.value);
                var dataarray = dateText.split("/")
                $("#span_month").html(dataarray[0]);
                $("#span_day").html(dataarray[1]);
                $("#span_year").html(dataarray[2]);

                //clear the textbox value
                this.value = "";
                $("#input-text-field").attr("data-selecteddate", dateText);
            }
        })            
    });
</script>

The result like this (using Microsoft Edge browser):

enter image description here

Upvotes: 1

Related Questions