Skip to main content

Pure CSS3 Responsive DropDown Button and Menu


Pure CSS3 Responsive DropDown Button and Menu




CSS3 Responsive DropDown Button





In this tutorial We are going to take a look at a CSS only technique for creating dropdowns.We will create pure css3 DropDown button without a single line of JavaScript.







You can see the example in action in our editor. Click the HTML and "CSS" button to see the code. You can Download the full source code from the button above.









The Markup




This is what our HTML structure looks like. Here it’s important to note, that the input element has to come first, before the label and before the ul. You will understand why this is needed later, when we check out the CSS.



<div class="dropdown">
<input type="checkbox" id="checkbox_toggle">
<label for="checkbox_toggle">Click to Expand</label>
<ul>
<li><a href="#">Link One</a></li>
<li><a href="#">Link Two</a></li>
<li><a href="#">Link Three</a></li>
<li><a href="#">Link Four</a></li>
</ul>
</div>




As you can see, there’s nothing out of the ordinary here, all of elements are in standard, frequently used HTML:




  • The div will serve as a container for the whole thing.

  • The input type=checkbox is needed because of it’s checked/unchecked property. It will be hidden at all times

  • The label will be used to toggle the input and will also serve as the trigger for your dropdown.

  • The ul is simply a list we want to be visible when the dropdown is extended. Can be any other element.






The CSS




body{
text-align: center;
font: 16px/1.5 sans-serif;
padding-top: 40px;
background-color: #ECEFF1;
}



.dropdown{
position: relative;
display: inline-block;
font-size: 16px;
color: #FFF;
}

/**
Hide the checkbox itself. Checking and unchecking
it we will be done via the label element.
*/

input[type=checkbox]{
display: none;
}


/* Click to expand button */

label{
box-sizing: border-box;
display: inline-block;
width: 100%;
background-color: #57A0D4;
padding: 15px 20px;

cursor: pointer;
text-align: center;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);

-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

input[type=checkbox]:checked ~ ul {
display: block
}

/* The ul will have display:none by default */

ul{
position: absolute;
list-style: none;
text-align: left;
width: 100%;
z-index: 1;
margin:0;
padding:0;
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.2);

display: none;
}


ul li{
padding: 15px;
background-color: #fff;
color: #4FB9A7;
margin-bottom: 1px;
cursor: pointer;
}

ul li:hover{
background-color: #4FB9A7;
color: #FFF;
}

ul li a{
color: inherit;
text-decoration: none;
}


/**

By using the Following-sibling selector (~),
we can target elements positioned after our checkbox in the DOM tree.

With the state pseudo selector (:checked),
we can make changes depending on the state of the checkbox.

Using this combination of selectors
allows to change the color of the label
and show the list of items
only when the checkbox is checked.

*/

input[type=checkbox]:checked ~ label {
background-color: #3D88BD;
}


input[type=checkbox]:checked ~ ul {
display: block;
}








Hope you enjoyed our quick tip!






Comments

Popular posts from this blog

Bouncy Content Filter for big Website

This space-saving content filter allows the users to switch from one category to the other in a fancy way! Each click fires the rotation of the gallery images, revealing the items belonging to the selected category. Content filters  are particularly useful for big websites, where each pixel counts. Lets say you are showing the “ last products ” of your e-commerce . How about giving the users the option to switch to the “most popular” products without a page refresh? A good solution could be to hide the “most popular” items right behind the “last products”, then use the power of CSS3 3D Transforms to rotate the items when the user switches from one option to the other. The bounce effect is optional, but you get the idea! The rotation won’t work on older browsers like IE9, but the experience won’t be broken – just a display on/off with no transitions. Lets dive into the code! Creating the structure We wrapped the filter into a <nav> element. The filter structur...

Content slider with Zoom Effect for a predefined area in each slide

Zoom Slider Today’s Blueprint is a simple content slider with depth-like zoom functionality. Each slide has a predefined zoom area that will be used to calculate the appropriate scale value for a fullscreen fill. Once the icon for zooming is clicked, the zoom area as well as the page get scaled, creating the illusion that the viewer is approaching the item. Once the whole page is covered, we show some more details. Navigating the slider will animate the inner parts of the slide, allowing for an independent control of the image area and the title. We are using CSS transitions and dymanic.js for moving the slide elements. Dymanic.js by MichaĆ«l Villar is a JavaScript library to create physics-based animations. Please note that we are using a couple of modern CSS properties, so only contemporary browsers are supported. The HTML <!-- Main container --> <div class="container"> <!-- Blueprint header --> <header class="bp-header cf"> ...

An AJAX Based Shopping Cart with Drap Drop Item Effect

An AJAX Based Shopping Cart In this tutorial we will create an AJAX Based Shopping Cart with Drag and Drop feature.You can easily use this shopping cart in you store.All the products are going to be stored in a MySQL database, with PHP showing and processing the data. So go ahead, download the demo files and start reading. Step 1 – the MySQL Database If you want to set up a working demo, you’ll need to execute the following SQL code in your database manager (e.g. phpMyAdmin). It will set up the table and insert a few products. The code is also available in table.sql in the demo files. table.sql CREATE TABLE IF NOT EXISTS `internet_shop` ( `id` int ( 6 ) NOT NULL auto_increment, `img` varchar ( 32 ) collate utf8_unicode_ci NOT NULL default '' , `name` varchar ( 64 ) collate utf8_unicode_ci NOT NULL default '' , `description` text collate utf8_unicode_ci NOT NULL , `price` double NOT NULL default '0' , PRIMARY K...