I'm trying to create different Form elements that reflect Google's material design principles. I have noticed that I'm not really able to find a dropdown example anywhere. The closest that I could find was in the Polymer Project .
My questions are:
<select>
.I have tried transforming a normal select using CSS but this seems to be extremely tricky. The other option is to recreate the select using Javascript and Unordered lists, but how user (Device and Developer) friendly is this solution?
* {
box-sizing: border-box;
}
.dropdown {
display:inline-block;
margin:30px;
position: relative;
padding:0;
border:0;
border-radius:5px;
font-size:24px;
font-weight:400;
background:#26A69A;
color:#FFF;
box-shadow: 0px 8px 17px 0px rgba(0, 0, 0, 0.2), 0px 6px 20px 0px rgba(0, 0, 0, 0.19);
}
.dropdown > input{
display:none;
position:fixed;
z-index:-2;
}
.dropdown > label{
display: inline-block;
max-width: 100%;
margin-bottom: 5px;
font-weight: 700;
}
.drop-ttl{
font-weight:400;
padding:5px 20px;
}
.dropdown ul {
width:100%;
font-weight:400;
margin:0;
padding:0;
position:absolute;
top:0;
text-align:center;
list-style-type:none;
background:#FFF;
color:#26A69A;
box-shadow: 0px 8px 17px 0px rgba(0, 0, 0, 0.2), 0px 6px 20px 0px rgba(0, 0, 0, 0.19);
border-radius:5px;
}
.dropdown ul >li{
padding:5px 20px;
}
.dropdown ul > li > a{
color:inherit;
outline:0;
text-decoration:none;
cursor:pointer;
}
.dropdown ul > li:hover{
background:#DEDEDE;
}
input[type="checkbox"]:checked ~ label > ul {
display:none;
}
<div class="dropdown">
<input id="checkbox1" type="checkbox" name="checkbox" checked />
<label for="checkbox1">
<div class="drop-ttl">Dropdown</div>
<ul>
<li><a>First</a></li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
</ul>
</label>
</div>
This is a nice css-only material dropdown. +1 if it helps.
Here's my take using a bit of jQuery, UL
list and a hidden input
(so you can eventually submit data-value with your form:)
jQuery(function($){
// /////
// MAD-SELECT
var madSelectHover = 0;
$(".mad-select").each(function() {
var $input = $(this).find("input"),
$ul = $(this).find("> ul"),
$ulDrop = $ul.clone().addClass("mad-select-drop");
$(this)
.append('<i class="material-icons">arrow_drop_down</i>', $ulDrop)
.on({
hover : function() { madSelectHover ^= 1; },
click : function() { $ulDrop.toggleClass("show"); }
});
// PRESELECT
$ul.add($ulDrop).find("li[data-value='"+ $input.val() +"']").addClass("selected");
// MAKE SELECTED
$ulDrop.on("click", "li", function(evt) {
evt.stopPropagation();
$input.val($(this).data("value")); // Update hidden input value
$ul.find("li").eq($(this).index()).add(this).addClass("selected")
.siblings("li").removeClass("selected");
});
// UPDATE LIST SCROLL POSITION
$ul.on("click", function() {
var liTop = $ulDrop.find("li.selected").position().top;
$ulDrop.scrollTop(liTop + $ulDrop[0].scrollTop);
});
});
// CLOSE ALL OPNED
$(document).on("mouseup", function(){
if(!madSelectHover) $(".mad-select-drop").removeClass("show");
});
});
::-webkit-scrollbar {
width: 4px;
height: 4px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background-color: rgba(0,0,0, 0.2);
border-radius: 1px;
}
::-webkit-scrollbar-thumb:hover {
background-color: rgba(0,0,0, 0.3);
}
*{box-sizing:border-box; -webkit-box-sizing:border-box;}
html, body{height:100%; margin:0;}
body{
font: 16px/24px Roboto, sans-serif;
background: #fafafa;
}
/*
MAD-SELECT by Roko CB
*/
.mad-select .material-icons{
vertical-align: middle;
}
.mad-select{
position:relative;
display:inline-block;
vertical-align:middle;
border-bottom: 1px solid rgba(0,0,0,0.12);
padding-right: 8px;
}
.mad-select ul {
list-style: none;
display:inline-block;
margin:0; padding:0;
}
.mad-select li{
vertical-align: middle;
white-space: nowrap;
height:24px;
line-height:24px;
display: none;
padding: 8px 16px;
margin:0;
box-sizing: initial;
}
.mad-select > ul:first-of-type{
max-width:120px; /* COMMENT FOR AUTO WIDTH */
}
.mad-select > ul:first-of-type li.selected{
display: inline-block;
height: 24px;
max-width: calc(100% - 24px);
overflow: hidden;
text-overflow: ellipsis;
}
.mad-select i.material-icons{
opacity: 0.5;
margin:0;
padding:0;
}
/*jQ*/
.mad-select ul.mad-select-drop{
position: absolute;
z-index: 9999;
visibility: hidden; opacity:0;
background: #fff;
box-shadow: 0 1px 4px rgba(0,0,0,0.2);
top: 0;
left: 0;
transition: 0.24s;
max-height: 0;
overflow: hidden;
overflow-y: auto;
}
.mad-select ul.mad-select-drop.show{
visibility: visible; opacity: 1;
max-height: 160px; /* COMMENT IF YOU DON?T NEED MAX HEIGHT */
}
.mad-select ul.mad-select-drop li{
display: block;
transition: background 0.24s;
cursor: pointer;
}
.mad-select ul.mad-select-drop li.selected{
background: rgba(0,0,0,0.07);
}
.mad-select ul.mad-select-drop li:hover{
background: rgba(0,0,0,0.04);
}
<link href='https://fonts.googleapis.com/css?family=Roboto:500,400,300,100&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
Select an option:
<div class="mad-select">
<ul>
<li data-value="1">Option 1</li>
<li data-value="2 foo">Option 2</li>
<li data-value="3 bar">Option 3</li>
<li data-value="4">Option 4</li>
<li data-value="5">Option long desc 5</li>
<li data-value="6">Option 6</li>
<li data-value="7">Option 7</li>
<li data-value="8">Option 8</li>
</ul>
<input type="hidden" name="myOptions" value="3 bar">
</div>
you can get it as Material-UI component
if you are using AngularJs, Material Angular project is a great place for material design ( still, dropdown not available. currently they are working for it as md-dropdown directive)
edit: it seems you can even use polymer element by importing this:
<link rel="import" href="components/paper-dropdown-menu/paper-dropdown-menu.html">
There is an issue over at github on this; https://github.com/google/material-design-lite/issues/854.
In the meantime I made my own "css only"-version (simple) http://codepen.io/pudgereyem/pen/PqBxQx/
<!-- Standard Select -->
<div class="mdl-selectfield">
<label>Standard Select</label>
<select class="browser-default">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
If you want a Javascript-version that styles the whole thing, I would recommend checking out materializecss.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With