InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > CSS cookbook

CSS menu using css pseudo element :hover

By admin | Last updated on Mar 25, 2016

CSS hover pseudo element can be used to create simple menu without using any Javascript. This is achieved by making menu children visible when parent element is hovered. Here are the steps to create it:

Create a menu item and its children

First lets create a menu and its children which are always visible. Here is the code:

<style type="text/css">
  .menutest {display: inline-block; background-color:lightgray;}
  .menutest ul {padding:0; margin:0}
</style>

<div class="menutest">
<a href="#">Menu</a>
<ul>
<li><a href="#">Child1</a></li>
<li><a href="#">Child1</a></li>
<li><a href="#">Child1</a></li>
</ul>
</div>
refresh done
try it online

Hide menu children and show on hover

Now we can hide ul using display:none; and show it when hover occurs on menu block with class="menu"

Here is the code and how it renders:

<style type="text/css">
  .menutest {display: inline-block; background-color:lightgray;}
  .menutest ul {padding:0; margin:0}
  .menutest ul {display:none;}
  .menutest:hover ul {display:block};
</style>

<div class="menutest">
<a href="#">Menu</a>
<ul>
<li><a href="#">Child1</a></li>
<li><a href="#">Child1</a></li>
<li><a href="#">Child1</a></li>
</ul>
</div>
refresh done
try it online

Few points to note:

  1. This approach does not work on mobile and there is no hover concept in mobile
  2. To watch hover style for debugging purpose, you can use Google Chrome developer tools to toggle hover event. Here is how it looks:

    chrome-developer-tools-hover-option

    chrome-developer-tools-hover-option

Suggested posts:

  1. jQuery – create iframe with content
  2. PHP – assign multi lines string to a variable
  3. CSS background image example
  4. CSS animation shorthand property
  5. CSS attribute substring selectors
  6. CSS – align text in center horizontally
  7. CSS ::first-line pseudo element
  8. CSS – drop cap effect
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, CSS cookbook, CSS Pseudo Classes, HTML, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress