2015. március 30., hétfő

CSS input::before and Firefox

Recently we got a request to build a simple landing page based on a given PSD in Phormster.
At first it appeared simple but in the end it became tricky.

The tricky part was the last three questions with checkbox handling type. They needed a custom designed checkbox instead of the default browser-created one so we needed to replace them somehow. On top all of this there were different custom designs on checked- and unchecked-states of the checkboxes.


My first thought was the 'old school' way with two little pngs (checked- and unchecked-state) and a piece of JavaScript when I realized we had some really useful tools in CSS3 to do it in the 'modern way'. So I decided to skip Javascript and unnecessary network load out of this work. I googled for the correct syntax of CSS pseudo-classes '::before', '::after' and the ':checked' and my first solution was born:

<style type="text/css">
    input[type="checkbox"]::before {
        display: inline-block;
        width: 33px;
        height: 33px;
        border: 2px solid #e2b553;
        border-radius: 2px;
        -webkit-border-radius: 2px;
        background: black;
        -webkit-box-shadow: inset 0 0 4px 4px rgba(255, 255, 255, 0.4);
        box-shadow: inset 0 0 4px 4px rgba(255, 255, 255, 0.4);
        margin-top: 8px;
        position: absolute;
        left: 0;
        z-index: 999;
    }
    
    input[type="checkbox"]:checked::before {
        -webkit-box-shadow: inset 0 0 20px 20px rgba(255, 255, 255, 0.4);
        box-shadow: inset 0 0 20px 20px rgba(255, 255, 255, 0.4);
    }
</style>
This first CSS definition creates a custom designed square and positiones right on the top of the original checkbox with a bigger z-index so the ugly, browser-created checkbox was hidden by this square. The second definition fills the square with a bigger inset shadow when the checkbox is checked-state making a "checked" look for the square. Finally the hidden checkbox state is changing when user is clicking anywhere inside the bounding <label> tag (this behavior cames from the Twitter Bootstrap what we use within Phormster).

So at this point, it worked like a charm in my Ubuntu's Chromium.


But later during our cross-brower testing we realized, literally nothing happened with the checkboxes in Firefox. So I did some researches and I found this: http://stackoverflow.com/a/12833498/1305261 and this: http://stackoverflow.com/a/12831771/1305261.


These StackOverflow users found the explanation: None of the input html elements have 'content property' so the working Chrome/Chromium behavior is invalid.


After that, it became clear to me I coluldn't ignore javascript to do this job. So here's my final, cross-browser working solution. At least there is no unnecessary network load. This solution is still better than the 'old school' way.


HTML:
<label>
    <input type="checkbox" class="questionValue" id="w_8_value" name="w_8_value" _optin="true" tabindex="7" />
    <div class="content">Lorem ipsum dolor.</div>
</label>
CSS:
<style type="text/css">
    label:before {
        content: '';
        display: inline-block;
        width: 33px;
        height: 33px;
        border: 2px solid #e2b553;
        border-radius: 2px;
        -webkit-border-radius: 2px;
        background: black;
        -webkit-box-shadow: inset 0 0 4px 4px rgba(255, 255, 255, 0.4);
        box-shadow: inset 0 0 4px 4px rgba(255, 255, 255, 0.4);
        margin-top: 8px;
        position: absolute;
        left: 0;
        z-index: 999;
    }
    
    label.checked:before {
        -webkit-box-shadow: inset 0 0 20px 20px rgba(255, 255, 255, 0.4);
        box-shadow: inset 0 0 20px 20px rgba(255, 255, 255, 0.4);
    }
</style>
JavaScript (with jQuery):
<script type="text/javascript">
    $(document).ready(function() {
        $("input[type=checkbox]").unbind("change").change(function() {
            if ($(this).is(":checked")) {
                $(this).parent().addClass("checked");
            } else {
                $(this).parent().removeClass("checked");
            }
        });
    });
</script>
Fiddle's here.

Nincsenek megjegyzések:

Megjegyzés küldése